我必须在摘要(字节表示)中生成 CryptoJS.HmacSHA256
的字符串表示。
我需要它,因为我必须复制在 javascript 中生成此类摘要的 python 代码:
print hmac.new("secret", "test", hashlib.sha256).digest()
')�kb��>�y+������:�o��H� '
目标是在 javascript 中复制上述代码的行为。
你能建议我怎么做吗?
最佳答案
您不能简单地将字节发送到 JavaScript。您需要将其转换为文本表示以使其具有可比性。 python 的 hmac 模块和 CryptoJS 都支持十六进制编码。
加密JS:
CryptoJS.HmacSHA256("test", "secret").toString(CryptoJS.enc.Hex)
Python2:
hmac.new("secret", "test", hashlib.sha256).hexdigest()
Python3:
hmac.new("secret".encode("utf-8"), "test".encode("utf-8"), hashlib.sha256).hexdigest()
注意参数顺序的不同。
所有农产品
0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914
关于javascript - 如何在 JS 中获取 CryptoJS.HmacSHA256 的摘要表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29432506/