Created
July 3, 2016 19:05
-
-
Save ngault/b1189d20e9808346402c2d1ffcf18709 to your computer and use it in GitHub Desktop.
password salting
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'); | |
// Generate a base64 nonce of length 'len' | |
function randomStrBase64(len) { | |
return crypto.randomBytes(Math.ceil(len * 3 / 4)) | |
.toString('base64') // convert to base64 format | |
.slice(0, len) // return required number of characters | |
.replace(/\+/g, '0') // replace '+' with '0' | |
.replace(/\//g, '0'); // replace '/' with '0' | |
} | |
function sha256(password) { | |
var hash = crypto.createHash('sha256').update(password).digest('base64'); | |
return hash; | |
} | |
// TODO: implement HMAC/salting, turning from 'encrypt' to 'hash' | |
function encryptPassword(password) { | |
return sha256(password); | |
} | |
console.log(encryptPassword("ngngng12")); | |
console.log(encryptPassword("ngngng14")); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment