-
-
Save jeffz2012/09637a3d173463f2cf35 to your computer and use it in GitHub Desktop.
Revisions
-
soplakanets revised this gist
May 19, 2011 . No changes.There are no files selected for viewing
-
soplakanets created this gist
May 19, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ var crypto = require('crypto'); var SaltLength = 9; function createHash(password) { var salt = generateSalt(SaltLength); var hash = md5(password + salt); return salt + hash; } function validateHash(hash, password) { var salt = hash.substr(0, SaltLength); var validHash = salt + md5(password + salt); return hash === validHash; } function generateSalt(len) { var set = '0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ', setLen = set.length, salt = ''; for (var i = 0; i < len; i++) { var p = Math.floor(Math.random() * setLen); salt += set[p]; } return salt; } function md5(string) { return crypto.createHash('md5').update(string).digest('hex'); } module.exports = { 'hash': createHash, 'validate': validateHash };