nodejs中的bcryptjs密碼加密

bcryptjs密碼加密

bcryptjs是一個第三方密碼加密庫,是對原有bcrypt的優化,優勢是不須要安裝任何依賴npm

npmjs地址promise

引入bcryptjs庫

npm install bcryptjs
var bcrypt = require('bcryptjs');
...

同步用法(Sync)

生成hash密碼異步

var bcrypt = require('bcryptjs');
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync("B4c0/\/", salt);
// Store hash in your password DB.

密碼驗證優化

// Load hash from your password DB. 
bcrypt.compareSync("B4c0/\/", hash); // true 
bcrypt.compareSync("not_bacon", hash); // false

快速生成hash值ui

var hash = bcrypt.hashSync('bacon', 8);

異步用法(Async)

生成hash密碼加密

var bcrypt = require('bcryptjs');
bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash("B4c0/\/", salt, function(err, hash) {
        // Store hash in your password DB. 
    });
});

密碼驗證code

// Load hash from your password DB. 
bcrypt.compare("B4c0/\/", hash, function(err, res) {
    // res === true 
});
bcrypt.compare("not_bacon", hash, function(err, res) {
    // res === false 
});
 
// As of bcryptjs 2.4.0, compare returns a promise if callback is omitted: 
bcrypt.compare("B4c0/\/", hash).then((res) => {
    // res === true 
});

快速生成hash值get

bcrypt.hash('bacon', 8, function(err, hash) {
});
相關文章
相關標籤/搜索