@csquare/random-bytes-seed v0.1.3
@csquare/random-bytes-seed
Zero-dependency package to generate seeded random bytes; TypeScript typings included.
Maintained by Mathieu Bour, Lead Platform Engineer at Cohesive Computing SA.
Installation
Install with npm:
npm install --save @csquare/random-bytes-seedInstall with Yarn:
yarn add @csquare/random-bytes-seedUsage
Basic usage
Using CommonJS syntax:
const { randomBytesSeed } = require('@csquare/random-bytes-seed');
const output = randomBytesSeed(10, 'my-seed'); // Generate a 10-bytes, stable bufferUsing ESM syntax (default import):
import randomBytesSeed from '@csquare/random-bytes-seed';
const output = randomBytesSeed(10, 'my-seed'); // Generate a 10-bytes, stable bufferor
import { randomBytesSeed } from '@csquare/random-bytes-seed';
const output = randomBytesSeed(10, 'my-seed'); // Generate a 10-bytes, stable bufferIf you do not provide a seed, the randomBytesSeed will simply fallbacks to randomBytes.
Advanced usage
Changing the base seed
By default, this packages uses a base seed equals to random-bytes-seed in conjunction of the given seed. You can
change the base seed by overriding the default options.
import { options } from '@csquare/random-bytes-seed';
options.seed = 'any-string-you-want'; // Override the base seed for all future callsChanging the hash algorithm
This package use Node createHash function from the crypto module. By default, the rounds are computed with
the sha256 hash algorithm. You can change this behavior by overriding the default options.
import { options } from '@csquare/random-bytes-seed';
options.algorithm = 'sha512'; // Use sha512 algorithm instead of sh256From the nodejs.org documentation:
The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are
'sha256','sha512', etc. On recent releases of OpenSSL,openssl list -digest-algorithms(openssl list-message-digest-algorithmsfor older versions of OpenSSL) will display the available digest algorithms.Reference: https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options