Skip to content

Instantly share code, notes, and snippets.

@nakul5harma
Created March 20, 2022 14:42
Show Gist options
  • Save nakul5harma/377c512eba4935a027af09e034a36ae0 to your computer and use it in GitHub Desktop.
Save nakul5harma/377c512eba4935a027af09e034a36ae0 to your computer and use it in GitHub Desktop.
Generate cryptographically secure OTP in TypeScript/ JavaScript/ NodeJS
import { randomBytes } from "crypto";
export const generateCryptographicallySecureOTP = (otpLength: number) => {
return parseInt(randomBytes(otpLength).toString("hex"), 16)
.toString()
.substr(0, otpLength);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment