|
| 1 | +# Patcode Documentation |
| 2 | + |
| 3 | +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. |
| 4 | +The library focuses on generating structured, high-readability numeric patterns while maintaining configurable security guarantees. |
| 5 | + |
| 6 | +**Patcode provides**: |
| 7 | + |
| 8 | +- Human-friendly numeric code generation (4-digit and 6-digit) |
| 9 | +- Pattern-based strategies (AABB, ABAB, wave, mirror, escalator) |
| 10 | +- Configurable strictness levels and security modes |
| 11 | +- Adaptive UX profiles (children, elderly, asia, latam) |
| 12 | +- Code analysis engine (entropy, memorability, collision risk) |
| 13 | +- Secure RNG abstraction with fallback logic |
| 14 | +- Fully typed API with TypeScript support |
| 15 | +- Works in Node.js, browsers, Deno, Bun, Cloudflare Workers |
| 16 | + |
| 17 | +## Why Patcode |
| 18 | + |
| 19 | +Memorability and predictability often conflict in numeric code generation. |
| 20 | +Patcode attempts to balance these competing goals by providing: |
| 21 | + |
| 22 | +1. Predictably structured patterns for user experience |
| 23 | +2. Customizable entropy and strictness levels |
| 24 | +3. Optional banking-grade security modes |
| 25 | +4. Transparent analysis tools for evaluating generated codes |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +Patcode supports all major JavaScript runtimes. |
| 30 | + |
| 31 | +### Node.js / Bun / Deno |
| 32 | + |
| 33 | +```bash |
| 34 | +npm install patcode |
| 35 | +``` |
| 36 | + |
| 37 | +or |
| 38 | + |
| 39 | +```bash |
| 40 | +yarn add patcode |
| 41 | +``` |
| 42 | + |
| 43 | +or |
| 44 | + |
| 45 | +```bash |
| 46 | +pnpm add patcode |
| 47 | +``` |
| 48 | + |
| 49 | +or |
| 50 | + |
| 51 | +```bash |
| 52 | +bun add patcode |
| 53 | +``` |
| 54 | + |
| 55 | +ESM: |
| 56 | + |
| 57 | +```js |
| 58 | +import { generateCode } from 'patcode' |
| 59 | +``` |
| 60 | + |
| 61 | +CJS: |
| 62 | + |
| 63 | +```js |
| 64 | +const { generateCode } = require('patcode') |
| 65 | +``` |
| 66 | + |
| 67 | +## Quick Start |
| 68 | + |
| 69 | +Generate a 6-digit code: |
| 70 | + |
| 71 | +```js |
| 72 | +import { generateCode } from 'patcode' |
| 73 | + |
| 74 | +const code = generateCode() |
| 75 | +``` |
| 76 | + |
| 77 | +Generate a 4-digit code |
| 78 | + |
| 79 | +```js |
| 80 | +generateCode({ length: 4 }) |
| 81 | +``` |
| 82 | + |
| 83 | +Use a specific pattern strategy |
| 84 | + |
| 85 | +```js |
| 86 | +generateCode({ strategy: 'wave' }) |
| 87 | +``` |
| 88 | + |
| 89 | +Analyze a code |
| 90 | + |
| 91 | +```js |
| 92 | +import { analyze } from 'patcode' |
| 93 | + |
| 94 | +const result = analyze('5566') |
| 95 | +``` |
| 96 | + |
| 97 | +## Features Overview |
| 98 | + |
| 99 | +### Pattern-Based Code Generation |
| 100 | + |
| 101 | +Patcode includes multiple predefined strategies: |
| 102 | + |
| 103 | +- mixed |
| 104 | +- pairs |
| 105 | +- triples |
| 106 | +- mirror |
| 107 | +- wave |
| 108 | +- escalator |
| 109 | +- pairWave |
| 110 | +- custom |
| 111 | + |
| 112 | +Patterns include: |
| 113 | + |
| 114 | +- AABB |
| 115 | +- ABAB |
| 116 | +- ABBA |
| 117 | +- AAAB |
| 118 | +- ABCABC |
| 119 | +- ABCCBA |
| 120 | + |
| 121 | +These strategies produce codes with predictable structures, improving user memorability. |
| 122 | + |
| 123 | +### Code Analysis Engine |
| 124 | + |
| 125 | +Patcode includes a built-in analysis module that identifies: |
| 126 | + |
| 127 | +- Shannon entropy (normalized per code length) |
| 128 | +- Memorability score |
| 129 | +- Collision risk |
| 130 | +- Ascending or descending sequences |
| 131 | +- Structural patterns |
| 132 | +- Group segmentation |
| 133 | + |
| 134 | +This module can be used for research, reporting, debugging, or building custom rules for OTP distribution. |
| 135 | + |
| 136 | +### Adaptive User Profiles |
| 137 | + |
| 138 | +Different user groups memorize patterns differently. |
| 139 | +Each profile adjusts: |
| 140 | + |
| 141 | +- allowed patterns |
| 142 | +- memorability weighting |
| 143 | +- entropy thresholds |
| 144 | +- sequence filtering |
| 145 | +- digit selection preferences |
| 146 | + |
| 147 | +### Security Modes |
| 148 | + |
| 149 | +Patcode offers specialized operational modes: |
| 150 | + |
| 151 | +- normal |
| 152 | +- banking |
| 153 | +- enterprise |
| 154 | +- ultra-secure |
| 155 | + |
| 156 | +High-security modes reduce pattern predictability, avoid digit repetition, and increase entropy guarantees. |
| 157 | + |
| 158 | +### Universal RNG Engine |
| 159 | + |
| 160 | +Patcode includes an abstraction layer for secure random number generation. |
| 161 | + |
| 162 | +Supported RNG sources: |
| 163 | + |
| 164 | +- Web Crypto API |
| 165 | +- Node.js crypto.webcrypto |
| 166 | +- Custom user-provided RNG |
| 167 | +- Math.random fallback (only when explicitly allowed) |
| 168 | + |
| 169 | +Secure RNG is automatically selected when available. |
| 170 | + |
| 171 | +## When to Use Patcode |
| 172 | + |
| 173 | +Patcode is particularly effective in: |
| 174 | + |
| 175 | +- SMS verification workflows |
| 176 | +- Password reset flows |
| 177 | +- 2FA onboarding |
| 178 | +- Banking transaction approvals |
| 179 | +- One-time PIN generation |
| 180 | +- IoT device pairing |
| 181 | +- Access control terminals |
| 182 | +- Authentication systems with UX constraints |
| 183 | + |
| 184 | +If your system requires codes that are both human-friendly and structurally meaningful, Patcode is a suitable solution. |
| 185 | + |
| 186 | +## When Not to Use Patcode |
| 187 | + |
| 188 | +Patcode is not intended for: |
| 189 | + |
| 190 | +- Cryptographically guaranteed high-entropy tokens |
| 191 | +- Long-form UUID or SHA-based identifiers |
| 192 | +- Authentication flows that prohibit human-readable structure |
| 193 | +- Encoding arbitrary data into numeric strings |
| 194 | + |
| 195 | +For these cases, consider using UUID v4 or crypto-based random generators. |
0 commit comments