UNPKG

@sunney/flareutils

Version:

Small Utilities and little goodies that make developing with Cloudflare easier and faster.

61 lines (60 loc) 2.52 kB
import type { IsaacSeed } from "./types"; /** * Cryptographically-secure random number generator. Based on the [ISAAC algorithm](http://burtleburtle.net/bob/rand/isaac.html) by [Bob Jenkins](http://burtleburtle.net/bob/), and the JS implementation by [Yves-Marie K. Rinquin](https://github.com/rubycon). Backed by [crypto.getRandomValues](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues), and the [DRAND](https://drand.love) network. */ export declare class Isaac { private readonly runs; private m; private acc; private brs; private cnt; private r; private gnt; /** * This promise represents whether the seeding process has been completed. It is recommended that you create the Isaac object as early as possible, do other tasks as needed, and then *await* the promise afterward to ensure that the seeding process has completed. If it is *false*, then the seeding process has completed, and no *await* is necessary. */ seeding: Promise<void> | false; /** * Creates a new Isaac CSPRNG. Note that you must await `seeding` before using the generator. * @param {IsaacSeed} seed Seed to be fed into the generator. * @param {number} runs Number of times to re-run the generator. * @constructor */ constructor(seed: IsaacSeed, runs?: number); /** * Batch-generates 256 random numbers, and stores them in the number buffer. * @private */ private prng; /** * Shuffles the given array with the seed array, to ensure even mix. * @param {number[]} arr Array filled with mixed numbers * @param {number[]} s The seed, as an array of numbers * @returns {number[]} The mixed array * @private */ private superShuffle; /** * Resets the internal state of the generator. * @private */ private reset; /** * Seeds the generator. Note that you must await `seeding` before using the generator, if not manually seeded and awaited. * @param {IsaacSeed} seed Seed to be fed into the generator * @returns {Promise<void>} Promise that resolves when the generator has been seeded * @async * @example ```ts * await isaac.seed(seed); * ``` */ seed(seed: IsaacSeed): Promise<void>; /** * Returns a pre-generated random number from the number buffer. * @returns {number} A random number between 0 and 1 * @example ```ts * const num = isaac.rand(); * ``` */ rand(): number; }