如何使用JavaScript的bcrypt庫

了解如何使用bcrypt庫在JavaScript中加密和驗證密碼 bcrypt是JavaScript中最常用的用於處理密碼的npm包之一。 這是基本的安全知識,但對於新手開發人員來說值得一提:您永遠不應該以明文形式在數據庫或其他地方存儲密碼。絕對不行。 相反,您需要從密碼中生成一個哈希值,然後將其存儲。 使用方法如下: import bcrypt from 'bcrypt' // 或 // const bcrypt = require('bcrypt') const password = 'oe3im3io2r3o2' const rounds = 10 bcrypt.hash(password, rounds, (err, hash) => { if (err) { console.error(err) return } console.log(hash) }) 您需要將一個數字作為第二個參數傳遞,該數字越大,哈希值就越安全,但生成的時間也越長。 該庫的README文件告訴我們,在2GHz核心上,我們可以生成: rounds=8 : ~40 hashes/sec rounds=9 : ~20 hashes/sec rounds=10: ~10 hashes/sec rounds=11: ~5 hashes/sec rounds=12: 2-3 hashes/sec rounds=13: ~1 sec/hash rounds=14: ~1.5 sec/hash rounds=15: ~3 sec/hash rounds=25: ~1 hour/hash rounds=31: 2-3 days/hash 如果您多次運行bcrypt....