Search in sources :

Example 6 with HmacKeyFormat

use of com.google.crypto.tink.proto.HmacKeyFormat in project tink by google.

the class MacKeyTemplates method createHmacKeyTemplate.

/**
 * @return a {@link KeyTemplate} containing a {@link HmacKeyFormat} with some specified
 *     parameters.
 */
public static KeyTemplate createHmacKeyTemplate(int keySize, int tagSize, HashType hashType) {
    HmacParams params = HmacParams.newBuilder().setHash(hashType).setTagSize(tagSize).build();
    HmacKeyFormat format = HmacKeyFormat.newBuilder().setParams(params).setKeySize(keySize).build();
    return KeyTemplate.newBuilder().setValue(format.toByteString()).setTypeUrl(HmacKeyManager.TYPE_URL).setOutputPrefixType(OutputPrefixType.TINK).build();
}
Also used : HmacParams(com.google.crypto.tink.proto.HmacParams) HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat)

Example 7 with HmacKeyFormat

use of com.google.crypto.tink.proto.HmacKeyFormat in project tink by google.

the class MacKeyTemplatesTest method testCreateHmacKeyTemplate.

@Test
public void testCreateHmacKeyTemplate() throws Exception {
    // Intentionally using "weird" or invalid values for parameters,
    // to test that the function correctly puts them in the resulting template.
    int keySize = 42;
    int tagSize = 24;
    HashType hashType = HashType.SHA512;
    KeyTemplate template = MacKeyTemplates.createHmacKeyTemplate(keySize, tagSize, hashType);
    assertEquals(HmacKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    HmacKeyFormat format = HmacKeyFormat.parseFrom(template.getValue());
    assertEquals(keySize, format.getKeySize());
    assertEquals(tagSize, format.getParams().getTagSize());
    assertEquals(hashType, format.getParams().getHash());
}
Also used : HashType(com.google.crypto.tink.proto.HashType) HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 8 with HmacKeyFormat

use of com.google.crypto.tink.proto.HmacKeyFormat in project tink by google.

the class MacKeyTemplatesTest method testHMAC_SHA256_128BITTAG.

@Test
public void testHMAC_SHA256_128BITTAG() throws Exception {
    KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
    assertEquals(HmacKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    HmacKeyFormat format = HmacKeyFormat.parseFrom(template.getValue());
    assertEquals(32, format.getKeySize());
    assertEquals(16, format.getParams().getTagSize());
    assertEquals(HashType.SHA256, format.getParams().getHash());
}
Also used : HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Aggregations

HmacKeyFormat (com.google.crypto.tink.proto.HmacKeyFormat)8 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)4 Test (org.junit.Test)4 HmacKey (com.google.crypto.tink.proto.HmacKey)2 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)1 AesCtrKeyFormat (com.google.crypto.tink.proto.AesCtrKeyFormat)1 HashType (com.google.crypto.tink.proto.HashType)1 HmacParams (com.google.crypto.tink.proto.HmacParams)1 ByteString (com.google.protobuf.ByteString)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1