sign-prng
v1.0.1
Published
Create random hex signatures and turn them into consistent, reproducible random number sequences. Same signature always produces the same numbers - perfect for deterministic testing and shareable random states
Maintainers
Readme
sign-prng
🔧 A module for generating and validating hexadecimal signatures, creating pseudo-random numbers, and computing hashes based on deterministic signatures.
Installation
npm install sign-prngUsage
import sign from 'sign-prng'API
sign.generate(bytesLength?: number): string
Generates a random signature as a hexadecimal string.
Parameters:
bytesLength- Length in bytes (default: 16)
Returns: Hexadecimal string (32 chars for 16 bytes)
Example:
const signature = sign.generate()
// => "a1ace80f9fabed1bc4b2d28bd5081afc"
const longSignature = sign.generate(32)
// => "a1b2c3...64 chars"sign.check(signature: string): boolean
Validates if a string is a valid hexadecimal signature.
Parameters:
signature- String to validate
Returns: true if valid, false otherwise
Example:
sign.check('a1ace80f9fabed1bc4b2d28bd5081afc') // => true
sign.check('invalid') // => falsesign.getMatRandomFn(signature: string): () => number
Returns a deterministic pseudo-random number generator (0-1) based on signature.
Parameters:
signature- Source signature for seeding
Returns: Function that returns numbers between 0 and 1
Example:
const random = sign.getMatRandomFn('a1ace80f9fabed1bc4b2d28bd5081afc')
random() // => 0.7336...
random() // => 0.5182...
// Same signature produces same sequence
const random2 = sign.getMatRandomFn('a1ace80f9fabed1bc4b2d28bd5081afc')
random2() // => 0.7336... (same as first call)sign.getIntFn(signature: string, max?: number): () => number
Returns a deterministic integer generator (0 to max) based on signature.
Parameters:
signature- Source signature for seedingmax- Maximum value (default: 1)
Returns: Function that returns integers from 0 to max
Example:
const getInt = sign.getIntFn('a1ace80f9fabed1bc4b2d28bd5081afc', 100)
getInt() // => 42
getInt() // => 71
getInt() // => 61sign.getMod(signature: string, max: number): number
Calculates modulo of signature by max value.
Parameters:
signature- Hexadecimal signaturemax- Maximum value for modulo operation
Returns: Integer from 0 to max
Example:
sign.getMod('a1ace80f9fabed1bc4b2d28bd5081afc', 100)
// => 42 (always the same for this signature)sign.sha(str: string): string
Computes a simple hash of a string.
Parameters:
str- Input string to hash
Returns: Hexadecimal hash string
Example:
sign.sha('hello') // => "5d41402abc..."
sign.sha('world') // => "7c211433f0..."Use Cases
- Deterministic randomness: Use signatures as seeds for reproducible random sequences
- ID generation: Create random hex identifiers
- Reproducible states: Generate consistent states from seed values (games, simulations, testing)
License
ISC
