Skip to content

Instantly share code, notes, and snippets.

@hachibu
Last active November 20, 2022 03:13
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 hachibu/089f59b13868b42af3ca030653b34ec0 to your computer and use it in GitHub Desktop.
Save hachibu/089f59b13868b42af3ca030653b34ec0 to your computer and use it in GitHub Desktop.
import { randomBytes, createHash } from 'node:crypto'
const ZERO_WIDTH_SPACE = '​'
const ZERO_WIDTH_JOINER = '‍'
const randomZeroWidthHash = () =>
randomBytes(32)
.reduce((a, b) => [...a, b.toString(2)], [])
.reduce((a, c) => a + (+c ? ZERO_WIDTH_SPACE : ZERO_WIDTH_JOINER), '')
.padEnd(256, ZERO_WIDTH_SPACE)
const createSha256Hash = (data) =>
createHash('sha256')
.update(data)
.digest('hex')
function main() {
const zwhash = randomZeroWidthHash()
const test = `Hello ${zwhash}World!`
console.log(createSha256Hash(zwhash))
console.log(createSha256Hash(test.slice(6, 6 + zwhash.length)))
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment