use of com.google.crypto.tink.proto.AesGcmKey in project tink by google.
the class AesGcmKeyManager method getPrimitive.
/**
* @param key {@code AesGcmKey} proto
*/
@Override
public Aead getPrimitive(MessageLite key) throws GeneralSecurityException {
if (!(key instanceof AesGcmKey)) {
throw new GeneralSecurityException("expected AesGcmKey proto");
}
AesGcmKey keyProto = (AesGcmKey) key;
validate(keyProto);
return new AesGcmJce(keyProto.getKeyValue().toByteArray());
}
use of com.google.crypto.tink.proto.AesGcmKey in project tink by google.
the class AesGcmKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
AesGcmKeyFormat gcmKeyFormat = AesGcmKeyFormat.newBuilder().setKeySize(16).build();
ByteString serialized = ByteString.copyFrom(gcmKeyFormat.toByteArray());
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesGcmKeyManager.TYPE_URL).setValue(serialized).build();
AesGcmKeyManager keyManager = new AesGcmKeyManager();
Set<String> keys = new TreeSet<String>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 27;
for (int i = 0; i < numTests / 3; i++) {
AesGcmKey key = (AesGcmKey) keyManager.newKey(gcmKeyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
key = (AesGcmKey) keyManager.newKey(serialized);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
key = AesGcmKey.parseFrom(keyData.getValue());
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
}
assertEquals(numTests, keys.size());
}
Aggregations