Created
January 8, 2016 14:33
-
-
Save arleyhr/57e6a75c3516c6bd7147 to your computer and use it in GitHub Desktop.
Simple Password Encript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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