I'm trying to generate some tokens. They have to be 26 characters along from the alphabet [a-z0-9]
.
The closest solution I've found is part 2 from this answer, but the string won't be uniformly distributed.
If my alphabet was a power of 2 in length, this wouldn't be so hard, but as it stands, I'm not sure how to do this properly.
Specifically, this is what I've got so far:
export function createSessionId() {
const len = 26;
let bytes = new Crypto.randomBytes(len);
let result = new Array(len);
const alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
for(let i=0; i<len; ++i) {
result[i] = alphabet[bytes[i]%alphabet.length];
}
return result.join('');
}
But I'm pretty sure that won't be distributed properly because of the modulo.