Skip to content

Instantly share code, notes, and snippets.

@tindzk
Last active January 4, 2024 18:12
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 tindzk/bc2cbacf3aa96d979ac5a0cf65b4a05a to your computer and use it in GitHub Desktop.
Save tindzk/bc2cbacf3aa96d979ac5a0cf65b4a05a to your computer and use it in GitHub Desktop.
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