Advertisement
framp

Hashing password module

Oct 18th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var crypto = require('crypto');
  2.  
  3. module.exports = {
  4.     algorithm: 'sha256',
  5.     repetitions: 65536,
  6.     generateHash: function(value){
  7.         var v = crypto.createHash(this.algorithm);
  8.         v.update(value);
  9.         return v.digest();
  10.     },
  11.     checkPassword: function(password, input){
  12.         return password === this.hashPassword(input, password.substr(0, password.length/2));
  13.     },
  14.     hashPassword: function(password, salt){
  15.         var repetitions = this.repetitions;
  16.         salt = salt || this.generateHash(crypto.randomBytes(16));
  17.         hash = this.generateHash (salt + password);
  18.         while (repetitions--)
  19.             hash = this.generateHash(hash + repetitions + salt + password);
  20.         return salt + hash;
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement