Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Decrypt an encrypted Password in Node.js

How to Decrypt an encrypted Password in Node.js

Problem

I want to create a change Password page for user. I encrypt the password when i save the user in Database(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');
  });

I don't know how to Decrypt it in order to get the original password back. any help will be appreciated .

Problem courtesy of: Dar Hamid

Solution

The password is hashed, not encrypted, and you can't get the original back -- that's the whole point of hashing, it's a one-way function. You shouldn't ever need to get the original back, as you have no legitimate use for it. To validate a user, you hash the password that they give you in the same way as the stored one, then compare the hashes.

Solution courtesy of: Mike Scott

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to Decrypt an encrypted Password in Node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×