我有一个想要哈希的字符串。在node.js中生成哈希最简单的方法是什么?
散列用于版本控制,而不是安全性。
我有一个想要哈希的字符串。在node.js中生成哈希最简单的方法是什么?
散列用于版本控制,而不是安全性。
当前回答
看看crypto。createhash (algorithm)
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);
});
其他回答
crypto模块使这非常容易。
设置:
// import crypto from 'crypto';
const crypto = require('crypto');
const sha256 = x => crypto.createHash('sha256').update(x, 'utf8').digest('hex');
用法:
sha256('Hello, world. ');
在这里,您可以在您的硬件上对所有受支持的哈希值进行基准测试,这些哈希值由您的node.js版本所支持。有些是加密的,有些只是用于校验和。每个算法计算“Hello World”100万次。每个算法可能需要大约1-15秒(在标准谷歌计算引擎上使用Node.js 4.2.2测试)。
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);
}
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的人
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);
适用于使用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);
超级简单! 快乐编码:)
简单的一句话:
如果你想要UTF8文本哈希:
const hash = require('crypto').createHash('sha256').update('Hash me', 'utf8').digest('hex');
如果你想用Python, PHP, Perl, Github得到相同的散列:
const hash = require('crypto').createHash('sha256').update('Hash me', 'binary').digest('hex');
你也可以用sha1, md5, sha256, sha512替换sha256
看看crypto。createhash (algorithm)
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);
});