這篇文章將為大家詳細講解有關(guān)nodejs中怎么實現(xiàn)密碼加密處理操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

一、關(guān)于node加密模塊crypto的介紹
其實就是使用MD5加密的,不太安全,在實際開發(fā)中根據(jù)自己的方案進行加鹽處理
二、在路由視圖中使用加密方式
1、導(dǎo)入node自帶的加密模塊(不需要安裝)
//導(dǎo)入加密模塊
const crypto = require("crypto");2、做一個用戶注冊,密碼加密的視圖
<div class="col-md-6"> <h5>用戶注冊</h5> <form role="form" method="post" action="/regest"> <div class="form-group"> <label for="username">用戶名:</label> <input id="username" type="text" placeholder="請輸入用戶名" name="username" class="form-control"/> </div> <div class="form-group"> <label for="password">密碼:</label> <input id="password" type="password" placeholder="請輸入密碼" name="password" class="form-control"/> </div> <div class="form-group"> <input type="submit" value="提交" class="btn btn-success"/> </div> </form> </div>
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、把加密后的密碼與數(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("用戶名或密碼錯誤");
}
}
})
})<div class="col-md-6"> <h5>用戶登錄</h5> <form role="form" method="post" action="/login"> <div class="form-group"> <label for="username2">用戶名:</label> <input id="username2" type="text" placeholder="請輸入用戶名" name="username" class="form-control"/> </div> <div class="form-group"> <label for="password">密碼:</label> <input id="password" type="password" placeholder="請輸入密碼" name="password" class="form-control"/> </div> <div class="form-group"> <input type="submit" value="提交" class="btn btn-success" id="sub-btn2"/> </div> </form> </div>
四、擴展(一般我們加密處理)
1、利用隨機數(shù)隨機生成多少位數(shù)
2、利用可逆加密把第一步的生成的隨機數(shù)加密
可逆加密有Base64和Hex加密(具體自己百度)
3、將第二步加密好的隨機數(shù)與我們真實密碼拼接在一起
4、將第三步進行加密(MD5)
5、將第四步進行可逆加密
6、將第二步與第五步生成的拼接成密碼
五、擴展(一般我們加密的登錄)
1、登錄時候獲取密碼
2、從獲取的密碼中截取隨機數(shù)加密的那段
3、重復(fù)操作上面加密的方式(3,4,5,6)
關(guān)于nodejs中怎么實現(xiàn)密碼加密處理操作就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁標題:nodejs中怎么實現(xiàn)密碼加密處理操作-創(chuàng)新互聯(lián)
標題來源:http://chinadenli.net/article20/diioco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、做網(wǎng)站、全網(wǎng)營銷推廣、網(wǎng)站收錄、建站公司、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容