Węzeł.js haszowanie haseł
Obecnie używam następujących hashów:
var pass_shasum = crypto.createHash('sha256').update(req.body.password).digest('hex');
Czy mógłbyś zasugerować ulepszenia, aby uczynić projekt bezpieczniejszym?
6 answers
Używam kodu follwing do zasolenia i hashowania haseł.
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);
});
};
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-27 08:33:42
Bcrypt można również nazwać synchronicznie. Przykładowy Coffeescript:
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-25 18:02:17
Istnieje również moduł bcrypt-nodejs dla węzła. https://github.com/shaneGirish/bcrypt-nodejs .
Poprzednio używałem już wspomnianego tutaj modułu bcrypt, ale wpadłem w problemy na win7 x64. Z drugiej strony bcrypt-nodejs jest czystą implementacją JS bcrypt i nie posiada żadnych zależności.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-03 01:07:20
Możesz użyć pakietu bcrypt-js do szyfrowania hasła.
- Try npm i bcryptjs
- var bcrypt = require('bcryptjs') in top.
- aby hashować hasło:
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash("B4c0/\/", salt, function(err, hash) {
// Store hash in your password DB.
});
});
- aby sprawdzić hasło,
// Load hash from your password DB.
bcrypt.compare("B4c0/\/", hash, function(err, res) {
// res === true
});
Możesz odwiedzić https://www.npmjs.com/package/bcryptjs aby uzyskać więcej informacji na temat bcryptjs.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-24 07:43:37
Spróbuj użyć Bcrypt, zabezpiecza hasło za pomocą hashowania.
bcrypt.hash(req.body.password, salt, (err, encrypted) => {
user.password = encrypted
next()
})
Gdzie sól to wartość kosztowa określająca siłę mieszania. Podczas logowania porównaj hasło za pomocą bcrypt.metoda porównania:
bcrypt.compare(password, user.password, (err, same) => {
if (same) {
req.session.userId = user._id
res.redirect('/bloglist')
} else {
res.end('pass wrong')
}
})
Aby uzyskać więcej informacji, zajrzyj na ten blog: https://medium.com/@nitinmanocha16/bcrypt-and-nodejs-e00a0d1df91f
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-24 07:45:48
Bcrypt nie jest złym wyborem, ale jest kilka gotchas :
- zostanie obcięty na
NUL
bajtach. - zostanie skrócony po 72 znakach. Jeśli używasz haseł, może to nieoczekiwanie osłabić twoje hasło.
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...
}
})();
Dokumentacja dla sodium-plus na Githubie zawiera hashowanie i przechowywanie haseł.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-10-12 07:31:10