Javascript HMAC 和 Python HMAC 未返回相同的哈希值
Javascript HMAC and Python HMAC not returning same hash
我目前有一个 Javascript 代码如下所示
var CryptoJS = require("crypto-js");
var key = "bookbookbook";
var msg = "2020-06-16 20:03:19";
var signature = CryptoJS.HmacSHA1(msg, key);
var checksum = CryptoJS.enc.Utf8.parse(signature);
console.log("checksum: " + CryptoJS.enc.Base64.stringify(checksum));
校验和为 ODNjOWY5NThmYzUxODNkYWM1MjhjZTY3ZTYzYmQxNjE1ZDRkZDQ5Zg==
我试着把它转换成Python
import base64
import time
import hmac
import hashlib
key = "bookbookbook".encode(encoding='utf-8')
msg = "2020-06-16 20:03:19".encode(encoding='utf-8')
digest = hmac.new(key, msg, hashlib.sha1).digest()
checksum = base64.b64encode(digest).decode('utf-8')
print(checksum)
但校验和 returned 是这个 g8n5WPxRg9rFKM5n5jvRYV1N1J8=
如何使它 return 一样?
这一步是不必要的,它会转换你的数据,导致结果错误:var checksum = CryptoJS.enc.Utf8.parse(signature);
(Utf8.parse
用于将 UTF-8 字符串转换为 wordsArray,但你已经有一个 wordsArray 作为来自 CryptoJS.HmacSHA1
的调用)
您的代码应该是:(负责将变量名称更改为更合适的名称)
var CryptoJS = require("crypto-js");
var key = "bookbookbook";
var msg = "2020-06-16 20:03:19";
var encrypted = CryptoJS.HmacSHA1(msg, key);
console.log("encrypted in Base64: " + CryptoJS.enc.Base64.stringify(encrypted));
see it in action in a fiddle, you'll see same result as the python code
我找到了解决方案。尽管我同意 Kaddath 的观点,但 Javascript 很烂,但由于以下事实,我无法更改它
1) 它已经被使用 运行 好几年了
2) 我正在做 Python 的端口,而不是 fixing/updating 原始代码和
3) "da bossman" 说只是端口没有修复
这是生成相同输出的 Python 代码:
import base64
import time
import hmac
import hashlib
import binascii
key = "bookbookbook".encode(encoding='utf-8')
msg = "2020-06-16 20:03:19".encode(encoding='utf-8')
digest = hmac.new(key, msg, hashlib.sha1).digest()
checksum = base64.b64encode(binascii.hexlify(bytearray(digest)))
print("checksum: ", checksum.decode('utf-8'))
我目前有一个 Javascript 代码如下所示
var CryptoJS = require("crypto-js");
var key = "bookbookbook";
var msg = "2020-06-16 20:03:19";
var signature = CryptoJS.HmacSHA1(msg, key);
var checksum = CryptoJS.enc.Utf8.parse(signature);
console.log("checksum: " + CryptoJS.enc.Base64.stringify(checksum));
校验和为 ODNjOWY5NThmYzUxODNkYWM1MjhjZTY3ZTYzYmQxNjE1ZDRkZDQ5Zg==
我试着把它转换成Python
import base64
import time
import hmac
import hashlib
key = "bookbookbook".encode(encoding='utf-8')
msg = "2020-06-16 20:03:19".encode(encoding='utf-8')
digest = hmac.new(key, msg, hashlib.sha1).digest()
checksum = base64.b64encode(digest).decode('utf-8')
print(checksum)
但校验和 returned 是这个 g8n5WPxRg9rFKM5n5jvRYV1N1J8=
如何使它 return 一样?
这一步是不必要的,它会转换你的数据,导致结果错误:var checksum = CryptoJS.enc.Utf8.parse(signature);
(Utf8.parse
用于将 UTF-8 字符串转换为 wordsArray,但你已经有一个 wordsArray 作为来自 CryptoJS.HmacSHA1
的调用)
您的代码应该是:(负责将变量名称更改为更合适的名称)
var CryptoJS = require("crypto-js");
var key = "bookbookbook";
var msg = "2020-06-16 20:03:19";
var encrypted = CryptoJS.HmacSHA1(msg, key);
console.log("encrypted in Base64: " + CryptoJS.enc.Base64.stringify(encrypted));
see it in action in a fiddle, you'll see same result as the python code
我找到了解决方案。尽管我同意 Kaddath 的观点,但 Javascript 很烂,但由于以下事实,我无法更改它 1) 它已经被使用 运行 好几年了 2) 我正在做 Python 的端口,而不是 fixing/updating 原始代码和 3) "da bossman" 说只是端口没有修复
这是生成相同输出的 Python 代码:
import base64
import time
import hmac
import hashlib
import binascii
key = "bookbookbook".encode(encoding='utf-8')
msg = "2020-06-16 20:03:19".encode(encoding='utf-8')
digest = hmac.new(key, msg, hashlib.sha1).digest()
checksum = base64.b64encode(binascii.hexlify(bytearray(digest)))
print("checksum: ", checksum.decode('utf-8'))