use of com.google.crypto.tink.proto.HmacKey in project tink by google.
the class RegistryTest method testGetKeyManager_wrongType_shouldThrowException.
@Test
public void testGetKeyManager_wrongType_shouldThrowException() throws Exception {
// TODO(thaidn): make this assignment throw some exception.
KeyManager<Aead> wrongType = Registry.getKeyManager(MacConfig.HMAC_TYPE_URL);
KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
HmacKey hmacKey = (HmacKey) Registry.newKey(template);
try {
Aead unused = wrongType.getPrimitive(hmacKey);
fail("Expected ClassCastException");
} catch (ClassCastException e) {
assertExceptionContains(e, "MacJce cannot be cast to com.google.crypto.tink.Aead");
}
}
use of com.google.crypto.tink.proto.HmacKey in project tink by google.
the class HmacKeyManager method getPrimitive.
/**
* @param key {@code HmacKey} proto
*/
@Override
public Mac getPrimitive(MessageLite key) throws GeneralSecurityException {
if (!(key instanceof HmacKey)) {
throw new GeneralSecurityException("expected HmacKey proto");
}
HmacKey keyProto = (HmacKey) key;
validate(keyProto);
HashType hash = keyProto.getParams().getHash();
byte[] keyValue = keyProto.getKeyValue().toByteArray();
SecretKeySpec keySpec = new SecretKeySpec(keyValue, "HMAC");
int tagSize = keyProto.getParams().getTagSize();
switch(hash) {
case SHA1:
return new MacJce("HMACSHA1", keySpec, tagSize);
case SHA256:
return new MacJce("HMACSHA256", keySpec, tagSize);
case SHA512:
return new MacJce("HMACSHA512", keySpec, tagSize);
default:
throw new GeneralSecurityException("unknown hash");
}
}
Aggregations