密碼的做用---證實是你
密碼的存儲
密碼的傳輸
密碼的替代方案
生物特徵密碼的問題javascript
加密次數越多,複雜度越大,就越能夠對抗彩虹表前端
傳說之前出現密碼明文存儲被泄露的案例(沒法肯定)java
加密node
var password = {}; var md5 = function(str) { var crypto = require('crypto'); var md5Hash = crypto.createHash('md5'); md5Hash.update(str); return md5Hash.digest('hex'); }; password.getSalt = function() { return md5(Math.random()*99999+''+new Data().getTime()); } password.encryptoPassword = function(salt, password) { return md5(salt + 'af@#$%^asda&' + password); } module.exports = password;
密碼驗證算法
// 若是用戶沒有salt,須要升級 if (!user.salt) { var salt = password.getSalt(); var newPassword = password.encryptPassword(salt, user.password); await query( `update user set password = '$(newPassword)', salt = '${salt} where id = ${user.id}'` ); user.salt = salt; user.password = newPassword; } var encryptPassword = password.encryptPassword(user.salt, data.password); if (encryptPassword !== user.password) { throw new Error("密碼不正確"); }
在前端加密須要安裝js-md5
模塊,運行jspm install node:js-md5
數據庫
前端進行加密,這是先後端要有相同的鹽 SUGAR後端
var SUGAR = "!@#¥@#@¥#¥%GDF456¥%"; daat.password = md5(data.username + SUGAR + data.password);
var password = {}; var md5 = function(str) { var crypto = require('crypto'); var md5Hash = crypto.createHash('md5'); md5Hash.update(str); return md5Hash.digest('hex'); }; password.getPasswordFromText = function(username, password) { var SUGAR = '!@#¥@#@¥#¥%GDF456¥%';//與前端一致 return md5(username+ SUGAR + password); }; password.getSalt = function() { return md5(Math.random()*99999+''+new Data().getTime()); } password.encryptoPassword = function(salt, password) { return md5(salt + 'af@#$%^asda&' + password); } module.exports = password;
if (!user.salt) { var salt = password.getSalt(); var newPassword = password.getPasswordFromText(user.username, user.password); var encryptedPassword = password.encryptPassword(salt, newPassword); await query( `update user set password = '$(newPassword)', salt = '${salt} where id = ${user.id}'` ); user.salt = salt; user.password = encryptedPassword; } var encryptPassword = password.encryptPassword(user.salt, data.password); if (encryptPassword !== user.password) { throw new Error("密碼不正確"); }