我目前正在使用以下方法对密码进行哈希处理:
var pass_shasum = crypto.createHash('sha256').update(req.body.password).digest('hex');
您能否提出改进建议以使项目更安全?
我目前正在使用以下方法对密码进行哈希处理:
var pass_shasum = crypto.createHash('sha256').update(req.body.password).digest('hex');
您能否提出改进建议以使项目更安全?
我使用以下代码对密码进行加盐和哈希处理。
var bcrypt = require('bcrypt');
exports.cryptPassword = function(password, callback) {
bcrypt.genSalt(10, function(err, salt) {
if (err)
return callback(err);
bcrypt.hash(password, salt, function(err, hash) {
return callback(err, hash);
});
});
};
exports.comparePassword = function(plainPass, hashword, callback) {
bcrypt.compare(plainPass, hashword, function(err, isPasswordMatch) {
return err == null ?
callback(null, isPasswordMatch) :
callback(err);
});
};
bcrypt 也可以同步调用。示例咖啡脚本:
bcrypt = require('bcrypt')
encryptionUtil =
encryptPassword: (password, salt) ->
salt ?= bcrypt.genSaltSync()
encryptedPassword = bcrypt.hashSync(password, salt)
{salt, encryptedPassword}
comparePassword: (password, salt, encryptedPasswordToCompareTo) ->
{encryptedPassword} = @encryptPassword(password, salt)
encryptedPassword == encryptedPasswordToCompareTo
module.exports = encryptionUtil
节点也有 bcrypt-nodejs 模块。https://github.com/shaneGirish/bcrypt-nodejs。
之前我使用了这里已经提到的 bcrypt 模块,但是在 win7 x64 上遇到了问题。另一方面,bcrypt-nodejs 是 bcrypt 的纯 JS 实现,完全没有任何依赖关系。
bcrypt 与打字稿
npm i bcrypt npm i -D @types/bcrypt
import * as bcrypt from 'bcrypt';
export const Encrypt = {
cryptPassword: (password: string) =>
bcrypt.genSalt(10)
.then((salt => bcrypt.hash(password, salt)))
.then(hash => hash),
comparePassword: (password: string, hashPassword: string) =>
bcrypt.compare(password, hashPassword)
.then(resp => resp)
}
示例:加密
const myEncryptPassword = await Encrypt.cryptPassword(password);
示例:比较
const myBoolean = await Encrypt.comparePassword(password, passwordHash);
您可以使用 bcrypt-js 包来加密密码。
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash("B4c0/\/", salt, function(err, hash) {
// Store hash in your password DB.
});
});
// Load hash from your password DB.
bcrypt.compare("B4c0/\/", hash, function(err, res) {
// res === true
});
您可以访问https://www.npmjs.com/package/bcryptjs了解有关 bcryptjs 的更多信息。
Try using Bcrypt, it secures the password using hashing.
bcrypt.hash(req.body.password, salt, (err, encrypted) => {
user.password = encrypted
next()
})
Where salt is the cost value which specifies the strength of hashing. While logging in, compare the password using bcrypt.compare method:
bcrypt.compare(password, user.password, (err, same) => {
if (same) {
req.session.userId = user._id
res.redirect('/bloglist')
} else {
res.end('pass wrong')
}
})
For more info, refer to this blog: https://medium.com/@nitinmanocha16/bcrypt-and-nodejs-e00a0d1df91f
Bcrypt 不是一个糟糕的选择,但有一些陷阱:
NUL
字节。截至 2019 年 10 月,Argon2id是最佳选择。
与 Argon2id 交互的首选方式是通过 libsodium(一个提供许多功能的密码库)。有几种绑定可供选择,但最简单的可能是钠加。
const SodiumPlus = require('sodium-plus').SodiumPlus;
let sodium;
(async function(){
if (!sodium) sodium = await SodiumPlus.auto(); // Autoload the backend
let password = 'Your example password goes here. Provided by the user.';
// Hashing...
let hash = await sodium.crypto_pwhash_str(
password,
sodium.CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
sodium.CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
// You can safely store {hash} in a database.
// Checking that a stored hash is still up to snuff...
let stale = await sodium.crypto_pwhash_str_needs_rehash(
hash,
sodium.CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
sodium.CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
if (stale) {
// Rehash password, update database
}
// Password verification
let valid = await sodium.crypto_pwhash_str_verify(password, hash);
if (valid) {
// Proceed...
}
})();
Github 上有关 sodium-plus的文档包括密码哈希和存储。