Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bigint-secrets

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bigint-secrets

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

  • 1.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-84%
Maintainers
1
Weekly downloads
 
Created
Source

bigint-secrets

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

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 webcrypto or node crypto.

Installation

bigint-secrets is distributed as both an ES6 and a CJS module.

The ES6 module is built for any web browser supporting BigInt. The module only uses native javascript implementations and no polyfills had been applied.

The CJS module is built as a standard node module.

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

npm install bigint-secrets

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**256));

From a browser:

  <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 ** 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

randBetween(max, min)Promise

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

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)

prime(bitLength, iterations)Promise

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

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

ParamTypeDefaultDescription
byteLengthnumberThe desired number of random bytes
forceLengthbooleanfalseIf 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]

ParamTypeDefaultDescription
maxbigintReturned value will be <= max
minbigint1Returned 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)

ParamTypeDefaultDescription
wbigintAn integer to be tested for primality
iterationsnumber16The 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

ParamTypeDefaultDescription
bitLengthnumberThe required bit length for the generated prime
iterationsnumber16The number of iterations for the Miller-Rabin Probabilistic Primality Test

Keywords

FAQs

Package last updated on 15 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc