Skip to content

Instantly share code, notes, and snippets.

@rkleine
Last active August 13, 2019 11:59
Show Gist options
  • Save rkleine/eeeb43248daacedf45e85b8a3e90ac10 to your computer and use it in GitHub Desktop.
Save rkleine/eeeb43248daacedf45e85b8a3e90ac10 to your computer and use it in GitHub Desktop.
CouchDB 2.x NodeJS password generator
const crypto = require('crypto')
function generatePassword(password, options = {}) {
const { iterations = 1000, keylen = 20, digest = 'sha1', saltSize = 24 } = options
const salt = crypto.randomBytes(saltSize).toString('hex')
const key = crypto.pbkdf2Sync(password, salt, iterations, keylen, digest).toString('hex')
return {
iterations,
salt,
key
}
}
console.log(generatePassword('my password'))
/* output
{
iterations: 1000,
salt: '87c61b8c12614abd37038769b1caa91f11f22efeea17a238',
key: '4251927518165a19f9aa6e459d7665e5fa7ef559'
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment