NodeJS - SHA256 密码加密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19236327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJS - SHA256 Password Encryption
提问by Dustin
I'm currently learning about encryption and password safety in NodeJS. I'm working with a current example that currently is using PBKDF2, I'd like to switch this out to use SHA256 instead. Is this possible and/or make sense? How would I go about it?
我目前正在学习 NodeJS 中的加密和密码安全。我正在使用当前使用 PBKDF2 的示例,我想将其切换为使用 SHA256。这可能和/或有意义吗?我该怎么办?
var crypto = require('crypto');
var len = 128;
var iterations = 13000;
module.exports = function (pwd, salt, fn) {
if (3 == arguments.length) {
crypto.pbkdf2(pwd, salt, iterations, len, fn);
} else {
fn = salt;
crypto.randomBytes(len, function(err, salt){
if (err) return fn(err);
salt = salt.toString('base64');
crypto.pbkdf2(pwd, salt, iterations, len, function(err, hash){
if (err) return fn(err);
fn(null, salt, hash);
});
});
}
};
回答by hexacyanide
If wanted to generate sha256hashes, then you'd have to drop the iterations and length property as those are specific to pbkdf2. You would then use crypto.createHash()which uses OpenSSL to generate hashes. That being said, the types of hashes you can generate are dependent on the version of OpenSSL that you have installed.
如果想要生成sha256哈希,那么您必须删除迭代和长度属性,因为它们特定于pbkdf2. 然后crypto.createHash(),您将使用which 使用 OpenSSL 生成哈希。也就是说,您可以生成的哈希类型取决于您安装的 OpenSSL 版本。
var crypto = require('crypto');
var hash = crypto.createHash('sha256').update(pwd).digest('base64');
Your specific implementation might look like this:
您的具体实现可能如下所示:
var crypto = require('crypto');
module.exports = function(pwd, fn) {
var hash = crypto.createHash('sha256').update(pwd).digest('base64');
fn(null, hash);
};

