Coder Social home page Coder Social logo

jlchntoz / node-jittertrng Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 249 KB

๐ŸŽฒ Bindings of Jitterentropy Library - A Hardware RNG based on CPU timing jitter - For Node.js

License: MIT License

Python 4.06% JavaScript 68.32% C++ 27.62%
trng node-js javascript-library random-number-generators random-generation true-random

node-jittertrng's Introduction

Node JitterTRNG

GitHub issues GitHub license Node version NPM version NPM downloads

This is a wrapper of jitterentropy-library, which is a hardware RNG based on CPU timing jitter, wrapped for Node.js.

For more information about this library, it is better to visit its site, www.chronox.de/jent.html.

This is my first attempt to create a native C++ module for Node.js, therefore it might be many bugs and might be just works on my machine. I have made some modification on the original source (Added a header) in order to make it compilable on Windows platform.

Usage

Please refer to documentation for exposed classes. :)

Example

const { JitterTrng } = require('jittertrng');

const rng = new JitterTrng();

async function() {
  console.log(await rng.randomAsync(100)); // Asynchronous call, get a float-point number between 0 and 100
}

console.log(rng.randomInt(1234, 5678)); // Synchronous call, gets an integer number between 1234 and 5678

const randombytes = rng.generate(100); // Gets 100 random bytes as a buffer.

rng.generateAsync(10000, (err, buffer) => {
  // Gets 10000 random bytes as a buffer.
});
// Getting large amount of random bits at once is slow, therefore asynchronous call will be safer as this does not block the javascript execution thread.
// Also, all asynchronous methods also provides old-school callback flow for use.

Or in a more convenient way:

const { RandomStream } = require('jittertrng');

(async() => {
  const rng = await RandomStream.create(1024); // Create a buffered random pool with 1024 bytes.
  // You also can use `new RandomStream(...)` constructor, but you have to be patient
  // because the pool does not immediately get ready after it constructs. (Async logic is used here)
  rng.random();
  rng.random(1, 10);
  rng.randomInt(1, 100);
  // Keep in mind that if the pool is already drain (and of cause it will starts to fill up at the background),
  // whatever you want to get, you will get undefined.
})();

Limits

As this is a true random number generator, it is not very efficient to get large amount of random numbers directly, even here I have made asynchronous direct and buffered methods to access the entropy. If you needs random values immediately at anytime and doesn't really mind the randomness, it is recommend to use other pseudo random number generators as a fallback, or you have another option to just use this as a source of seed for other pseudo random number generators.

Also, I don't make the memory secure here, this wrapper just pulls the random bytes out and pass it to the JavaScript engine, nothing is protecting them to prevent third party to access or spy on them. So if anyone wants to use this in something secure-critical is not recommend.

Installation

You can use

$ npm i jittertrng

to install this package.

As this is a native addon module, to use it you will have to prepare suitable C++ compilers (gcc for *nix systems and Visual C++ with Windows SDK for Windows systems) and node-gyp as a global dependency. Prebuilt binaries are not yet available, sorry about that :(

Contributing

Yes! If you don't mind my messy code and you want to make it better, issues and pull-requests are welcome!

License

Original jitterentropy-library is dual-licensed with BSD and GPLv2, all original notes are kept untouched in its own dependency directory. The wrapper and binding codes are licensed with MIT.

node-jittertrng's People

Contributors

dependabot[bot] avatar jlchntoz avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.