Search in sources :

Example 6 with HmacPrfKeyFormat

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

the class HmacPrfKeyManagerTest method testHmacSha256Template.

@Test
public void testHmacSha256Template() throws Exception {
    KeyTemplate template = HmacPrfKeyManager.hmacSha256Template();
    assertThat(template.getTypeUrl()).isEqualTo(new HmacPrfKeyManager().getKeyType());
    assertThat(template.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.RAW);
    HmacPrfKeyFormat format = HmacPrfKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertThat(format.getKeySize()).isEqualTo(32);
    assertThat(format.getParams().getHash()).isEqualTo(HashType.SHA256);
}
Also used : HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 7 with HmacPrfKeyFormat

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

the class HmacPrfKeyManagerTest method createKey_checkValues.

@Test
public void createKey_checkValues() throws Exception {
    HmacPrfKeyFormat keyFormat = makeHmacPrfKeyFormat(16, HashType.SHA256);
    HmacPrfKey key = factory.createKey(keyFormat);
    assertThat(key.getKeyValue()).hasSize(keyFormat.getKeySize());
    assertThat(key.getParams().getHash()).isEqualTo(keyFormat.getParams().getHash());
}
Also used : HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) HmacPrfKey(com.google.crypto.tink.proto.HmacPrfKey) Test(org.junit.Test)

Example 8 with HmacPrfKeyFormat

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

the class HmacPrfKeyManagerTest method testDeriveKey_badVersion_throws.

@Test
public void testDeriveKey_badVersion_throws() throws Exception {
    final int keySize = 32;
    byte[] keyMaterial = Random.randBytes(100);
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(HashType.SHA256).build();
    HmacPrfKeyFormat format = HmacPrfKeyFormat.newBuilder().setVersion(1).setParams(params).setKeySize(keySize).build();
    assertThrows(GeneralSecurityException.class, () -> factory.deriveKey(format, new ByteArrayInputStream(keyMaterial)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams) Test(org.junit.Test)

Example 9 with HmacPrfKeyFormat

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

the class HmacPrfKeyManager method createTemplate.

/**
 * @return a {@link KeyTemplate} containing a {@link HmacKeyFormat} with some specified
 *     parameters.
 */
private static KeyTemplate createTemplate(int keySize, HashType hashType) {
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(hashType).build();
    HmacPrfKeyFormat format = HmacPrfKeyFormat.newBuilder().setParams(params).setKeySize(keySize).build();
    return KeyTemplate.create(new HmacPrfKeyManager().getKeyType(), format.toByteArray(), KeyTemplate.OutputPrefixType.RAW);
}
Also used : HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams)

Aggregations

HmacPrfKeyFormat (com.google.crypto.tink.proto.HmacPrfKeyFormat)9 Test (org.junit.Test)6 HmacPrfParams (com.google.crypto.tink.proto.HmacPrfParams)4 KeyTemplate (com.google.crypto.tink.KeyTemplate)2 ByteString (com.google.protobuf.ByteString)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HmacPrfKey (com.google.crypto.tink.proto.HmacPrfKey)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1