@arraypress/generate
v1.0.0
Published
Cryptographically secure generation utilities — license keys, short IDs, prefixed keys, codes, and random strings.
Downloads
54
Maintainers
Readme
@arraypress/generate
Cryptographically secure generation utilities — license keys, short IDs, prefixed keys, codes, random strings, tokens, and PINs.
All functions use crypto.getRandomValues() for secure randomness. Works in browsers, Node.js 19+, Cloudflare Workers, Deno, and Bun. Zero dependencies.
Install
npm install @arraypress/generateFunctions
licenseKey(options?)
Generate a formatted license key.
import { licenseKey } from '@arraypress/generate';
licenseKey()
// => 'FA15-328F-84FE-1974-D55D-A3E4-5F66-35A3'
licenseKey({ bytes: 8 })
// => 'FA15-328F-84FE-1974'
licenseKey({ segmentLength: 8, separator: '.' })
// => 'FA15328F.84FE1974.D55DA3E4.5F6635A3'key(prefix?, length?, charset?)
Generate a prefixed key.
import { key } from '@arraypress/generate';
key() // => 'id_a7bx3km2q'
key('usr') // => 'usr_m7xp3qn9v'
key('sk', 24) // => 'sk_m7xp3qn9v2bk8ej4aw6trcf'shortId(length?)
Generate a short URL-safe ID. Excludes confusing characters (0/O, 1/I/l).
import { shortId } from '@arraypress/generate';
shortId() // => 'mKp3Xv7'
shortId(12) // => 'mKp3Xv7nBq2Y'code(options?)
Generate codes with configurable segments, separators, and character sets.
import { code } from '@arraypress/generate';
code({ segments: 4, separator: '-' })
// => 'A7BX-3KM2-QP9N-V4HL'
code({ length: 6, segments: 1, numbers: false })
// => 'KBXQNV'
code({ segments: 3, separator: '-', prefix: 'DISC-' })
// => 'DISC-A7BX-3KM2-QP9N'string(length?, charset?)
Generate a random string. Accepts named charsets or a custom string.
import { string } from '@arraypress/generate';
string(32) // => 'kP7mN9qW2vX8bE4rL6tY0uA3sD5gH1j'
string(16, 'hex') // => 'a3f90bce172d64a0'
string(8, 'numeric') // => '47291038'
string(10, 'ABC123') // => 'B1AC32A1CB'Built-in charsets: alnum, alnumLower, alnumUpper, alpha, alphaLower, alphaUpper, numeric, hex, hexUpper, urlSafe.
token(bytes?)
Generate a hex token.
import { token } from '@arraypress/generate';
token() // => 64-char hex string (32 bytes of entropy)
token(16) // => 32-char hex stringpin(length?)
Generate a numeric PIN.
import { pin } from '@arraypress/generate';
pin() // => '472910'
pin(4) // => '3847'charsets
Access built-in character sets for custom use.
import { charsets } from '@arraypress/generate';
charsets.alnum // 'abcdefghijklmnop...0123456789'
charsets.hex // '0123456789abcdef'
charsets.urlSafe // excludes 0, O, 1, I, l
charsets.symbols // '!@#$%^&*()_+-=[]{}|;:,.<>?'License
MIT
