Skip to content

Instantly share code, notes, and snippets.

@ngault
Created July 3, 2016 19:05
Show Gist options
  • Save ngault/b1189d20e9808346402c2d1ffcf18709 to your computer and use it in GitHub Desktop.
Save ngault/b1189d20e9808346402c2d1ffcf18709 to your computer and use it in GitHub Desktop.
password salting
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