标签:enc 生成 频率 速度慢 script encrypt error 升级 user
密码的作用---证明是你
密码的存储
密码的传输
密码的替代方案
生物特征密码的问题
加密次数越多,复杂度越大,就越可以对抗彩虹表
传说以前出现密码明文存储被泄露的案例(无法确定)
加密
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("密码不正确");
}
标签:enc 生成 频率 速度慢 script encrypt error 升级 user
原文地址:https://www.cnblogs.com/ygjzs/p/12245919.html