Search in sources :

Example 1 with MacJce

use of com.google.crypto.tink.subtle.MacJce 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");
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) HashType(com.google.crypto.tink.proto.HashType) MacJce(com.google.crypto.tink.subtle.MacJce) HmacKey(com.google.crypto.tink.proto.HmacKey)

Aggregations

HashType (com.google.crypto.tink.proto.HashType)1 HmacKey (com.google.crypto.tink.proto.HmacKey)1 MacJce (com.google.crypto.tink.subtle.MacJce)1 GeneralSecurityException (java.security.GeneralSecurityException)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1