nodejs中密碼加密處理操作詳解,nodejs詳解

來源:互聯網
上載者:User

nodejs中密碼加密處理操作詳解,nodejs詳解

本文執行個體講述了nodejs中密碼加密處理操作。分享給大家供大家參考,具體如下:

一、關於node加密模組crypto的介紹

其實就是使用MD5加密的,不太安全,在實際開發中根據自己的方案進行加鹽處理

二、在路由視圖中使用加密方式

1、匯入node內建的加密模組(不需要安裝)

//匯入加密模組const crypto = require("crypto");

2、做一個使用者註冊,密碼加密的視圖

<div class="col-md-6">  <h4>使用者註冊</h4>  <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、把加密後的密碼與資料庫中匹配

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("使用者名稱或密碼錯誤");      }    }  })})
<div class="col-md-6">  <h4>使用者登入</h4>  <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、利用隨機數隨機產生多少位元
2、利用可逆加密把第一步的產生的隨機數加密
可逆加密有Base64Hex加密(具體自己百度)
3、將第二步加密好的隨機數與我們真實密碼拼接在一起
4、將第三步進行加密(MD5)
5、將第四步進行可逆加密
6、將第二步與第五步產生的拼接成密碼

五、擴充(一般我們加密的登入)

1、登入時候擷取密碼
2、從擷取的密碼中截取隨機數加密的那段
3、重複操作上面加密的方式(3,4,5,6)

六、關於正常項目中開發加密的方式代碼正確的加密方式

PS:關於加密解密感興趣的朋友還可以參考本站線上工具:

BASE64編碼解碼工具:
http://tools.jb51.net/transcoding/base64

MD5線上加密工具:
http://tools.jb51.net/password/CreateMD5Password

文字線上加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode

線上散列/雜湊演算法加密工具:
http://tools.jb51.net/password/hash_encrypt

線上MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

線上sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

希望本文所述對大家nodejs程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.