cryptography using node.js having issues
newbie question re: nodejs and cryto
var crypto = require('crypto');
var User = {
user1: { name: 'bob', salt: 'randomSalt', password: sha1('mypass', this.salt) }
};
function sha1(pass, salt) {
return crypto.createHmac('sha1', salt).update(p开发者_运维知识库ass).digest('hex');
}
Why do I have
console.log(User.user1.password == sha1('mypass', 'randomSalt') //false
?
Answering to myself:
"classic" javascript gotcha :
the context (this) has changed when sha1 is called from the object. Therefore, this.salt is "undefined"
This should work:
var crypto = require("crypto");
function sha1(pass, salt) {
return crypto.createHmac('sha1', salt).update(pass).digest('hex');
}
var User = { name:'Robin', salt:'mysalt'}
User.password = sha1('mypass', User.salt);
// 'cfbc41a870bb7ddd3d7fcc774dd6d2d5850d5340'
精彩评论