您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何操作nodejs对密码进行加密

2025/1/13 21:59:23发布32次查看
这次给大家带来如何操作nodejs对密码进行加密,操作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>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
js动态添加方法与php动态添加方法比较
p5.js做出礼花绽放动画
以上就是如何操作nodejs对密码进行加密的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product