
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
rand-verification-code
Advanced tools
A lightweight, flexible JavaScript library for generating random verification codes with customizable character sets and security options.
npm install rand-verification-code
const { generateVerificationCode } = require('rand-verification-code');
// Generate a 6-character code with digits and uppercase letters (default)
const code = generateVerificationCode();
console.log(code); // Example: "A3K9P2"
// 8-digit numeric code
const numericCode = generateVerificationCode({
length: 8,
digits: true,
uppercase: false
});
console.log(numericCode); // Example: "48273956"
// Alphanumeric with lowercase
const mixedCode = generateVerificationCode({
length: 10,
digits: true,
lowercase: true,
uppercase: true
});
console.log(mixedCode); // Example: "Kj8mN3pQr2"
// Include symbols
const strongCode = generateVerificationCode({
length: 12,
digits: true,
uppercase: true,
lowercase: true,
symbols: true
});
console.log(strongCode); // Example: "aB3#xY9@mK!p"
// Custom character set
const customCode = generateVerificationCode({
length: 6,
charset: 'ABCDEF0123456789' // Hexadecimal
});
console.log(customCode); // Example: "A3F90B"
generateVerificationCode([options])Generates a random verification code string.
| Option | Type | Default | Description |
|---|---|---|---|
length | number | 6 | Length of the generated code |
digits | boolean | true | Include digits (0-9) |
lowercase | boolean | false | Include lowercase letters (a-z) |
uppercase | boolean | true | Include uppercase letters (A-Z) |
symbols | boolean | false | Include symbols (!@#$%^&*...) |
avoidAmbiguous | boolean | true | Avoid ambiguous characters (0, O, o, 1, l, I, etc.) |
charset | string | undefined | Custom character set (overrides all other character options) |
Returns a string containing the randomly generated verification code.
TypeError - If length is not a positive integerError - If the character pool is empty (no character types enabled)const emailCode = generateVerificationCode({
length: 6,
digits: true,
uppercase: true,
lowercase: false,
avoidAmbiguous: true
});
// Example: "A3K9P2"
const smsCode = generateVerificationCode({
length: 4,
digits: true,
uppercase: false,
lowercase: false
});
// Example: "7392"
const resetToken = generateVerificationCode({
length: 32,
digits: true,
uppercase: true,
lowercase: true,
symbols: true,
avoidAmbiguous: false
});
// Example: "aB3#xY9@mK!pQ2$wR5^nT8&vU7*zC4"
This library uses crypto.randomBytes() when available (Node.js environment) for cryptographically secure random number generation. In browser environments or when crypto is unavailable, it falls back to Math.random().
Note: For security-critical applications, ensure you're running in an environment where crypto.randomBytes() is available.
This library works in all modern browsers and Node.js environments. When using in browsers, make sure to bundle it with your preferred build tool (Webpack, Rollup, etc.).
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
Niroshan Vijayarasa
If you encounter any issues or have questions, please file an issue on the GitHub repository.
FAQs
Small package to generate random verification codes
The npm package rand-verification-code receives a total of 1 weekly downloads. As such, rand-verification-code popularity was classified as not popular.
We found that rand-verification-code demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.