我有一个想要哈希的字符串。在node.js中生成哈希最简单的方法是什么?
散列用于版本控制,而不是安全性。
我有一个想要哈希的字符串。在node.js中生成哈希最简单的方法是什么?
散列用于版本控制,而不是安全性。
当前回答
我使用的是blueimp-md5,它“兼容服务器端环境,如Node.js,模块加载器,如RequireJS, Browserify或webpack和所有网络浏览器。”
像这样使用它:
var md5 = require("blueimp-md5");
var myHashedString = createHash('GreensterRox');
createHash(myString){
return md5(myString);
}
如果在公开场合传递散列值,对它们加盐总是一个好主意,这样人们就更难重新创建它们:
createHash(myString){
var salt = 'HnasBzbxH9';
return md5(myString+salt);
}
其他回答
你可以使用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();
看看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);
});
使用PBKDF2在NodeJs中进行密码哈希
const pbkdf2 = require("pbkdf2");
const crypto = require("crypto");
// UserSchema
//...
// Method to create pbkdf2 hash from plain text
UserSchema.methods.createHash = async function (plainTextPassword) {
// Generate a salt and then create hash
const salt = crypto.randomBytes(16).toString("hex");
const hashedPassword = pbkdf2
.pbkdf2Sync(plainTextPassword, salt, 10, 32, "sha512")
.toString("hex");
// Saving both the dynamic salt and hash in the Database
return [salt, hashedPassword].join("#");
};
// Validating the password with pbkdf2 hash
UserSchema.methods.validatePassword = async function (candidatePassword) {
const hashedPassword = this.password_hash.split("#")[1];
const salt = this.password_hash.split("#")[0];
const hash = pbkdf2
.pbkdf2Sync(candidatePassword, salt, 10, 32, "sha512")
.toString("hex");
if (hash === hashedPassword) {
return true;
}
return false;
};
module.exports.User = mongoose.model("User", UserSchema);
使用Argon2在NodeJs中进行密码哈希
const argon2 = require("argon2");
// UserSchema
...
// Method to generate Hash from plain text using argon2
UserSchema.methods.createHash = async function (plainTextPassword) {
// return password hash
return await argon2.hash(plainTextPassword);
};
// Method to validate the entered password using argon2
UserSchema.methods.validatePassword = async function (candidatePassword) {
return await argon2.verify(this.password_hash, candidatePassword)
};
module.exports.User = mongoose.model("User", UserSchema);
本文可以帮助您设置并执行演示项目。 https://mojoauth.com/blog/hashing-passwords-in-nodejs/
如果你只是想md5哈希一个简单的字符串,我发现这对我有用。
var crypto = require('crypto');
var name = 'braitsch';
var hash = crypto.createHash('md5').update(name).digest('hex');
console.log(hash); // 9b74c9897bac770ffc029102a200c5de
在这里,您可以在您的硬件上对所有受支持的哈希值进行基准测试,这些哈希值由您的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