Last active
January 4, 2024 18:12
-
-
Save tindzk/bc2cbacf3aa96d979ac5a0cf65b4a05a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { randomFillSync } from "node:crypto"; | |
// Defined for length >= 2 | |
// | |
// From https://stackoverflow.com/a/27747377/13300239 | |
export function randomStr(length: number): string { | |
const rnd = new Uint8Array(length / 2); | |
randomFillSync(rnd); | |
// i.e. 0-255 -> '00'-'ff' | |
const dec2hex = (_: number) => _.toString(16).padStart(2, "0"); | |
return Array.from(rnd, dec2hex).join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment