trng-crypto
v1.1.0
Published
A small TypeScript/Node.js utility that generates a cryptographically secure random integer with a specific digit length.
Readme
trng-crypto
A small TypeScript/Node.js utility that generates a cryptographically secure random integer with a specific digit length.
Features
- Uses Node's
crypto.randomBytes()for secure randomness. - Avoids modulo bias via rejection sampling.
- Returns
bigintby default, or a decimal string with{ type: 'string' }.
Install
npm i trng-cryptogetRandomInt(desiredOutputLength: number, options?: { type?: 'string' | 'bigint' }): bigint | string
Generates a random integer with exactly desiredOutputLength decimal digits (desiredOutputLength = 1 may return 0–9).
Optional second argument: pass { type: 'string' } for a decimal string, or { type: 'bigint' } / omit options for a bigint. Default output is bigint.
desiredOutputLengthmust be an integer and at least1.
Throws:
TypeErrorifdesiredOutputLengthis not an integer.RangeErrorifdesiredOutputLength < 1.TypeErrorifoptions.typeis present but not"string"or"bigint"(e.g. from plain JavaScript).
Usage
import { getRandomInt } from 'trng-crypto';
const oneDigit = getRandomInt(1); // 0n to 9n
const sixDigits = getRandomInt(6); // 100000n to 999999n
const asString = getRandomInt(6, { type: 'string' }); // '100000' to '999999'
console.log(oneDigit, sixDigits, asString); // 3n 993284n '276402'const { getRandomInt } = require('trng-crypto');
const oneDigit = getRandomInt(1); // 0n to 9n
const sixDigits = getRandomInt(6); // 100000n to 999999n
const asString = getRandomInt(6, { type: 'string' }); // '100000' to '999999'
console.log(oneDigit, sixDigits, asString); // 8 403709 '855681'Notes
- Project output is configured to compile from
srcto dual outputs indist/cjsanddist/esm.
