如何解密Node.js中的加密密码

Dar*_*mid 2 javascript-events mongoose mongodb node.js express

我想为用户创建一个更改密码页面.我在用户数据库(mongodb)中保存用户时加密了密码.

User.virtual('password')
    .set(function(password) {
      this._password = password;
      this.salt = this.makeSalt();
      this.hashed_password = this.encryptPassword(password);
    })
    .get(function() { return this._password; });

  User.method('authenticate', function(plainText) {
    return this.encryptPassword(plainText) === this.hashed_password;
  });

  User.method('makeSalt', function() {
    return Math.round((new Date().valueOf() * Math.random())) + '';
  });

  User.method('encryptPassword', function(password) {
    return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
  });
Run Code Online (Sandbox Code Playgroud)

我不知道如何解密它以获取原始密码.任何帮助将不胜感激 .

Mik*_*ott 17

密码是经过哈希处理的,而不是加密的,并且您无法获得原始密码 - 这是散列的全部要点,它是单向函数.你不应该需要原件,因为你没有合法的用途.要验证用户,您可以使用与存储密码相同的方式对他们提供的密码进行哈希处理,然后比较哈希值.