Search in sources :

Example 1 with HmacPrfKey

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

the class HmacPrfKeyManagerTest method getPrimitive_worksForSha256.

@Test
public void getPrimitive_worksForSha256() throws Exception {
    HmacPrfKey validKey = factory.createKey(makeHmacPrfKeyFormat(16, HashType.SHA256));
    Prf managerPrf = manager.getPrimitive(validKey, Prf.class);
    Prf directPrf = new PrfHmacJce("HMACSHA256", new SecretKeySpec(validKey.getKeyValue().toByteArray(), "HMAC"));
    byte[] message = Random.randBytes(50);
    assertThat(managerPrf.compute(message, 29)).isEqualTo(directPrf.compute(message, 29));
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) PrfHmacJce(com.google.crypto.tink.subtle.PrfHmacJce) HmacPrfKey(com.google.crypto.tink.proto.HmacPrfKey) Test(org.junit.Test)

Example 2 with HmacPrfKey

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

the class HmacPrfKeyManagerTest method getPrimitive_worksForSha512.

@Test
public void getPrimitive_worksForSha512() throws Exception {
    HmacPrfKey validKey = factory.createKey(makeHmacPrfKeyFormat(16, HashType.SHA512));
    Prf managerPrf = manager.getPrimitive(validKey, Prf.class);
    Prf directPrf = new PrfHmacJce("HMACSHA512", new SecretKeySpec(validKey.getKeyValue().toByteArray(), "HMAC"));
    byte[] message = Random.randBytes(50);
    assertThat(managerPrf.compute(message, 33)).isEqualTo(directPrf.compute(message, 33));
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) PrfHmacJce(com.google.crypto.tink.subtle.PrfHmacJce) HmacPrfKey(com.google.crypto.tink.proto.HmacPrfKey) Test(org.junit.Test)

Example 3 with HmacPrfKey

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

the class HmacPrfKeyManagerTest method testDeriveKey_size27.

@Test
public void testDeriveKey_size27() throws Exception {
    final int keySize = 27;
    byte[] keyMaterial = Random.randBytes(100);
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(HashType.SHA256).build();
    HmacPrfKey key = factory.deriveKey(HmacPrfKeyFormat.newBuilder().setVersion(0).setParams(params).setKeySize(keySize).build(), new ByteArrayInputStream(keyMaterial));
    assertThat(key.getKeyValue()).hasSize(keySize);
    for (int i = 0; i < keySize; ++i) {
        assertThat(key.getKeyValue().byteAt(i)).isEqualTo(keyMaterial[i]);
    }
    assertThat(key.getParams()).isEqualTo(params);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams) HmacPrfKey(com.google.crypto.tink.proto.HmacPrfKey) Test(org.junit.Test)

Example 4 with HmacPrfKey

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

the class HmacPrfKeyManagerTest method getPrimitive_worksForSha1.

@Test
public void getPrimitive_worksForSha1() throws Exception {
    HmacPrfKey validKey = factory.createKey(makeHmacPrfKeyFormat(16, HashType.SHA1));
    Prf managerPrf = manager.getPrimitive(validKey, Prf.class);
    Prf directPrf = new PrfHmacJce("HMACSHA1", new SecretKeySpec(validKey.getKeyValue().toByteArray(), "HMAC"));
    byte[] message = Random.randBytes(50);
    assertThat(managerPrf.compute(message, 19)).isEqualTo(directPrf.compute(message, 19));
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) PrfHmacJce(com.google.crypto.tink.subtle.PrfHmacJce) HmacPrfKey(com.google.crypto.tink.proto.HmacPrfKey) Test(org.junit.Test)

Example 5 with HmacPrfKey

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

the class HmacPrfKeyManagerTest method testDeriveKey_justEnoughKeyMaterial.

@Test
public void testDeriveKey_justEnoughKeyMaterial() throws Exception {
    final int keySize = 32;
    byte[] keyMaterial = Random.randBytes(keySize);
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(HashType.SHA256).build();
    HmacPrfKey key = factory.deriveKey(HmacPrfKeyFormat.newBuilder().setVersion(0).setParams(params).setKeySize(keySize).build(), new ByteArrayInputStream(keyMaterial));
    assertThat(key.getKeyValue()).hasSize(keySize);
    for (int i = 0; i < keySize; ++i) {
        assertThat(key.getKeyValue().byteAt(i)).isEqualTo(keyMaterial[i]);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams) HmacPrfKey(com.google.crypto.tink.proto.HmacPrfKey) Test(org.junit.Test)

Aggregations

HmacPrfKey (com.google.crypto.tink.proto.HmacPrfKey)8 Test (org.junit.Test)8 PrfHmacJce (com.google.crypto.tink.subtle.PrfHmacJce)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 HmacPrfParams (com.google.crypto.tink.proto.HmacPrfParams)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HmacPrfKeyFormat (com.google.crypto.tink.proto.HmacPrfKeyFormat)1