What is @lukeed/csprng?
@lukeed/csprng is a lightweight and fast library for generating cryptographically secure random numbers. It is designed to be simple and efficient, making it suitable for use in various applications where secure random number generation is required.
What are @lukeed/csprng's main functionalities?
Generate a random number
This feature allows you to generate a cryptographically secure random number of a specified byte length. The example code generates a 16-byte random number and logs it to the console.
const { random } = require('@lukeed/csprng');
const randomNumber = random(16); // Generates a 16-byte random number
console.log(randomNumber);
Generate a random number with a specific length
This feature allows you to specify the length of the random number in bytes. The example code generates a 32-byte random number and logs it to the console.
const { random } = require('@lukeed/csprng');
const randomNumber = random(32); // Generates a 32-byte random number
console.log(randomNumber);
Other packages similar to @lukeed/csprng
crypto
The 'crypto' module is a built-in Node.js module that provides cryptographic functionality, including a method for generating cryptographically secure random numbers. It is more comprehensive than @lukeed/csprng, offering a wide range of cryptographic operations.
uuid
The 'uuid' package is used for generating RFC4122 UUIDs (Universally Unique Identifiers). While it is not specifically designed for generating random numbers, it uses cryptographically secure random number generation under the hood for creating UUIDs.
randombytes
The 'randombytes' package is a simple library for generating cryptographically secure random bytes. It is similar to @lukeed/csprng in terms of functionality but is more focused on generating random bytes rather than numbers.
@lukeed/csprng
A tiny (~90B) isomorphic wrapper for crypto.randomBytes
in Node.js and browsers.
Why?
This package allows you/dependents to import a cryptographically secure generator (CSPRNG) without worrying about (aka, checking the runtime environment for) the different crypto
implementations. Instead, by extracting a random
function into a third-party/external package, one can rely on bundlers and/or module resolution to load the correct implementation for the desired environment.
In other words, one can include the browser-specific implementation when bundling for the browser, completely ignoring the Node.js code – or vice versa.
By default, this module is set up to work with Rollup, webpack, and Node's native ESM and CommonJS path resolutions.
Install
$ npm install --save @lukeed/csprng
Usage
General Usage
import { random } from '@lukeed/csprng';
const array = random(12);
Specific Environment
import { random } from '@lukeed/csprng/browser';
const array = random(1024);
import { random } from '@lukeed/csprng/node';
const array = random(1024);
API
random(length)
Returns: Buffer
or Uint8Array
Returns a typed array of given length
.
length
Type: Number
The desired length of your output TypedArray.
Related
- uid - A tiny (134B) and fast utility to randomize unique IDs of fixed length
- @lukeed/uuid - A tiny (230B), fast, and cryptographically secure UUID (V4) generator for Node and the browser
License
MIT © Luke Edwards