Skip to content

Instantly share code, notes, and snippets.

@arleyhr
Created January 8, 2016 14:33
Show Gist options
  • Save arleyhr/57e6a75c3516c6bd7147 to your computer and use it in GitHub Desktop.
Save arleyhr/57e6a75c3516c6bd7147 to your computer and use it in GitHub Desktop.
Simple Password Encript
var crypto = require('crypto');
var SaltLength = 9;
var set = '/.EyDNbL7R^eJvwm';
function createHash (password, cb){
var salt = generateSalt(SaltLength),
hash = md5(password + salt);
cb(salt + hash)
}
function validateHash (password, hash){
var salt = hash.substr(0, SaltLength),
validHash = salt + md5(password + salt);
return hash === validHash;
}
function md5 (string){
return crypto
.createHash('md5')
.update(string)
.digest('hex');
}
function generateSalt(len){
var setLen = set.length,
salt = '';
for (var i = 0; i < len; i++) {
var p = Math.floor(Math.random() * setLen)
salt += set[p]
};
return salt;
}
module.exports = {
'hash': createHash,
'validate': validateHash
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment