Last active
April 11, 2018 07:41
-
-
Save meetzaveri/1bb8d0efd0076e2df0392320ce2e3748 to your computer and use it in GitHub Desktop.
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 characters
const genRandomString = function(length){ | |
return crypto.randomBytes(Math.ceil(length/2)) | |
.toString('hex') /** convert to hexadecimal format */ | |
.slice(0,length); /** return required number of characters */ | |
}; | |
const sha512 = function(password, salt){ | |
var hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */ | |
hash.update(password); | |
var value = hash.digest('hex'); | |
return { | |
salt:salt, | |
passwordHash:value | |
}; | |
}; | |
exports.saltHashPassword = function (userpassword) { | |
var salt = genRandomString(20); /** Gives us salt of length 16 */ | |
var passwordData = sha512(userpassword, salt); | |
return passwordData; | |
console.log('UserPassword = '+userpassword); | |
console.log('Passwordhash = '+passwordData.passwordHash); | |
console.log('nSalt = '+passwordData.salt); | |
} | |
///////// | |
//////// | |
// Login | |
//SECRET KEY maybe anything | |
var jwt = require('jsonwebtoken'); | |
exports.verifyPassword = function(password, salt){ | |
var hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */ | |
hash.update(password); | |
var value = hash.digest('hex'); | |
return value; | |
}; | |
exports.generateToken = function(dataItem,SECRET_KEY){ | |
return jwt.sign({ email: dataItem.email }, SECRET_KEY); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment