Skip to content

Instantly share code, notes, and snippets.

@ccondry
Created February 20, 2021 14:43
Show Gist options
  • Save ccondry/7f362acb66e4a73f4e25ce8689e72bb5 to your computer and use it in GitHub Desktop.
Save ccondry/7f362acb66e4a73f4e25ce8689e72bb5 to your computer and use it in GitHub Desktop.
create a short alphanumeric hash using shake128
const crypto = require('crypto')
// create hash of user sub, and append it to the first 8 characters of the user sub
function getHash (sub) {
const hash = crypto
.createHash('shake128', {outputLength: 6})
.update(sub, 'utf-8')
.digest('base64')
// you could remove this replace statement if you didn't want to alter the base64 or number of output characters
// this is here so that only alphanumerics are returned. but it does change the length of some hashes (consistently)
.replace('+', '')
return sub.split('@').shift().slice(0, 8) + hash
}
module.exports = getHash
@ccondry
Copy link
Author

ccondry commented Feb 20, 2021

oh, it's for Node.js of course

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment