• 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

  • 相关阅读:
    [BJWC2010]外星联络
    [NOI2015]品酒大会
    工艺 /【模板】最小表示法
    [NOI2016]优秀的拆分
    [HEOI2016/TJOI2016]字符串
    [SDOI2016]生成魔咒
    【模板】后缀自动机 (SAM)【SA解法】
    [湖南集训]图森
    [USACO17DEC]Standing Out from the Herd P
    Annihilate
  • 原文地址:https://www.cnblogs.com/yehui-mmd/p/7806453.html
Copyright © 2020-2023  润新知