一、關(guān)于node
加密模塊crypto
的介紹
其實就是使用MD5加密的,不太安全,在實際開發(fā)中根據(jù)自己的方案進(jìn)行加鹽處理
二、在路由視圖中使用加密方式
1、導(dǎo)入node自帶的加密模塊(不需要安裝)
//導(dǎo)入加密模塊 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("注冊成功"); } }) });
三、用戶登錄進(jìn)行密碼校驗
1、把用戶輸入的密碼用同樣的方式加密處理
2、把加密后的密碼與數(shù)據(jù)庫中匹配
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("發(fā)生錯誤"); } 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>
四、擴(kuò)展(一般我們加密處理)
1、利用隨機(jī)數(shù)隨機(jī)生成多少位數(shù)
2、利用可逆加密把第一步的生成的隨機(jī)數(shù)加密
可逆加密有Base64
和Hex
加密(具體自己百度)
3、將第二步加密好的隨機(jī)數(shù)與我們真實密碼拼接在一起
4、將第三步進(jìn)行加密(MD5
)
5、將第四步進(jìn)行可逆加密
6、將第二步與第五步生成的拼接成密碼
五、擴(kuò)展(一般我們加密的登錄)
1、登錄時候獲取密碼
2、從獲取的密碼中截取隨機(jī)數(shù)加密的那段
3、重復(fù)操作上面加密的方式(3,4,5,6)
相信看了本文案例你已經(jīng)掌握了方法,更多精彩請關(guān)注Gxl網(wǎng)其它相關(guān)文章!
推薦閱讀:
vue處理storejs獲取的數(shù)據(jù)
用p5.js制作煙花特效的示例代碼_javascript技巧
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com