Skip to content

Instantly share code, notes, and snippets.

@kendru
Created September 13, 2021 22:18
Show Gist options
  • Save kendru/a6f0b4becbcc8ca154ed5cca7749ce1a to your computer and use it in GitHub Desktop.
Save kendru/a6f0b4becbcc8ca154ed5cca7749ce1a to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
const charTbl = '0123456789abcdefghjkmnpqrstvwxyz';
const encodeChunk = (n) => charTbl[n];
const nthChunk = (input, idx) => {
const startBit = 5*idx;
const offset = startBit % 8;
const startByte = (startBit - offset)/8;
const mask = 0xff >> offset;
let n = input[startByte] & mask;
const postShift = 3-offset;
if (postShift < 0) {
n = n << -postShift;
let nextByte = 0;
if (input.length > startByte+1) {
nextByte = input[startByte+1];
}
const secondByteBits = nextByte >> (8+postShift);
n |= secondByteBits;
} else {
n = n >> postShift;
}
return n;
}
const encodeBase32 = (input) => {
let out = '';
const totalBytes = input.length * 8;
for (let i = 0; i*5 <= totalBytes; i++) {
out += encodeChunk(nthChunk(input, i))
}
return out;
};
const uniqueId = () => encodeBase32(crypto.randomBytes(16))
const main = async () => {
for (let index = 0; index < 100; index++) {
console.log(uniqueId());
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment