加密 防撞库

1
> npm install --save bcryptjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const bcrypt = require('bcryptjs')
var password = '123abc!';

//加密, salt password 是为了防止撞库攻击、密码概率攻击
bcrypt.genSalt(10, (err, salt) => {
 bcrypt.hash(password, salt, (err, hash) => {
   console.log(hash);
 });
});

//解密
var hashedPassword = '$2a$10$huAU4qTnQuGPifHEXfV9cOmPJ7p61oKaoXrY1WviiDAznE/rW8oLK';

bcrypt.compare('123abc!', hashedPassword, (err, res) => {
 console.log(res);
});

nodejs渐入佳境[26]-bcryptjs库实现密码哈希_前端