bcrypt
哈希加密为单程加密方式(1234=>abcd),加密的密码中加入随机字符串可增加密码被破解的难度;bcrypt依赖其他环境1.python 2.x;
2.node-gyp (npm install node-gyp -g)
3.windows-build-tools(npm install --global --production windows-build-tools)(Windows环境安装)
环境配置好后开始撸它写测试代码观察使用情况
1.代码块: //引入bcrypt(测试密码加密)const bcrypt = require('bcrypt');//定义异步函数async function run() { //cwen生成随机字符串 //genSalt方法接收一个数值作为参数 //数值越大,生成随机字符串复杂程度越高 //数值越小,生成随机字符串复杂程度越低 //默认值是10 //返回生成的随机字符串 const salt = await bcrypt.genSalt(10); //对密码进行加密 //1要进行加密的明文,2随机字符串 //返回的值为加密后的密码 const result = await bcrypt.hash('123456', salt); console.log(salt); console.log(result);}run();
2.执行结果:
第一次输出为生成的字符串,第二次输出为字符串+123456(暗文)
(1)代码块 //!创建传入加密密码的方法async function createUser() { const salt = await bcrypt.genSalt(10); const pass = await bcrypt.hash('123456', salt); const user = await User.create({ username: 'cwen', email: 'cwen@it.com', password: pass, role: 'admin', state: 0 });}//调用方法createUser();
(2)执行结果
密码从明文变为暗文
比对成功即可接着撸代码啦~~
OVER