簡體   English   中英

Bcrypt不一致地比較Node.js中的密碼

[英]Bcrypt not consistently comparing passwords in Node.js

我現在正在使用bcrypt在兩個不同的程序中比較相同的plainTextPassword和哈希,一個告訴我密碼匹配,另一個告訴我密碼不匹配。

在問題文件中:

passport.use(new LocalStrategy(function(username,plainTextPassword,done) {
  process.nextTick(() => {
    db.query('SELECT * FROM users WHERE username = ?',[username],(err,rows) => {
      if (err) return done(err);
      if (!rows[0]) return done(null,false);

      let hash = rows[0].password;
      let user = rows[0];
      bcrypt.compare(plainTextPassword, hash, (err, res) => {
        console.log(plainTextPassword);      // test
        console.log(hash);                   // $2b$10$EefqCwYTMHDDtIGH.SIHIu.BPCOiT7Bp4Zzej5iUoxlKtMZ67oHtC
        console.log(res);                    // false
        if (!res) return done(null,false);
        return done(null,rows[0]);
      });
    });
  });
}));

我正在復制平原文本密碼和哈希的記錄值,並將它們粘貼到以下代碼中:

const bcrypt = require('bcrypt');
const saltRounds = 10;
bcrypt.compare('test', '$2b$10$EefqCwYTMHDDtIGH.SIHIu.BPCOiT7Bp4Zzej5iUoxlKtMZ67oHtC', (err,same) => {
  console.log(same);    // true
}

並且返回的是true,而不是false,就像第一個代碼塊的console.log(res)返回一樣。 第一個代碼塊一直在檢查大多數密碼,但是這似乎有所不同。 沒有特殊字符,plainTextPassword為'test',哈希存儲在mysql數據庫的VARCHAR(150)中。

請參閱[]( https://en.wikipedia.org/wiki/Bcrypt :“ cost參數將密鑰擴展迭代計數指定為2的冪,這是crypt算法的輸入”

$ 10 $指定成本參數10,表示2 ^ 10個密鑰擴展回合。

當然,代碼行: const saltRounds = 10; 未使用,請刪除無效代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM