如何在登录后每次发布帖子请求时随机生成sha1?目前我只是req.body.username
向我传递了用户名,并像sha1(req.body.username)
一样对其进行散列,但是生成加密字符串,但是它总是相同的,因为它基于用户名。
是否可以单独根据用户名生成随机哈希?
我想,除非我做错了,否则它每次都会给我相同的哈希。
User.find(req.body, function (err, results){
if (err) throw err;
if (results && results.length === 1){
for (var i = 0; i < results.length; i++) {
var element = results[i];
console.log(element);
console.log('new key per request', sha1(element));
}
res.json(sha1(req.body.username));
}
})
编辑:我正在哈希它,所以我可以将它传递给localStorage作为会话密钥,它可能不是最好的和安全的方式,但不知道我如何在mongoDB中生成sessionKey。
使用sha1和crypto,我将sha1和crypto结合起来生成随机密钥,尽管只有密码正在改变。 sha1部分只是加密的用户名,然后添加32byte的crypto.toString('hex')
。我知道它不是创建localStorage值标识符的最佳方法。
var buf = crypto.randomBytes(32);
hexKey = buf.toString('hex');
res.json(sha1(req.body.username) + hexKey);