title: SALT and HASH password in nodejs w/ crypto
body: <p>I am trying to figure out how to salt and hash a password in nodejs using the crypto module.  I am able to create the hashed password doing this:</p>
  
  <pre><code>UserSchema.pre('save', function(next) {
    var user = this;
  
    var salt = crypto.randomBytes(128).toString('base64');
    crypto.pbkdf2(user.password, salt, 10000, 512, function(err, derivedKey) {
      user.password = derivedKey;
      next();
    });
  });
  </code></pre>
  
  <p>However I am confused about how to later validate the password.</p>
  
  <pre><code>UserSchema.methods.validPassword = function(password) {    
    // need to salt and hash this password I think to compare
    // how to I get the salt?
  }
  </code></pre>