try { // Generate a key for the HMAC-MD5 keyed-hashing algorithm
KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey key = keyGen.generateKey();
// Generate a key for the HMAC-SHA1 keyed-hashing algorithm
keyGen = KeyGenerator.getInstance("HmacSHA1");
key = keyGen.generateKey(); }
catch (java.security.NoSuchAlgorithmException e) { }
Above code would give us keys to digest a message using HMAC. Now i wanted to implement a HMAC logic in java where the key would be given by user.
Any ideas?
Construct a
KeySpecusing the provided user key (either aSecretKeySpecif it’s a byte array, or aPBEKeySpecif it’s a password), then use aSecretKeyFactoryto turn that into aSecretKey.