Skip to content

Instantly share code, notes, and snippets.

@thameera
Last active March 24, 2021 08:50
Show Gist options
  • Save thameera/60d34481cb797941568bb1b2fb220a8b to your computer and use it in GitHub Desktop.
Save thameera/60d34481cb797941568bb1b2fb220a8b to your computer and use it in GitHub Desktop.
/*
* Converts a PBKDF2 hash generated by PBKDF2PasswordEncryptor to PHC format
*/
const digest = 'sha1'
const convert = origHash => {
const buf = Buffer.from(origHash, 'base64')
const keylenBits = parseInt(buf.slice(0, 4).toString('hex'), 16)
const keylen = keylenBits / 8
const iterations = parseInt(buf.slice(4, 8).toString('hex'), 16)
const salt = buf.slice(8, 16).toString('base64').replace(/=/g, '')
const hash = buf.slice(16).toString('base64').replace(/=/g, '')
return `$pbkdf2-${digest}$i=${iterations},l=${keylen}$${salt}$${hash}`
}
const h = 'AAAAoAAB9ABzWnS2vspWtgNxyaXRnXX4Kg6IwSd2A+3V8WuZ' // plaintext value = 'changeme'
const converted = convert(h)
console.log(converted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment