簡體   English   中英

如何使用加密檢查字符串是否已在 Node.js 中進行哈希處理

[英]How to check if a string is already hashed in Node.js using crypto

我正在開發一個允許用戶更改密碼的應用程序。

我正在使用 Node.js 和mongoosecrypto

為了生成密碼的哈希值,我已經連接到模型的 pre('save') 事件。 所以基本上當你使用它時:

var user = new User({password: '123'});
user.save(); // the password will be encrypted automatically

具有更改密碼的能力,我需要找到一種方法,如果用戶實際上已經更改了密碼或修改了任何其他字段。 例子:

var user = User.findById('1234567', function(error, user){
     user.name = 'Hello';
     user.save(); // this will hash the password again, although it is not necessary
})

我正在使用名為crypto的 Node.js 模塊生成密碼 hash:

crypto.pbkdf2(password, salt, 5000, 512, 'sha512', function(error, buffer){

        this.mixins.callback.call(callback, [error, buffer.toString('base64')]);

}.bind(this));

有沒有辦法檢查password是否已經過哈希處理?
如果沒有,我將再次生成 hash。

謝謝

這本質上是不好的方法。 只需調用字段password_hash並始終顯式傳遞哈希即可。 否則,在進行一些小的配置更改后,您可能會意外地開始存儲未加密的密碼。

如果允許使用任意字符串作為密碼,通常也無法將哈希與未哈希的字符串區分開。

散列字符串具有 hash 算法標識符前綴。 您可以通過檢查字符串是否以$2a$$2b$開頭來驗證

參考

暫無
暫無

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

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