title: NodeJS pbkdf2Sync password hasing portability from v0.12.7 to v6.9.1
body: <p>I have been working on porting an application written from node v0.12.7 to node v6.9.1. </p>
  
  <p>We are using MEAN stack with all of them upgraded to their latest versions.</p>
  
  <p>We have been able to upgrade everything except for one problem. We used pbkdf2Sync method (comes inbuilt with express) for hashing passwords like so:</p>
  
  <pre><code>/**
  * Hook a pre save method to hash the password
  */
  UserSchema.pre('save', function(next) {
      if (this.password &amp;&amp; this.password.length &gt; 6) {
         this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
         this.password = this.hashPassword(this.password);
      }
  
      next();
  });
  
  /**
  * Create instance method for hashing a password
  */
  UserSchema.methods.hashPassword = function(password) {
     if (this.salt &amp;&amp; password) {
        return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');
     } else {
      return password;
     }
  };
  </code></pre>
  
  <p>In the latest versions, they have changed the encoding to utf8 and also changed the pbkdf2Sync to include a mandatory digest. I am not sure what else they have changed.</p>
  
  <p><strong>The problem:</strong></p>
  
  <p>The passwords hashed and stored in the mongo database using earlier version of node does not match with the password generated by hashPassword function after version upgrade.</p>
  
  <p><strong>I have tried:</strong></p>
  
  <p>1) Specifying encoding string</p>
  
  <p>2) Using buffers</p>
  
  <p>3) Adding digest option as parameter</p>
  
  <p>And I dont get the same hashed password with any of these.</p>
  
  <p>I tried changing the hashPassword function with many combinations. One of the tries I made was this, but does not work.</p>
  
  <pre><code>UserSchema.methods.hashPassword = function (password) {
    if (this.salt &amp;&amp; password) {
       return crypto.pbkdf2Sync(password, new Buffer(this.salt, 'base64').toString('binary'), 10000, 64, 'SHA1').toString('base64');
    } else {
      return password;
   }
  };
  </code></pre>
  
  <p><strong>A test case:</strong></p>
  
  <p>Password to hash: ramco@123</p>
  
  <p>Salt: d\u001e'��\u0001\u0004\u0012)aq�**G\u000f</p>
  
  <p>Result I am supposed to get: kG6uCjSk87I7PrXMko+nS8Mz/78LMilXDMJZI0mzBgi75mBpi8hIkh3+B8CqpuYZdvvs5HWjcNthhhnUA89sCw==</p>
  
  <p>But I get some other string from the hashPassword function.</p>
  
  <p><strong>I referred:</strong></p>
  
  <p>The NodeJS commit made in git: 
  <a href="https://github.com/nodejs/node/commit/b010c8716498dca398e61c388859fea92296feb3" rel="nofollow noreferrer">https://github.com/nodejs/node/commit/b010c8716498dca398e61c388859fea92296feb3</a></p>
  
  <p>The express commit made in git:
  <a href="https://github.com/meanjs/mean/commit/61f1a22c91ac15f06143ace6e540b334fa9e3bd6" rel="nofollow noreferrer">https://github.com/meanjs/mean/commit/61f1a22c91ac15f06143ace6e540b334fa9e3bd6</a></p>
  
  <p>Crypto documentation:
  <a href="https://nodejs.org/api/crypto.html" rel="nofollow noreferrer">https://nodejs.org/api/crypto.html</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19337693/how-to-store-crypto-pbkdf2-in-mongodb">How to store crypto pbkdf2 in mongoDB?</a></p>
  
  <p>And lot of other sites and forums but did not help me. Kindly help me if you can.</p>
  
  <p>Thanks in advance.</p>