Java method which can provide the same output as Python method for HMAC-SHA256 in Hex

谁说我不能喝 提交于 2019-12-02 21:23:42

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

Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec("1234".getBytes(), "HmacSHA256");
sha256_HMAC.init(secretKey);
byte[] hash = sha256_HMAC.doFinal("test".getBytes());
String check = Hex.encodeHexString(hash);
System.out.println(new String(check));

Output
24c4f0295e1bea74f9a5cb5bc40525c8889d11c78c4255808be00defe666671f

Python

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

Output
24c4f0295e1bea74f9a5cb5bc40525c8889d11c78c4255808be00defe666671f
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!