node中的加密模块 crypto


node中的加密模块 crypto

crypto 加密模块(不是很安全):是使用md5来加密,这是node自带的模块,不需要安装。

  • 引入模块:

    const crypto = require(‘crypto’);

  • 当用户注册时,我们将从前端页面映射到后台的密码进行加密处理,关键语句:

    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("注册成功");
            }
        })
    });
    
  • 用户登录时也要进行密码校验,也需要对用户输入的密码进行加密,然后和数据库中加密的密码进行匹配。

    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("用户名或密码错误");
                }
            }
        })
    })
    

    let newPas = md5.update(password).digest(“hex”);语句中的hex是指可逆的加密,这样的加密还有base64.

总结

加解密技术专业性很强,需要花费时间,深入研究。node的crypto模块能进行可逆的简单加密,但是不安全,我也不是很懂加密,但是我们在做小型的项目和demo时,还是可以用到。要想了解更多的加密技术,可以去找相关资料研究下。这里推荐相关node中加密讲解的文章(我看了还比较好理解的文章)http://cnodejs.org/topic/56e22b279386fbf86ddd69ce
https://www.cnblogs.com/laogai/p/4664917.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值