Patcode is a human-centric numeric code generator designed for OTP, SMS verification, 2FA systems, banking environments, onboarding flows, and any scenario that requires short and memorable codes. The library focuses on generating structured, high-readability numeric patterns while maintaining configurable security guarantees.
Patcode provides:
- Human-friendly numeric code generation (4-digit and 6-digit)
- Pattern-based strategies (AABB, ABAB, wave, mirror, escalator)
- Configurable strictness levels and security modes
- Adaptive UX profiles (children, elderly, asia, latam)
- Code analysis engine (entropy, memorability, collision risk)
- Secure RNG abstraction with fallback logic
- Fully typed API with TypeScript support
- Works in Node.js, browsers, Deno, Bun, Cloudflare Workers
Memorability and predictability often conflict in numeric code generation. Patcode attempts to balance these competing goals by providing:
- Predictably structured patterns for user experience
- Customizable entropy and strictness levels
- Optional banking-grade security modes
- Transparent analysis tools for evaluating generated codes
Patcode supports all major JavaScript runtimes.
npm install patcodeor
yarn add patcodeor
pnpm add patcodeor
bun add patcodeESM:
import { generateCode } from 'patcode'CJS:
const { generateCode } = require('patcode')Generate a 6-digit code:
import { generateCode } from 'patcode'
const code = generateCode()Generate a 4-digit code
generateCode({ length: 4 })Use a specific pattern strategy
generateCode({ strategy: 'wave' })Analyze a code
import { analyze } from 'patcode'
const result = analyze('5566')Patcode includes multiple predefined strategies:
- mixed
- pairs
- triples
- mirror
- wave
- escalator
- pairWave
- custom
Patterns include:
- AABB
- ABAB
- ABBA
- AAAB
- ABCABC
- ABCCBA
These strategies produce codes with predictable structures, improving user memorability.
Patcode includes a built-in analysis module that identifies:
- Shannon entropy (normalized per code length)
- Memorability score
- Collision risk
- Ascending or descending sequences
- Structural patterns
- Group segmentation
This module can be used for research, reporting, debugging, or building custom rules for OTP distribution.
Different user groups memorize patterns differently. Each profile adjusts:
- allowed patterns
- memorability weighting
- entropy thresholds
- sequence filtering
- digit selection preferences
Patcode offers specialized operational modes:
- normal
- banking
- enterprise
- ultra-secure
High-security modes reduce pattern predictability, avoid digit repetition, and increase entropy guarantees.
Patcode includes an abstraction layer for secure random number generation.
Supported RNG sources:
- Web Crypto API
- Node.js crypto.webcrypto
- Custom user-provided RNG
- Math.random fallback (only when explicitly allowed)
Secure RNG is automatically selected when available.
Patcode is particularly effective in:
- SMS verification workflows
- Password reset flows
- 2FA onboarding
- Banking transaction approvals
- One-time PIN generation
- IoT device pairing
- Access control terminals
- Authentication systems with UX constraints
If your system requires codes that are both human-friendly and structurally meaningful, Patcode is a suitable solution.
Patcode is not intended for:
- Cryptographically guaranteed high-entropy tokens
- Long-form UUID or SHA-based identifiers
- Authentication flows that prohibit human-readable structure
- Encoding arbitrary data into numeric strings
For these cases, consider using UUID v4 or crypto-based random generators.