bra*_*sch 654
如果你只想md5哈希一个简单的字符串我发现这对我有用.
var crypto = require('crypto');
var name = 'braitsch';
var hash = crypto.createHash('md5').update(name).digest('hex');
console.log(hash); // 9b74c9897bac770ffc029102a200c5de
Run Code Online (Sandbox Code Playgroud)
tim*_*ooo 210
var filename = process.argv[2];
var crypto = require('crypto');
var fs = require('fs');
var md5sum = crypto.createHash('md5');
var s = fs.ReadStream(filename);
s.on('data', function(d) {
md5sum.update(d);
});
s.on('end', function() {
var d = md5sum.digest('hex');
console.log(d + ' ' + filename);
});
Run Code Online (Sandbox Code Playgroud)
pvo*_*orb 79
Node的加密模块API仍然不稳定.
从版本4.0.0开始,本机Crypto模块不再不稳定.从官方文档:
加密
稳定性:2 - 稳定
该API已被证明令人满意.与npm生态系统的兼容性是一个高优先级,除非绝对必要,否则不会被破坏.
因此,在没有外部依赖性的情况下使用本机实现应该被认为是安全的.
作为参考,当Crypto模块仍然不稳定时,建议将下面提到的模块作为替代解决方案.
$ npm install sha1
Run Code Online (Sandbox Code Playgroud)
然后
var sha1 = require('sha1');
var hash = sha1("my message");
console.log(hash); // 104ab42f1193c336aa2cf08a2c946d5c6fd0fcdb
Run Code Online (Sandbox Code Playgroud)
要么
$ npm install md5
Run Code Online (Sandbox Code Playgroud)
然后
var md5 = require('md5');
var hash = md5("my message");
console.log(hash); // 8ba6c19dc1def5702ff5acbf2aeea5aa
Run Code Online (Sandbox Code Playgroud)
(MD5不安全,但经常被像Gravatar这样的服务使用.)
这些模块的API不会改变!
A-3*_*312 22
sha256("string or binary");
Run Code Online (Sandbox Code Playgroud)
我遇到了其他答案的问题.我建议你设置编码参数来binary使用字节串并防止Javascript(NodeJS)和其他语言/服务(如Python,PHP,Github)之间的不同哈希...
如果您不使用此代码,您可以在NodeJS和Python之间获得不同的哈希值...
NodeJS正在散列字符串的UTF-8表示.其他语言(如Python,PHP或PERL ...)正在散列字节字符串.
我们可以添加二进制参数来使用字节串.
代码:
const crypto = require("crypto");
function sha256(data) {
return crypto.createHash("sha256").update(data, "binary").digest("base64");
// ------ binary: hash the byte string
}
sha256("string or binary");
Run Code Online (Sandbox Code Playgroud)
文档:
您可以通过以下方式解决问题:sha256("\ xac"),"\ xd1","\ xb9","\ xe2","\ xbb","\ x93"等...
其他语言(如PHP,Python,Perl ......)和我的解决方案.update(data, "binary"):
sha1("\xac") //39527c59247a39d18ad48b9947ea738396a3bc47
Run Code Online (Sandbox Code Playgroud)Nodejs默认情况下(没有二进制):
sha1("\xac") //f50eb35d94f1d75480496e54f4b4a472a9148752
Run Code Online (Sandbox Code Playgroud)chi*_*ens 18
简单的一个衬垫:
如果你想要 UTF8 文本哈希:
const hash = require('crypto').createHash('sha256').update('Hash me', 'utf8').digest('hex');
Run Code Online (Sandbox Code Playgroud)
如果你想用 Python、PHP、Perl、Github 获得相同的哈希:
const hash = require('crypto').createHash('sha256').update('Hash me', 'binary').digest('hex');
Run Code Online (Sandbox Code Playgroud)
您也可以替换'sha256'为'sha1', 'md5', 'sha256','sha512'
sdg*_*sdh 14
该crypto模块使这很容易.
建立:
// import crypto from 'crypto';
const crypto = require('crypto');
const sha256 = x => crypto.createHash('sha256').update(x, 'utf8').digest('hex');
Run Code Online (Sandbox Code Playgroud)
用法:
sha256('Hello, world. ');
Run Code Online (Sandbox Code Playgroud)
在这里,您可以对您的硬件上所有支持的哈希进行基准测试,您的node.js版本支持这些哈希.有些是加密的,有些只是校验和.它为每个算法计算"Hello World"100万次.每种算法可能需要大约1-15秒(使用Node.js 4.2.2在标准Google Computing Engine上测试).
for(var i1=0;i1<crypto.getHashes().length;i1++){
var Algh=crypto.getHashes()[i1];
console.time(Algh);
for(var i2=0;i2<1000000;i2++){
crypto.createHash(Algh).update("Hello World").digest("hex");
}
console.timeEnd(Algh);
}
Run Code Online (Sandbox Code Playgroud)
结果:
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 sha384与
RSAEncryption:2343ms
sha512:2371ms
sha512WithRSAEncryption:2434ms
shaWithRSAEncryption:1966ms
ssl2-md5:1853ms
ssl3-md5:1868ms
ssl3-sha1:1971ms
whirlpool:2578ms
我不得不扔掉我的 2 美分,希望这能帮助别人。
对于那些使用 CommonJS 的人
const crypto = require('crypto');
const secret = 'I love writing code, fixing things and building helpful tools';
const hash = crypto.createHmac('sha256', secret).digest('hex');
console.log('Hash successfully generated: ', hash);
Run Code Online (Sandbox Code Playgroud)
对于那些使用 ES 模块的人
const { createHmac } = await import('crypto');
const secret = 'I love writing code, fixing things and building helpful tools';
const hash = createHmac('sha256', secret).digest('hex');
console.log('Hash successfully generated: ', hash);
Run Code Online (Sandbox Code Playgroud)
超级简单!快乐编码:)
| 归档时间: |
|
| 查看次数: |
196878 次 |
| 最近记录: |