Nodejs crypto vs python hashlib [英] Nodejs crypto vs python hashlib
问题描述
我使用的python是:
hash = hashlib.sha512()
hash.update(salt)
hash.update(password.encode('utf8'))
hash.digest()
节点/ coffeescript是:
crypto.createHash('sha512')。update(salt,'binary')。update(password,'utf8')。digest $ b
这些行应该产生相同的结果,但由于某些原因,它们没有。帮助?
他们似乎产生相同的结果,但因为节点的 digest() code>返回一个Unicode字符串,而Python返回一个
个字节
对象,这可能并不明显:
CoffeeScript 1.4.0 on Node 0.8.11:
coffee> salt ='abc'
/ pre>
'abc'
咖啡> password ='def'
'def'
coffee> d = crypto.createHash('sha512')。update(salt,'binary')。update(password,'utf8')。digest()
'ã.ñ#èí& ezK = \\\»v \\\\\\CWEVNAP§\\\¾*}¶\\\= 9\f +¹〜-L1\\\ÜiÖ±&\\\õ©ç'
Python 3.3.0:
>> > salt,password = b'abc','def'
>>>> hash = hashlib.sha512()
>>> hash.update(salt)
>>> hash.update(password.encode('utf8'))
>>> d = hash.digest()
>>> print(d)
b'\xe3.\xf1\x96#\xe8\xed\x9d& \x7fez\x81\x94K = \x07\xad\xbbv \x85\x18\x06\x8e\x88CWEVN\x8dAP\xa0\xa7\x03\xbe *} \x88\xb6\x1e = 9\x0c + \xb9〜 -L1\x1f\xdci\xd6\xb1& \x7f\x05\xf5\x9a\xa9 \xe7'
看起来差不多吧?但是,如果仔细观察,可打印字符是相同的 -
CWEVN
运行是非常明显的。如果您将其解释为Latin-1,您可以看到更多相似之处。>>> print(d.decode('latin1'))
ã.ñ#èí& ezK =»vCWEVNAP§¾*}¶= 9
+¹〜-L1ÜiÖ±&õ©ç
很明显,这是完全相同的字符串,只是Node正在转义不可打印的字符。 p>
和Python 2.7.2:
>>> salt,password ='abc',u'def'
>>> hash = hashlib.sha512()
>>> hash.update(salt)
>>> hash.update(password.encode('utf8'))
>>> d = hash.digest()
>>> print(d)
?。????& ez?K = ?? v?CWEVN?AP ??? *} ?? = 9
+?〜-L1?i& ;? ?
>>> print(d.decode('latin1'))
ã.ñ#èí& ezK =»vCWEVNAP§¾*}¶= 9
+¹〜-L1ÜiÖ±&õ©ç
同样的字符串。
鉴于我的终端,C语言环境等都是UTF-8(这是OS X),我不知道为什么CoffeeScript解码为Latin-1。
I'm trying to make a python function and a nodejs function compute the same hash. However, it seems like the binary that is outputted is different between nodejs crypto and python hashlib.
The python I'm using is:
hash = hashlib.sha512() hash.update(salt) hash.update(password.encode('utf8')) hash.digest()
The node/coffeescript is:
crypto.createHash('sha512').update(salt, 'binary').update(password, 'utf8').digest()
These lines should produce the same result, but for some reason they don't. Help?
解决方案They do seem to produce the same result, but because node's
digest()
returns a Unicode string, while Python's returns abytes
object, this may not be immediately obvious:CoffeeScript 1.4.0 on Node 0.8.11:
coffee> salt='abc' 'abc' coffee> password='def' 'def' coffee> d = crypto.createHash('sha512').update(salt, 'binary').update(password, 'utf8').digest() 'ã.ñ#èí&ezK=\u0007»v\u0018\u0006CWEVNAP §\u0003¾*}¶\u001e=9\f+¹~-L1\u001fÜiÖ±&\u0005õ© ç'
Python 3.3.0:
>>> salt, password=b'abc', 'def' >>> hash = hashlib.sha512() >>> hash.update(salt) >>> hash.update(password.encode('utf8')) >>> d = hash.digest() >>> print(d) b'\xe3.\xf1\x96#\xe8\xed\x9d&\x7fez\x81\x94K=\x07\xad\xbbv\x85\x18\x06\x8e\x88CWEVN\x8dAP\xa0\xa7\x03\xbe*}\x88\xb6\x1e=9\x0c+\xb9~-L1\x1f\xdci\xd6\xb1&\x7f\x05\xf5\x9a\xa9 \xe7'
Looks pretty different, right? But if you look closely, the printable characters are the same—that
CWEVN
run is pretty obvious. And you can see even more similarities if you decode it as Latin-1…>>> print(d.decode('latin1')) ã.ñ#èí&ezK=»vCWEVNAP §¾*}¶=9 +¹~-L1ÜiÖ±&õ© ç
It's pretty obvious this is the exact same string, it's just that Node is escaping the non-printable characters.
And Python 2.7.2:
>>> salt, password='abc', u'def' >>> hash = hashlib.sha512() >>> hash.update(salt) >>> hash.update(password.encode('utf8')) >>> d = hash.digest() >>> print(d) ?.?#??&ez??K=??v???CWEVN?AP???*}??=9 +?~-L1?iֱ&? ? >>> print(d.decode('latin1')) ã.ñ#èí&ezK=»vCWEVNAP §¾*}¶=9 +¹~-L1ÜiÖ±&õ© ç
Again, same string.
Given that my terminal, C locale, etc. are all UTF-8 (this is OS X), I have no idea why CoffeeScript is decoding as Latin-1.
这篇关于Nodejs crypto vs python hashlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!