Skip to content

Instantly share code, notes, and snippets.

@azechi
Last active July 3, 2023 17:14
Show Gist options
  • Save azechi/01cb8908a41c132ad95f54bbb91ad5aa to your computer and use it in GitHub Desktop.
Save azechi/01cb8908a41c132ad95f54bbb91ad5aa to your computer and use it in GitHub Desktop.
Generate a random base32 string(length:6) using Web Crypto API
const base = "abcdefghijklmnopqrstuvwxyz234567" //a-z, 2-7
const array = new Int32Array(1)
window.crypto.getRandomValues(array)
const i = array[0]
var result =
base[i >>> 27 & 0x1f] +
base[i >>> 22 & 0x1f] +
base[i >>> 17 & 0x1f] +
base[i >>> 12 & 0x1f] +
base[i >>> 7 & 0x1f] +
base[i >>> 2 & 0x1f]
console.log(result)
@Senhua-Liu
Copy link

Very good my friend thank you

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