Skip to content Skip to sidebar Skip to footer

Java Method Which Can Provide The Same Output As Python Method For Hmac-sha256 In Hex

I am now trying to encode the string using HMAC-SHA256 using Java. The encoded string required to match another set of encoded string generated by Python using hmac.new(mySecret, m

Solution 1:

Are you sure your key and input are identical and correctly encoded in both java and python?

HMAC-SHA256 works the same on both platforms.

Java

Macsha256_HMAC= Mac.getInstance("HmacSHA256");
SecretKeySpecsecretKey=newSecretKeySpec("1234".getBytes(), "HmacSHA256");
sha256_HMAC.init(secretKey);
byte[] hash = sha256_HMAC.doFinal("test".getBytes());
Stringcheck= Hex.encodeHexString(hash);
System.out.println(newString(check));

Output
24c4f0295e1bea74f9a5cb5bc40525c8889d11c78c4255808be00defe666671f

Python

print hmac.new("1234", "test", hashlib.sha256).hexdigest();

Output
24c4f0295e1bea74f9a5cb5bc40525c8889d11c78c4255808be00defe666671f

Post a Comment for "Java Method Which Can Provide The Same Output As Python Method For Hmac-sha256 In Hex"