每次服务器重新启动时散列模式都会更改
Hashing pattern changes every time the server restarts
我有一个 table,其中有两列:license & activated。
我正在存储许可证代码,不是以纯文本形式而是以散列形式存储。我正在使用 bcrypt 模块生成带有第 10 轮盐的散列。当我在数据库中搜索许可证时出现问题。要搜索许可证,我首先生成许可证的哈希值,然后搜索数据库。它工作正常,直到服务器重新启动。
当服务器重新启动时,它会为相同的许可证代码生成不同的哈希字符串。有没有什么办法解决这一问题?每次服务器重新启动时,如何才能停止更改相同许可证代码的哈希模式?
问题在于盐和您使用 bcrypt 的方式。
您正在使用 saltRounds 生成新的盐(一个随机值),因此哈希总是不同的。
如果使用固定的盐值,哈希值将是相同的。请参阅以下示例:
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
const someOtherPlaintextPassword = 'not_bacon';
bcrypt.genSalt(saltRounds, function(err, salt) {
console.log('new salt:%s',salt);
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
// Store hash in your password DB.
console.log('new hash:%s',hash);
});
})
//first time generated values were below, but each run will generate new values:
//salt:b$X4kv7j5ZcG39WgogSl16au
//hash:b$X4kv7j5ZcG39WgogSl16aupL0..j8Fmm8Lwgq92uWuM5KyXhE6tpO
//Generate the same hash value as fixed salt value is used
salt = 'b$X4kv7j5ZcG39WgogSl16au'
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
console.log('same value:%s', hash); //hash:b$X4kv7j5ZcG39WgogSl16aupL0..j8Fmm8Lwgq92uWuM5KyXhE6tpO
});
// Test comparison
hash='b$X4kv7j5ZcG39WgogSl16aupL0..j8Fmm8Lwgq92uWuM5KyXhE6tpO' //first hash of myPlaintextPassword
bcrypt.compare(myPlaintextPassword, hash, function(err, res) {
console.log('Test using the correct password/key - should authenticate');
if (res === true) {
console.log('authenticated ');
} else {
console.log('NOT authenticated');
}
});
bcrypt.compare(someOtherPlaintextPassword, hash, function(err, res) {
console.log('Test using an incorrect password/key - should fail authentication');
if (res === true) {
console.log('authenticated');
} else {
console.log('NOT authenticated');
}
});
也许使用一些其他值作为主键(许可证号)和一些加密值来指示它是否是有效许可证。
我有一个 table,其中有两列:license & activated。
我正在存储许可证代码,不是以纯文本形式而是以散列形式存储。我正在使用 bcrypt 模块生成带有第 10 轮盐的散列。当我在数据库中搜索许可证时出现问题。要搜索许可证,我首先生成许可证的哈希值,然后搜索数据库。它工作正常,直到服务器重新启动。
当服务器重新启动时,它会为相同的许可证代码生成不同的哈希字符串。有没有什么办法解决这一问题?每次服务器重新启动时,如何才能停止更改相同许可证代码的哈希模式?
问题在于盐和您使用 bcrypt 的方式。 您正在使用 saltRounds 生成新的盐(一个随机值),因此哈希总是不同的。 如果使用固定的盐值,哈希值将是相同的。请参阅以下示例:
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
const someOtherPlaintextPassword = 'not_bacon';
bcrypt.genSalt(saltRounds, function(err, salt) {
console.log('new salt:%s',salt);
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
// Store hash in your password DB.
console.log('new hash:%s',hash);
});
})
//first time generated values were below, but each run will generate new values:
//salt:b$X4kv7j5ZcG39WgogSl16au
//hash:b$X4kv7j5ZcG39WgogSl16aupL0..j8Fmm8Lwgq92uWuM5KyXhE6tpO
//Generate the same hash value as fixed salt value is used
salt = 'b$X4kv7j5ZcG39WgogSl16au'
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
console.log('same value:%s', hash); //hash:b$X4kv7j5ZcG39WgogSl16aupL0..j8Fmm8Lwgq92uWuM5KyXhE6tpO
});
// Test comparison
hash='b$X4kv7j5ZcG39WgogSl16aupL0..j8Fmm8Lwgq92uWuM5KyXhE6tpO' //first hash of myPlaintextPassword
bcrypt.compare(myPlaintextPassword, hash, function(err, res) {
console.log('Test using the correct password/key - should authenticate');
if (res === true) {
console.log('authenticated ');
} else {
console.log('NOT authenticated');
}
});
bcrypt.compare(someOtherPlaintextPassword, hash, function(err, res) {
console.log('Test using an incorrect password/key - should fail authentication');
if (res === true) {
console.log('authenticated');
} else {
console.log('NOT authenticated');
}
});
也许使用一些其他值作为主键(许可证号)和一些加密值来指示它是否是有效许可证。