npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bigint-secrets

v1.2.5

Published

Cryptographically secure random numbers and prime generation/testing using native JS (stage 3) implementation of BigInt

Downloads

4

Readme

bigint-secrets

IMPORTANT! This package has been superseded by bigint-crypto-utils. Please install that package instead.

Secure random numbers and probable prime (Miller-Rabin primality test) generation/testing using native JS (stage 3) implementation of BigInt. It can be used by any Web Browser or webview supporting BigInt and with Node.js (>=10.4.0).

The operations supported on BigInts are not constant time. BigInt can be therefore unsuitable for use in cryptography. Many platforms provide native support for cryptography, such as Web Cryptography API or Node.js Crypto.

Installation

bigint-secrets is distributed for web browsers and/or webviews supporting BigInt as an ES6 module; and for Node.js (>=10.4.0), as a CJS module.

bigint-secrets can be imported to your project with npm:

npm install bigint-secrets

NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js.

For web browsers, you can also download the bundle from GitHub.

Usage example

With node js:

const secrets = require('bigingt-secrets');

// Generation of a probable prime of 2048 bits
const prime = await secrets.prime(2048);

// Testing if a prime is a probable prime (Miller-Rabin)
if ( await secrets.isProbablyPrime(prime) )
return true;

// Get a cryptographically secure random number between 1 and 2**256 bits.
const rnd = secrets.randBetween(BigInt(2) ** BigInt(256));

From a browser, you can just load the module in a html page as:

<script type="module">
  import * as bigintSecrets from 'bigint-secrets-latest.browser.mod.min.js';

  (async function () {
    // Get a cryptographically secure random number between 1 and 2**256 bits.
    const rnd = await bigintSecrets.randBetween(BigInt(2) ** BigInt(256));
    alert(rnd);

    // Generation of a probable prime of 2018 bits
    const p = await bigintSecrets.prime(2048);

    // Testing if a prime is a probable prime (Miller-Rabin)
    const isPrime = await bigintSecrets.isProbablyPrime(p);
    alert(p.toString() + '\nIs prime?\n' + isPrime);
  })();
</script>

bigint-secrets JS Doc

Functions

randBytes(byteLength, forceLength) ⇒ Promise

Secure random bytes for both node and browsers. Browser implementation uses WebWorkers in order to not lock the main process

Kind: global function
Returns: Promise - A promise that resolves to a Buffer/UInt8Array filled with cryptographically secure random bytes

| Param | Type | Default | Description | | --- | --- | --- | --- | | byteLength | number | | The desired number of random bytes | | forceLength | boolean | false | If we want to force the output to have a bit length of 8*byteLength. It basically forces the msb to be 1 |

randBetween(max, min) ⇒ Promise

Returns a cryptographically secure random integer between [min,max]

Kind: global function
Returns: Promise - A promise that resolves to a cryptographically secure random bigint between [min,max]

| Param | Type | Default | Description | | --- | --- | --- | --- | | max | bigint | | Returned value will be <= max | | min | bigint | 1 | Returned value will be >= min |

isProbablyPrime(w, iterations) ⇒ Promise

The test first tries if any of the first 250 small primes are a factor of the input number and then passes several iterations of Miller-Rabin Probabilistic Primality Test (FIPS 186-4 C.3.1)

Kind: global function
Returns: Promise - A promise that resolve to a boolean that is either true (a probably prime number) or false (definitely composite)

| Param | Type | Default | Description | | --- | --- | --- | --- | | w | bigint | | An integer to be tested for primality | | iterations | number | 16 | The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3 |

prime(bitLength, iterations) ⇒ Promise

A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator

Kind: global function
Returns: Promise - A promise that resolves to a bigint probable prime of bitLength bits

| Param | Type | Default | Description | | --- | --- | --- | --- | | bitLength | number | | The required bit length for the generated prime | | iterations | number | 16 | The number of iterations for the Miller-Rabin Probabilistic Primality Test |