GithubHelp home page GithubHelp logo

isabella232 / node-secure-pin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from frisb/node-secure-pin

0.0 0.0 0.0 835 KB

A cryptographically secure pin code generator for NodeJS

JavaScript 50.95% TypeScript 49.05%

node-secure-pin's Introduction

A simple and cryptographically secure way to generate PIN codes and Strings. Has no dependencies and includes Typescript definitions.

GitHub stars Build Status codecov npm Beerpay Beerpay Homepage

Installation

From NPM

npm install secure-pin

Typescript

import * as SecurePIN from "secure-pin";

Javascript

var SecurePIN = require("secure-pin");

Generate PIN

Signature

SecurePIN.generatePin(length : number, callback: (pin: string) => void);
  • The length can be any number > 0

Example

SecurePIN.generatePin(4, (pin)=> {
    console.log("Pin: " + pin);
})

Returns

Pin: 1234

Synchronously

You can also generate a pin synchronously with generatePinSync

var pin = SecurePIN.generatePinSync(4);

Generate Numbers

Methods also exist to securely generate numbers within a given range

Async

SecurePIN.generateInt(0, 100, function(int) {
    console.log(int);
});

Synchronously

var int = SecurePIN.generateIntSync(0, 100);
console.log(int);

Generate String

In order to generate a string, first you must create a character set. This can either be an array of characters, or the built-in CharSet class

var charSet = new SecurePIN.CharSet();

Then, you need to add characters to the set. The class has a couple functions to add basic characters, as well as some extra functions.

  • addLowerCaseAlpha() Adds the characters a-z to the set. Returns the CharSet.
  • addUpperCaseAlpha() Adds the characters A-Z to the set. Returns the CharSet.
  • addNumeric() Adds the characters 0-9 to the set. Returns the CharSet.
  • addSpecialCharacters() Adds the characters !"#$%&'()*+,-./:;<=>?@[\]^_\{|}~. Returns the CharSet.
  • addCharacter( char ) Adds the specified characters to the set. Can be either a, abc, or ["a", "b", "c"]. Returns the CharSet.
  • randomize() Randomizes the characters in the set, providing greater entropy. Returns the CharSet.
  • removeChar( char ) Removes the specified characters from the set. Can be either a, abc, or ["a", "b", "c"]. Returns the CharSet.

Example

var charSet = new SecurePIN.CharSet();
charSet.addLowerCaseAlpha().addUpperCaseAlpha().removeChar("aA").randomize();

Once you have your CharSet created, pass it along to the string generator and generate a string!

var charSet = ...
...
SecurePIN.generateString(15, charSet, function(str) {
	console.log(str);
});

Synchronous Example

var charSet = ...
...
var str = SecurePIN.generateStringSync(15, charSet);
console.log(str);

PIN Entropy

Secure-Pin uses a cryptographically secure method of generating pins, ensuring a high amount of entropy

Entropy Graph

Above is a graph of 100,000 randomly generated 4 Digit PINs, with an even distribution across the graph.

The X-Axis represents the PIN number/1000.0f, the Y-Axis represents the amount of times the PIN was generated during the 100,000 cycle.

String Entropy

String generation uses the same cryptographically secure routines as the PIN generation. The result is a secure string generator with a very low collision rate.

Below is a sample of a 5 character alphanumeric string, with both upper and lowercase letters.

Collision | Generation | Collided String
----------|------------|----------------
1         | 36368      | V81w4
2         | 64478      | TfYqX
3         | 66830      | kNtBL
4         | 81599      | c5MQH
5         | 86058      | neddv
6         | 88481      | MqHq6
7         | 92785      | wVMep
8         | 94769      | y0uNQ
9         | 94896      | bUERh
10        | 94941      | vKBY7
...
290       | 671944     | JpYhU
291       | 672343     | aLL0D
292       | 674242     | TNnj4
293       | 674632     | Tv9Xp
294       | 674796     | 7dFX6
295       | 675841     | R3weS
296       | 677368     | mwftq
297       | 677385     | O3Lmf
298       | 677507     | 2bOwb
299       | 677818     | 4xQEL
300       | 678343     | KhNZW

At the start of the generation, the first collision only occurs after 36,368 strings are generated. As we move later on in the generation we see that the collisions happen more frequently. This is to be expected, as we're only using a 5 character string.

10 Character string

Let's increase the string size to 10 characters and see what happens.

Collision | Generation | Collided String | Collision Chance
----------|------------|-----------------|-----------------
2796203 Collisions: 0
<--- Last few GCs --->

[25568:0000014404C51BE0]   355188 ms: Mark-sweep 1063.7 (1200.0) -> 1063.6 (1200.0) MB, 2444.7 / 0.0 ms  allocation failure GC in old space requested

Oh, we ran out of memory. Apparently we managed to generate 2,796,203 strings without a single collision!

node-secure-pin's People

Contributors

ozouai avatar ashbrener 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.