Python hashlib.sha3_512 和 Nodejs jsSHA(“SHA3-512”, “HEX) 中的小差异
我正在尝试加密一个字符串以发送到另一个应用程序,但我的 Python 和 Nodejs 实现的输出不匹配。有人可以告诉我这两种方法之间的区别,或者我做错了什么?我的 Nodejs 版本可以工作并且兼容,并且我希望 Python 版本与其输出相匹配。
NodeJS:
var jsSHA = require("jssha")
var hash = new jsSHA("SHA3-512","HEX");
hash.update("6b0d");
console.log(hash.getHash("B64"))
// $ 9G1hk/ztGnZyk1HGPQMYAtrkg6dFoPW+s5TZou101Yl4QJyaSe+l1uZIEpi/rosNCfpsKOI7kh5usLrn06uYtQ==
Python:
import hashlib
import base64
hash = hashlib.sha3_512("6b0d".encode()).digest()
print(base64.b64encode(hash).decode())
# $ mBg3+maf_9gyfkDIIsREJM8VjCxKEo3J5MrCiK8Bk6FFJZ81IcAc8PjTRB+/3jd0MGnynqjkSZEg++c40JRwhQ==
任何人都可以说出其中的区别,或者我在 Python 中缺少的东西吗?
编辑:添加输出字符串。
Node:
$ 9G1hk/ztGnZyk1HGPQMYAtrkg6dFoPW+s5TZou101Yl4QJyaSe+l1uZIEpi/rosNCfpsKOI7kh5usLrn06uYtQ==
Python:
$ mBg3+maf_9gyfkDIIsREJM8VjCxKEo3J5MrCiK8Bk6FFJZ81IcAc8PjTRB+/3jd0MGnynqjkSZEg++c40JRwhQ==
I'm trying to encrypt a string to send away to another app, and my Python and Nodejs implementation's outputs are not matching. Can someone tell me the difference between these two methods, or what I'm doing wrong? I have the Nodejs version working and compatible, and I'd like the may the Python version match its output.
NodeJS:
var jsSHA = require("jssha")
var hash = new jsSHA("SHA3-512","HEX");
hash.update("6b0d");
console.log(hash.getHash("B64"))
// $ 9G1hk/ztGnZyk1HGPQMYAtrkg6dFoPW+s5TZou101Yl4QJyaSe+l1uZIEpi/rosNCfpsKOI7kh5usLrn06uYtQ==
Python:
import hashlib
import base64
hash = hashlib.sha3_512("6b0d".encode()).digest()
print(base64.b64encode(hash).decode())
# $ mBg3+maf_9gyfkDIIsREJM8VjCxKEo3J5MrCiK8Bk6FFJZ81IcAc8PjTRB+/3jd0MGnynqjkSZEg++c40JRwhQ==
Can anyone tell the difference, or something I'm missing in Python?
Edit: Added output strings.
Node:
$ 9G1hk/ztGnZyk1HGPQMYAtrkg6dFoPW+s5TZou101Yl4QJyaSe+l1uZIEpi/rosNCfpsKOI7kh5usLrn06uYtQ==
Python:
$ mBg3+maf_9gyfkDIIsREJM8VjCxKEo3J5MrCiK8Bk6FFJZ81IcAc8PjTRB+/3jd0MGnynqjkSZEg++c40JRwhQ==
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
"6b0d".encode()
不会从str
十六进制表示形式转换为两个原始字节,而是转换为表示每个字符的四个原始字节 (b' 6b0d'
),而散列(str.encode()
是基于字符编码的编码,默认为UTF-8;没有方便的方法使用它来转换到/从十六进制Python 3 上的表示)。相比之下,告诉库将输入解释为十六进制表示,因此它代表您将其从四个字符解码为两个字节并对其进行哈希处理。
要使 Python 执行相同的操作,请将: 更改
为:
这将获取 jsSHA 代表您生成的用于散列的相同两个字节(相当于
b'\x6b\x0d'
,或者像 Python 那样在回显时表示它,b'k\r'
,因为这两个字节在 ASCII 中都有更短的可打印表示。注意:在旧版本的 Python 上,您需要
导入 binascii
并将bytes.fromhex
替换为binascii.unhexlify
,但所有支持的版本都提供它作为内置bytes
类型的备用构造函数,因此这是最简单的方法。"6b0d".encode()
is not converting fromstr
hex representation to two raw bytes, it's converting to four raw bytes representing each of the characters (b'6b0d'
), and hashing that (str.encode()
is for encoding based on a character encoding, and it defaults to UTF-8; there's no convenient way to use it to convert to/from hex representation on Python 3). By contrast,is telling the library to interpret the input as a hex representation, so it decodes it from four characters to two bytes on your behalf and hashes that.
To make Python do the same thing, change:
to:
which will get the same two bytes that jsSHA is producing for hashing on your behalf (equivalent to
b'\x6b\x0d'
, or as Python would represent it when echoing,b'k\r'
, since both bytes have shorter printable representations in ASCII).Note: On older versions of Python, you'd
import binascii
and replacebytes.fromhex
withbinascii.unhexlify
, but all supported versions provide it as an alternate constructor on the built-inbytes
type, so that's the easiest approach.