本文实例讲述了nodejs中密码加密处理操作。分享给大家供大家参考,具体如下:
一、关于node加密模块crypto的介绍
其实就是使用md5加密的,不太安全,在实际开发中根据自己的方案进行加盐处理
二、在路由视图中使用加密方式
1、导入node自带的加密模块(不需要安装)
//导入加密模块 const crypto = require(crypto);
2、做一个用户注册,密码加密的视图
<p class="col-md-6"> <h4>用户注册</h4> <form role="form" method="post" action="/regest"> <p class="form-group"> <label for="username">用户名:</label> <input id="username" type="text" placeholder="请输入用户名" name="username" class="form-control"/> </p> <p class="form-group"> <label for="password">密码:</label> <input id="password" type="password" placeholder="请输入密码" name="password" class="form-control"/> </p> <p class="form-group"> <input type="submit" value="提交" class="btn btn-success"/> </p> </form> </p>
router.post(/regest,(req,res)=>{ console.log(req.body); let name = req.body.username; let password = req.body.password; let md5 = crypto.createhash(md5); let newpas = md5.update(password).digest(hex); db(insert into user1(name,password) values(?,?),[name,newpas],(err,data)=>{ if (err){ res.send(注册失败); } console.log(data); if (data){ res.send(注册成功); } }) });
三、用户登录进行密码校验
1、把用户输入的密码用同样的方式加密处理
2、把加密后的密码与数据库中匹配
router.post(/login,(req,res)=>{ let name = req.body.username; let password = req.body.password; let md5 = crypto.createhash(md5); let newpas = md5.update(password).digest(hex); db(select * from user1 where name = ?,[name],(err,data)=>{ console.log(data[0].password); if (err){ res.send(发生错误); } if (data){ if (data[0].password === newpas){ res.send(登录成功); }else { res.send(用户名或密码错误); } } }) })
<p class="col-md-6"> <h4>用户登录</h4> <form role="form" method="post" action="/login"> <p class="form-group"> <label for="username2">用户名:</label> <input id="username2" type="text" placeholder="请输入用户名" name="username" class="form-control"/> </p> <p class="form-group"> <label for="password">密码:</label> <input id="password" type="password" placeholder="请输入密码" name="password" class="form-control"/> </p> <p class="form-group"> <input type="submit" value="提交" class="btn btn-success" id="sub-btn2"/> </p> </form> </p>
四、扩展(一般我们加密处理)
1、利用随机数随机生成多少位数
2、利用可逆加密把第一步的生成的随机数加密
可逆加密有base64和hex加密(具体自己百度)
3、将第二步加密好的随机数与我们真实密码拼接在一起
4、将第三步进行加密(md5)
5、将第四步进行可逆加密
6、将第二步与第五步生成的拼接成密码
五、扩展(一般我们加密的登录)
1、登录时候获取密码
2、从获取的密码中截取随机数加密的那段
3、重复操作上面加密的方式(3,4,5,6)
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue.js+flask来构建单页的app(附代码)
需要遍历不规则多维数组时应怎么写js
以上就是nodejs对密码加密处理方法总结的详细内容。