node.js 哈希字符串?
我有一个想要散列的字符串。在 Node.js 中生成哈希值的最简单方法是什么?
哈希用于版本控制,而不是安全性。
I have a string that I want to hash. What's the easiest way to generate the hash in node.js?
The hash is for versioning, not security.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
如果你只想对一个简单的字符串进行 md5 哈希,我发现这对我有用。
If you just want to md5 hash a simple string I found this works for me.
看看 crypto.createHash(算法)
Take a look at crypto.createHash(algorithm)
Node 的加密模块 API 仍然不稳定。从版本 4.0.0 开始,原生 Crypto 模块不再不稳定。来自官方文档:
因此,使用本机实现应该被认为是安全的,无需外部依赖。
作为参考,当 Crypto 模块仍然不稳定时,建议使用下面提到的模块作为替代解决方案。
您还可以使用模块 sha1 或 之一md5 两者都可以完成这项工作。
然后
或
然后
(MD5 不安全,但经常被 Gravatar 等服务使用。)
这些模块的 API 不会改变!
Node's crypto module API is still unstable.As of version 4.0.0, the native Crypto module is not unstable anymore. From the official documentation:
So, it should be considered safe to use the native implementation, without external dependencies.
For reference, the modules mentioned bellow were suggested as alternative solutions when the Crypto module was still unstable.
You could also use one of the modules sha1 or md5 which both do the job.
and then
or
and then
(MD5 is insecure but often used by services like Gravatar.)
The API of these modules won't change!
Simple One Liners:
如果您想要 UTF8 文本哈希:
如果您想使用 Python、PHP、Perl、Github 获得相同的哈希:
您还可以将
'sha256'
替换为'sha1'
、'md5'
、'sha256'
、'sha512'
Simple One Liners:
If you want UTF8 text hash:
If you want to get the same hash with Python, PHP, Perl, Github:
You can also replace
'sha256'
with'sha1'
,'md5'
,'sha256'
,'sha512'
我遇到了其他答案的问题。我建议您将编码参数设置为
binary
以使用字节字符串并防止 Javascript (NodeJS) 和其他语言/服务(如 Python、PHP、Github)之间出现不同的哈希值...如果您不使用这段代码,你可以在 NodeJS 和 Python 之间获得不同的哈希值...
如何获得与 Python、PHP、Perl、Github 相同的哈希值(并防止出现问题):
我们可以添加 binary 参数来使用字节字符串。
代码:
文档:
您可以通过以下方式解决问题:sha256("\xac")、"\xd1"、"\xb9"、"\xe2"、"\xbb"、"\x93" 等...
其他语言 (像 PHP、Python、Perl...) 和我的解决方案
.update(data, "binary")
:Nodejs 默认情况下(没有二进制):
I experienced issue with other answer. I advice you to set encoding argument to
binary
to use the byte string and prevent different hash between Javascript (NodeJS) and other langage/service like Python, PHP, Github...If you don't use this code, you can get a different hash between NodeJS and Python...
How to get the same hash that Python, PHP, Perl, Github (and prevent an issue) :
We can add binary argument to use the byte string.
Code :
Documentation:
You can get the issue with : sha256("\xac"), "\xd1", "\xb9", "\xe2", "\xbb", "\x93", etc...
Other languages (like PHP, Python, Perl...) and my solution with
.update(data, "binary")
:Nodejs by default (without binary) :
crypto
模块使这变得非常简单。设置:
使用:
The
crypto
module makes this very easy.Setup:
Usage:
在这里,您可以对硬件上受您的 Node.js 版本支持的所有支持的哈希进行基准测试。有些是加密的,有些只是用于校验和。每个算法都会计算“Hello World”100 万次。每个算法可能需要大约 1-15 秒(在使用 Node.js 4.2.2 的标准 Google 计算引擎上测试)。
结果:
DSA:1992 毫秒
DSA-SHA:1960 毫秒
DSA-SHA1:2062ms
DSA-SHA1-旧:2124ms
RSA-MD4:1893ms
RSA-MD5:1982ms
RSA-MDC2:2797ms
RSA-RIPEMD160:2101ms
RSA-SHA:1948ms
RSA-SHA1:1908ms
RSA-SHA1-2:2042ms
RSA-SHA224:2176ms
RSA-SHA256:2158ms
RSA-SHA384:2290ms
RSA-SHA512:2357ms
dsa加密:1936ms
dsaWithSHA:1910ms
dsaWithSHA1:1926ms
DSS1:1928毫秒
带 SHA1 的 ECDSA:1880 毫秒
md4: 1833ms
md4WithRSAEncryption:1925ms
md5: 1863ms
md5WithRSAEncryption:1923ms
mdc2:2729ms
mdc2WithRSA:2890ms
成熟:2101ms
ripemd160:2153ms
ripemd160WithRSA: 2210ms
rmd160: 2146ms
沙:1929毫秒
sha1: 1880ms
sha1WithRSA加密:1957ms
sha224: 2121ms
sha224WithRSA加密:2290ms
sha256: 2134ms
sha256WithRSA加密:2190ms
sha384: 2181ms
sha384WithRSA加密:2343ms
sha512:2371ms
sha512WithRSA加密:2434ms
shaWithRSAEncryption:1966ms
ssl2-md5:1853ms
ssl3-md5:1868ms
ssl3-sha1:1971ms
漩涡:2578ms
Here you can benchmark all supported hashes on your hardware, supported by your version of node.js. Some are cryptographic, and some is just for a checksum. Its calculating "Hello World" 1 million times for each algorithm. It may take around 1-15 seconds for each algorithm (Tested on the Standard Google Computing Engine with Node.js 4.2.2).
Result:
DSA: 1992ms
DSA-SHA: 1960ms
DSA-SHA1: 2062ms
DSA-SHA1-old: 2124ms
RSA-MD4: 1893ms
RSA-MD5: 1982ms
RSA-MDC2: 2797ms
RSA-RIPEMD160: 2101ms
RSA-SHA: 1948ms
RSA-SHA1: 1908ms
RSA-SHA1-2: 2042ms
RSA-SHA224: 2176ms
RSA-SHA256: 2158ms
RSA-SHA384: 2290ms
RSA-SHA512: 2357ms
dsaEncryption: 1936ms
dsaWithSHA: 1910ms
dsaWithSHA1: 1926ms
dss1: 1928ms
ecdsa-with-SHA1: 1880ms
md4: 1833ms
md4WithRSAEncryption: 1925ms
md5: 1863ms
md5WithRSAEncryption: 1923ms
mdc2: 2729ms
mdc2WithRSA: 2890ms
ripemd: 2101ms
ripemd160: 2153ms
ripemd160WithRSA: 2210ms
rmd160: 2146ms
sha: 1929ms
sha1: 1880ms
sha1WithRSAEncryption: 1957ms
sha224: 2121ms
sha224WithRSAEncryption: 2290ms
sha256: 2134ms
sha256WithRSAEncryption: 2190ms
sha384: 2181ms
sha384WithRSAEncryption: 2343ms
sha512: 2371ms
sha512WithRSAEncryption: 2434ms
shaWithRSAEncryption: 1966ms
ssl2-md5: 1853ms
ssl3-md5: 1868ms
ssl3-sha1: 1971ms
whirlpool: 2578ms
我不得不扔掉我的 2 美分,希望这能帮助别人。
对于那些使用 CommonJS 的人
对于那些使用 ES 模块的人
超级简单!
快乐编码:)
I had to throw my 2 cents in hope this helps someone out.
For those using CommonJS
For those using ES Modules
Super simple!
Happy Coding :)
我使用 blueimp-md5 它“与 Node.js 等服务器端环境兼容,模块加载器,如 RequireJS、Browserify 或 webpack 以及所有 Web 浏览器。”
像这样使用它:
如果公开传递散列值,那么对它们加盐总是一个好主意,这样人们就更难重新创建它们:
I use blueimp-md5 which is "Compatible with server-side environments like Node.js, module loaders like RequireJS, Browserify or webpack and all web browsers."
Use it like this:
If passing hashed values around in the open it's always a good idea to salt them so that it is harder for people to recreate them:
考虑到来自
http://www.thoughtcrime.org/blog/the-cryptographic-doom-原理/
(简而言之:首先加密,然后验证。然后先验证,然后解密)
我已经在node.js中实现了以下解决方案:
它可以通过以下方式进行测试:
希望这有帮助:-)
Considering the thoughts from
http://www.thoughtcrime.org/blog/the-cryptographic-doom-principle/
(in short: FIRST encrypt, THEN authenticate. Afterwards FIRST verify, THEN decrypt)
I have implemented the following solution in node.js:
It can be tested with:
Hope this helps :-)
您可以使用 crypto-js 加密标准的 javaScript 库,有最简单的方法来生成
sha256
或sha512
you can use crypto-js javaScript library of crypto standards, there is easiest way to generate
sha256
orsha512
即使哈希不是为了安全,如果你想要更广泛的范围,你也可以使用 sha 代替 md5。
普通的 Nodejs sha256 已被弃用。所以,你有一些选择:
Even if the hash is not for security, you can also use sha instead of md5 if you want a wider range.
The normal nodejs sha256 is deprecated. So, you have some alternatives:
使用 PBKDF2 在 NodeJs 中进行密码散列
使用 Argon2 在 NodeJs 中进行密码散列
本文可以帮助您设置并执行演示项目。
https://mojoauth.com/blog/hashing-passwords-in-nodejs/< /a>
Password hashing in NodeJs using PBKDF2
Password hashing in NodeJs using Argon2
This article can help in setup to execute with a demo project.
https://mojoauth.com/blog/hashing-passwords-in-nodejs/