0.1.0 • Published 8 years ago

deterministic-pseudorandombytes v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

deterministic-pseudorandombytes

Stability: 1 - Experimental

NPM version

Node.js crypto.randomBytes(size[, callback]) API compatible deterministic pseudorandom implementation.

Usage

const Generator = require("deterministic-pseudorandombytes");

const generator1 = new Generator({seed: "foo"});
const bytes1 = generator1.randomBytes(42);

const generator2 = new Generator({seed: "foo"});
const bytes2 = generator2.randomBytes(42);

const random = require("random-seed");
const generator3 = new Generator({random: random.create("foo")});
const bytes3 = generator3.randomBytes(42);

console.log(bytes1.equals(bytes2));
console.log(bytes1.equals(bytes3));

Overview

This module uses random-seed in order to generate deterministic pseudorandom bytes. The primary use-case for this is to support deterministic testing.

Documentation

Generator

new Generator(config)

  • config: Object
    • random: Object Initialized instance of random-seed generator. Mutually exclusive with config.seed.
    • seed: String Seed to use to initialize a new instance of random-seed generator. Mutually exclusive with config.random.

Creates a new Generator instance ready to serve random bytes.

generator.randomBytes(size, callback)

  • size: Integer Number of bytes to generate.
  • callback: Function (Default: undefined) (error, buffer) => {} Callback to invoke once bytes are generated.

Generates deterministic pseudorandom bytes. If callback is not provided, bytes will returned synchronously as a Buffer.