@bturkis/keygen
v1.0.0
Published
Cryptographically secure key generators — hex keys, UUIDs, passwords, random strings, and text-to-key hashing. Uses Web Crypto API.
Maintainers
Readme
@bturkis/keygen
Cryptographically secure key generators for Node.js, Deno, and browsers.
All generators use Web Crypto API (crypto.getRandomValues) — no Math.random().
🌐 Website: keygen.neisterse.com
Install
npm install @bturkis/keygen
# or
pnpm add @bturkis/keygen
# or
yarn add @bturkis/keygenQuick Start
import {
generateHexKey,
generateUUID,
generatePassword,
generateSecretKey,
generateRandomString,
textToKey,
} from "@bturkis/keygen";
// Hex key (256-bit)
const hexKey = generateHexKey();
// => ["a1b2c3d4e5f6..."]
// UUID v4
const uuid = generateUUID();
// => ["550e8400-e29b-41d4-a716-446655440000"]
// Secure password
const password = generatePassword({ length: 16 });
// => ["xK9@mP2#qR5&wT8!"]
// Secret key (mixed chars)
const secret = generateSecretKey({ length: 64 });
// => ["x!K9@mP2#qR5&wT8..."]
// Random alphanumeric string
const token = generateRandomString({ length: 24 });
// => ["Abc123Xyz456DefGHij789Klm"]
// Text to SHA-256 hash
const hash = await textToKey("hello world");
// => "b94d27b9934d3e08..."API Reference
generateHexKey(options?)
Generate cryptographically secure hex keys.
| Option | Type | Default | Description |
| ------------------- | --------- | ------- | -------------------------- |
| bytes | number | 32 | Number of random bytes |
| count | number | 1 | Number of keys to generate |
| prefix | string | "" | Prepend to each key |
| suffix | string | "" | Append to each key |
| separator | string | "" | Between hex segments |
| separatorInterval | number | 0 | Chars between separators |
| uppercase | boolean | false | Uppercase hex output |
// API key with prefix
generateHexKey({ bytes: 32, prefix: "sk_live_" });
// MAC address style
generateHexKey({
bytes: 6,
separator: ":",
separatorInterval: 2,
uppercase: true,
});
// => ["A1:B2:C3:D4:E5:F6"]generateUUID(options?)
Generate RFC 4122 compliant UUID v4 identifiers.
| Option | Type | Default | Description |
| ------- | -------- | ------- | --------------- |
| count | number | 1 | Number of UUIDs |
generatePassword(options?)
Generate secure passwords. Quotes (' and ") excluded by default for shell/SQL safety.
| Option | Type | Default | Description |
| --------------- | --------- | ------- | ------------------------ |
| length | number | 16 | Password length |
| count | number | 1 | Number of passwords |
| uppercase | boolean | true | Include A-Z |
| lowercase | boolean | true | Include a-z |
| numbers | boolean | true | Include 0-9 |
| symbols | boolean | true | Include special chars |
| customSymbols | string | — | Override default symbols |
// Simple password
generatePassword({ length: 8, symbols: false });
// Custom symbols only
generatePassword({ customSymbols: "!@#$", length: 20 });generateSecretKey(options?)
High-entropy keys with letters, digits, and special characters.
| Option | Type | Default | Description |
| -------- | -------- | ------- | -------------- |
| length | number | 32 | Key length |
| count | number | 1 | Number of keys |
generateRandomString(options?)
Alphanumeric strings (A-Z, a-z, 0-9). Safe for URLs, filenames, and databases.
| Option | Type | Default | Description |
| -------- | -------- | ------- | ----------------- |
| length | number | 16 | String length |
| count | number | 1 | Number of strings |
textToKey(text, options?)
Convert text to a deterministic hash. Returns a Promise<string>.
| Option | Type | Default | Description |
| ----------- | -------- | ----------- | ---------------------------------------- |
| algorithm | string | "SHA-256" | SHA-1, SHA-256, SHA-384, SHA-512 |
calculateEntropy(params)
Calculate entropy information for a key configuration.
import { calculateEntropy } from "@bturkis/keygen";
calculateEntropy({ bytes: 32 });
// => { bits: 256, strength: "very-strong", label: "Very Strong - Maximum security" }
calculateEntropy({ charPool: 92, length: 16 });
// => { bits: 104, strength: "fair", label: "Fair - Acceptable for basic security" }Security
- ✅ Uses
crypto.getRandomValues()(Web Crypto API) - ✅ No
Math.random()— cryptographically secure - ✅ Works in Node.js 18+, Deno, and modern browsers
- ✅ Zero runtime dependencies (except
uuidfor UUID v4) - ✅ Tree-shakeable ESM + CJS builds
License
MIT © bturkis
