To use the HMAC_SHA256 you need a shared secret key with message. Below code can be used to create the hash text for String data for the given key.
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class HMACSHA256Test {
public static String encode(String key, String data) throws Exception {
Mac sha256HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256HMAC.init(secretKey);
return Hex.encodeHexString(sha256HMAC.doFinal(data.getBytes("UTF-8")));
}
public static void main(String [] args) throws Exception {
System.out.println("Hashed text: "+ encode("key", "The quick brown fox jumps over the lazy dog"));
}
}
Output
Hashed text: f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
0 Comments