tokgen
Generate cryptographically secure token strings.
const TokenGenerator = ; let generator = ; let token = generator;// => 'Q1GM0wL95xUkE2JBXzBiV75MrGTw6GAk' token = generator;// => 'RPTXBktJl1lgHMTVQOzKVpFlR3hfOIfI'
const TokenGenerator = ; let generator = chars: '0-9a-f' length: 8; let token = generator;// => '5f8ab69e' token = generator;// => 'e5e3c24a'
Installation
npm install tokgen
API
const TokenGenerator = ;
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.
; // default chars: 'a-z'; 'a-z'; // shortcut
length
number (default = 32
)
The default length (in characters) of tokens returned by this generator.
; // default length: 16; 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 = ; // default lengthlet token = generator;// => 'mY9LJeyGt5p6TcJ7kEOm4M0N7mdoIbGh' // explicit lengthtoken = generator;// => '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 = ; // default lengthgenerator; // explicit lengthgenerator;
Tests
To run the test suite, install dependencies, then run npm test
:
npm installnpm test
Coverage reports are generated by running npm run coverage
.
Linting is done with npm run lint
.