Skip to content

Instantly share code, notes, and snippets.

@kosinix
Created June 29, 2020 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosinix/83f31ced9ec289b5855c51af09ba2495 to your computer and use it in GitHub Desktop.
Save kosinix/83f31ced9ec289b5855c51af09ba2495 to your computer and use it in GitHub Desktop.
/**
* Generates random string and password hashing
* @type {module:crypto}
*/
//// Core modules
const crypto = require('crypto');
const util = require('util');
//// External modules
//// Modules
let randomBytesAsync = util.promisify(crypto.randomBytes);
module.exports = {
randomStringAsync: async (length = 32) => {
let bytes = await randomBytesAsync(length / 2);
return bytes.toString('hex');
},
randomString: (length = 32) => {
let bytes = crypto.randomBytes(length / 2);
return bytes.toString('hex');
},
hashPassword: (password, salt) => {
return crypto.pbkdf2Sync(password, salt, 10000, 64, 'sha512').toString('hex');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment