我有一个想要哈希的字符串。在node.js中生成哈希最简单的方法是什么?
散列用于版本控制,而不是安全性。
我有一个想要哈希的字符串。在node.js中生成哈希最简单的方法是什么?
散列用于版本控制,而不是安全性。
当前回答
考虑到来自 http://www.thoughtcrime.org/blog/the-cryptographic-doom-principle/ (简而言之:首先加密,然后验证。之后先验证,然后解密) 我在node.js中实现了以下解决方案:
function encrypt(text,password){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text,password){
var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
function hashText(text){
var hash = crypto.createHash('md5').update(text).digest("hex");
//console.log(hash);
return hash;
}
function encryptThenAuthenticate(plainText,pw)
{
var encryptedText = encrypt(plainText,pw);
var hash = hashText(encryptedText);
return encryptedText+"$"+hash;
}
function VerifyThenDecrypt(encryptedAndAuthenticatedText,pw)
{
var encryptedAndHashArray = encryptedAndAuthenticatedText.split("$");
var encrypted = encryptedAndHashArray[0];
var hash = encryptedAndHashArray[1];
var hash2Compare = hashText(encrypted);
if (hash === hash2Compare)
{
return decrypt(encrypted,pw);
}
}
它可以用以下方法进行测试:
var doom = encryptThenAuthenticate("The encrypted text",user.cryptoPassword);
console.log(VerifyThenDecrypt(doom,user.cryptoPassword));
希望这对你有所帮助:-)
其他回答
sha256("string or binary");
我在其他答案上遇到了问题。我建议你将编码参数设置为二进制以使用字节字符串,并防止Javascript (NodeJS)和其他语言/服务之间的不同哈希,如Python, PHP, Github…
如果你不使用这段代码,你可以在NodeJS和Python之间得到不同的散列…
如何获得相同的散列,Python, PHP, Perl, Github(并防止一个问题):
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");
文档:
加密。createHash(algorithm[, options]):该算法依赖于平台上OpenSSL版本支持的可用算法。 hash.digest([encoding]):编码可以是'hex', 'latin1'或'base64'。(64进制更短)。
你可以把问题:sha256(“\ xac”),“\ xd1”、“\ xb9”,“\ xe2”、“\ xbb”,“\ x93”等……
其他语言(如PHP, Python, Perl…)和我用.update(data, "binary")的解决方案: sha1(“\ xac”)/ / 39527 c59247a39d18ad48b9947ea738396a3bc47 默认情况下(不含二进制文件): sha1 / / f50eb35d94f1d75480496e54f4b4a472a9148752(“\ xac”)
如果你只是想md5哈希一个简单的字符串,我发现这对我有用。
var crypto = require('crypto');
var name = 'braitsch';
var hash = crypto.createHash('md5').update(name).digest('hex');
console.log(hash); // 9b74c9897bac770ffc029102a200c5de
节点的加密模块API仍然不稳定。
从4.0.0版本开始,本地Crypto模块不再不稳定。来自官方文件:
加密 稳定性:2 -稳定 该API已被证明令人满意。与npm生态系统的兼容性 是高优先级的,除非绝对必要,否则不会被破坏。
因此,应该认为使用本机实现是安全的,没有外部依赖。
作为参考,下面提到的模块是在Crypto模块仍然不稳定时作为替代解决方案提出的。
您还可以使用sha1或md5模块中的一个,它们都可以完成这项工作。
$ npm install sha1
然后
var sha1 = require('sha1');
var hash = sha1("my message");
console.log(hash); // 104ab42f1193c336aa2cf08a2c946d5c6fd0fcdb
or
$ npm install md5
然后
var md5 = require('md5');
var hash = md5("my message");
console.log(hash); // 8ba6c19dc1def5702ff5acbf2aeea5aa
(MD5是不安全的,但经常被Gravatar等服务使用。)
这些模块的API不会改变!
考虑到来自 http://www.thoughtcrime.org/blog/the-cryptographic-doom-principle/ (简而言之:首先加密,然后验证。之后先验证,然后解密) 我在node.js中实现了以下解决方案:
function encrypt(text,password){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text,password){
var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
function hashText(text){
var hash = crypto.createHash('md5').update(text).digest("hex");
//console.log(hash);
return hash;
}
function encryptThenAuthenticate(plainText,pw)
{
var encryptedText = encrypt(plainText,pw);
var hash = hashText(encryptedText);
return encryptedText+"$"+hash;
}
function VerifyThenDecrypt(encryptedAndAuthenticatedText,pw)
{
var encryptedAndHashArray = encryptedAndAuthenticatedText.split("$");
var encrypted = encryptedAndHashArray[0];
var hash = encryptedAndHashArray[1];
var hash2Compare = hashText(encrypted);
if (hash === hash2Compare)
{
return decrypt(encrypted,pw);
}
}
它可以用以下方法进行测试:
var doom = encryptThenAuthenticate("The encrypted text",user.cryptoPassword);
console.log(VerifyThenDecrypt(doom,user.cryptoPassword));
希望这对你有所帮助:-)
在这里,您可以在您的硬件上对所有受支持的哈希值进行基准测试,这些哈希值由您的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