tokgen

Generate cryptographically secure token strings

  • tokgen

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
tokgen
301.0.08 years ago8 years agoMinified + gzip package size for tokgen in KB

Readme

tokgen
!npm Versionnpm-imagenpm-url !npm Downloadsdownloads-imagedownloads-url !Test Statustravis-imagetravis-url !Test Coveragecoveralls-imagecoveralls-url !MIT Licensedlicense-imagelicense-url
Generate cryptographically secure token strings.
const TokenGenerator = require('tokgen');

let generator = new TokenGenerator();

let token = generator.generate();
// => 'Q1GM0wL95xUkE2JBXzBiV75MrGTw6GAk'

token = generator.generate();
// => 'RPTXBktJl1lgHMTVQOzKVpFlR3hfOIfI'

const TokenGenerator = require('tokgen');

let generator = new TokenGenerator({chars: '0-9a-f', length: 8});

let token = generator.generate();
// => '5f8ab69e'

token = generator.generate();
// => 'e5e3c24a'

Installation

npm install tokgen

API

const TokenGenerator = require('tokgen');

new TokenGenerator(options)

Create a new token generator with the specified options.

options

object, string, number (default = {})
TokenGenerator's constructor accepts these properties in the options object:
Note: If options is a string it's treated as chars. Note: If options is a number it's treated as length.
chars
string (default = '0-9a-zA-Z')
The character range used for tokens returned by this generator.
new TokenGenerator(); // default

new TokenGenerator({chars: 'a-z'});

new TokenGenerator('a-z'); // shortcut
length
number (default = 32)
The default length (in characters) of tokens returned by this generator.
new TokenGenerator(); // default

new TokenGenerator({length: 16});

new TokenGenerator(16); // shortcut

TokenGenerator#generate(length, callback)

Generates a new token string. The call is asynchronous if callback is specified.

length

number (default = undefined)
The length of the token to be generated. If length is not specified the default length as given to the constructor is used.
let generator = new TokenGenerator();

// default length
let token = generator.generate();
// => 'mY9LJeyGt5p6TcJ7kEOm4M0N7mdoIbGh'

// explicit length
token = generator.generate(8);
// => 'sySbqK9N'

callback

function (default = undefined)
Generate the token asynchronously and then pass it to the callback.
The callback is expected to be a (for Node.js typical) error-first callback.
let generator = new TokenGenerator();

// default length
generator.generate((error, token) => {
  if (error) {
    return console.error(error);
  }
  console.log(token);
  // => 'mY9LJeyGt5p6TcJ7kEOm4M0N7mdoIbGh'
});

// explicit length
generator.generate(8, (error, token) => {
  if (error) {
    return console.error(error);
  }
  console.log(token);
  // => 'sySbqK9N'
});

Tests

To run the test suite, install dependencies, then run npm test:
npm install
npm test

Coverage reports are generated by running npm run coverage.
Linting is done with npm run lint.