Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 每次服务器重新启动时,哈希模式都会更改_Node.js_Hash_Bcrypt - Fatal编程技术网

Node.js 每次服务器重新启动时,哈希模式都会更改

Node.js 每次服务器重新启动时,哈希模式都会更改,node.js,hash,bcrypt,Node.js,Hash,Bcrypt,我有一个表,其中有两列:许可证&已激活 我存储的许可证代码不是纯文本,而是散列的。我正在使用bcrypt模块生成salt第10轮的哈希。在数据库中搜索许可证时出现问题。要搜索许可证,我首先生成许可证的哈希,然后搜索数据库。在服务器重新启动之前,它可以正常工作 当服务器重新启动时,它会为相同的许可证代码生成不同的哈希字符串。有办法解决这个问题吗?如何在每次服务器重新启动时停止更改相同许可证代码的哈希模式?问题在于salt和您使用bcrypt的方式。 您正在使用salthards生成新的salt(一

我有一个表,其中有两列:许可证&已激活

我存储的许可证代码不是纯文本,而是散列的。我正在使用bcrypt模块生成salt第10轮的哈希。在数据库中搜索许可证时出现问题。要搜索许可证,我首先生成许可证的哈希,然后搜索数据库。在服务器重新启动之前,它可以正常工作


当服务器重新启动时,它会为相同的许可证代码生成不同的哈希字符串。有办法解决这个问题吗?如何在每次服务器重新启动时停止更改相同许可证代码的哈希模式?

问题在于salt和您使用bcrypt的方式。 您正在使用salthards生成新的salt(一个随机值),因此哈希值总是不同的。 如果使用固定的salt值,则散列将是相同的。请参见下面的示例:

        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:$2b$10$X4kv7j5ZcG39WgogSl16au
        //hash:$2b$10$X4kv7j5ZcG39WgogSl16aupL0..j8Fmm8Lwgq92uWuM5KyXhE6tpO

        //Generate the same hash value as fixed salt value is used
        salt = '$2b$10$X4kv7j5ZcG39WgogSl16au'
        bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
            console.log('same value:%s', hash); //hash:$2b$10$X4kv7j5ZcG39WgogSl16aupL0..j8Fmm8Lwgq92uWuM5KyXhE6tpO
        });

        // Test comparison
        hash='$2b$10$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');
            }
        });

也许可以使用其他一些值作为主键(许可证号码),并使用一些加密值来指示它是否是有效的许可证。

为什么不将盐与哈希一起存储?