Harvests organic entropy (CPU timing + event-loop jitter), expands it via SHA-256/HKDF, and mixes the result with the platform CSPRNG to produce cryptographically strong random bytes.
promised-entropy collects non-deterministic system noise and processes it into a secure random stream.
- Sources: CPU timing races (floating-point operation counts) and event-loop jitter (
setTimeout/performance.nowdeltas). - Processing: Samples are packed, hashed with SHA-256, and expanded using HKDF-SHA-256 (with a counter-mode fallback).
- Mixing: The expanded output is XORed with fresh bytes from
crypto.getRandomValues(Web Crypto API). - Safety: Guarantees output never relies solely on timing noise and prevents repeated 32-byte hash blocks in long outputs.
Requires Node.js >= 18 (for native Web Crypto API) or a modern browser environment.
npm install promised-entropyimport { promisedEntropy } from 'promised-entropy';
// Generate 256 bits (32 bytes) of entropy
const randomBytes = await promisedEntropy(256);
console.log(randomBytes instanceof Uint8Array); // true
console.log(randomBytes.length); // 32const { promisedEntropy } = require('promised-entropy');
(async () => {
// Generate 512 bits (64 bytes) of entropy
const randomBytes = await promisedEntropy(512);
console.log(randomBytes instanceof Uint8Array); // true
})();The library operates on the principle that organic entropy sources are noisy and biased. It mitigates this through:
- Harvesting: Continuous background loops measure CPU work duration and event-loop wake-up delays.
- Expansion: Raw integer deltas are hashed (SHA-256) and expanded using HKDF to ensure uniform distribution.
- Mixing: The derived entropy is XORed with data from the platform's CSPRNG (
crypto.getRandomValues). This ensures that even if timing sources are compromised, the output remains secure. - Uniqueness: Expansion uses HKDF (or counter-mode SHA-256), so long outputs never repeat a single 32-byte digest.
Asynchronously generates a Promise<Uint8Array> containing cryptographically mixed bytes.
bits(number): Desired strength in bits. Output length isceil(bits / 8).options(EntropyOptions, optional): Harvester tuning parameters.
Returns: Promise<Uint8Array>
A class for low-level control over the entropy harvesting process.
const generator = new Generator({ loopDelay: 10 });
const samples = await generator.collect(256); // Returns raw integer deltascollect(bitsWanted: number): Collects raw samples until the estimated bit total is reached. ReturnsPromise<number[]>.countUnusedBits(): Returns the estimated unused bits currently buffered.stop()/resume(): Manually control the background harvesting loop.
| Option | Type | Default | Description |
|---|---|---|---|
loopDelay |
number |
5 |
Milliseconds to pause between loops while a collect/generate is in flight. |
lazyLoopDelay |
number |
30 |
Milliseconds to pause between loops while idle. |
workMin |
number |
1 |
Target milliseconds of CPU work per timing sample. |
autoStopBits |
number |
4096 |
Stop harvesting once this many unclaimed estimated bits are buffered. |
maxBitsPerDelta |
number |
4 |
Cap estimated entropy bits claimed per delta. |
autoStop |
boolean |
true |
Stop the background loop after collect finishes. |
- Return Type:
promisedEntropynow returns aUint8Arrayinstead of anumber[]. - Raw Samples: To access raw timing deltas, use
new Generator().collect(bits)instead of the main function. - Dependencies: Removed dependencies on
bluebirdandiced. Requires nativeasync/await. - Runtime: Requires Node.js >= 18 (or equivalent Web Crypto support).
MIT. Forked from keybase/more-entropy.