我有一个想要哈希的字符串。在node.js中生成哈希最简单的方法是什么?
散列用于版本控制,而不是安全性。
我有一个想要哈希的字符串。在node.js中生成哈希最简单的方法是什么?
散列用于版本控制,而不是安全性。
当前回答
我不得不把我的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);
超级简单! 快乐编码:)
其他回答
看看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-js javaScript库的加密标准,有最简单的方法生成sha256或sha512
const SHA256 = require("crypto-js/sha256");
const SHA512 = require("crypto-js/sha512");
let password = "hello"
let hash_256 = SHA256 (password).toString();
let hash_512 = SHA512 (password).toString();
考虑到来自 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));
希望这对你有所帮助:-)
我不得不把我的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