README
UUID Key Generator
uuid-token-generator. Please note that there are breaking changes when upgrading to uuid-token-generator
.
This module has been deprecated in favour of uid-generator instead since it allows for more customization in the tokens it generates and does not produce overlapping UIDs.
However, you should probably useProvides a class that generates random keys with custom size and base-encoding using the RFC4122 v4 UUID algorithm. Generated keys are strings that are guaranteed to always be the same length, depending on the bit-size specified for the key.
Great for generating things like API keys and compact IDs.
Installation
npm install uuid-key-generator --save
Usage
var KeyGenerator = require('uuid-key-generator');
var keygen = new KeyGenerator(); // Default is a 128-bit key encoded in base58
keygen.generateKey();
// -> '4QhmRwHwwrgFqXULXNtx4d'
var keygen2 = new KeyGenerator(256, KeyGenerator.BASE62);
keygen2.generateKey();
// -> 'x6GCX3aq9hIT8gjhvO96ObYj0W5HBVTsj64eqCuVc5X'
API
Object
new KeyGenerator([bitSize][, baseEncoding]) ⇒ Creates a new KeyGenerator instance that generates bitSize
-bit keys encoded using the characters in baseEncoding
.
Param | Default | Type | Description |
---|---|---|---|
[bitSize] | 128 |
number | The size of the key to generate in bits. Must be a multiple of 128. |
[baseEncoding] | KeyGenerator.BASE58 |
string | One of the KeyGenerator.BASE## constants or a custom string of characters to use to encode the key. |
Example
new KeyGenerator();
new KeyGenerator(256);
new KeyGenerator(KeyGenerator.BASE36);
new KeyGenerator(512, KeyGenerator.BASE62);
new KeyGenerator('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'); // Custom encoding (base64)
String
KeyGenerator.BASE16 : 0123456789abcdef
String
KeyGenerator.BASE36 : 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
String
KeyGenerator.BASE58 : 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
String
KeyGenerator.BASE62 : 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
String
KeyGenerator.BASE71 : 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!'()*-._~
(all ASCII characters that are not escaped by encodeURIComponent()
)
String
keygen.generateKey() ⇒ Generates a random key.
Returns: String
- A random key that is always keygen.keyLength
characters long.
Example
var keygen = new KeyGenerator();
keygen.generateKey();
// -> 'vf5NrETkUKCa6FhkyRSazD'
Number
(readonly) keygen.bitSize : The size of the key that will be generated in bits (the bitSize
value passed to the KeyGenerator
constructor).
String
(readonly) keygen.baseEncoding : The set of characters used to encode the key (the baseEncoding
value passed to the KeyGenerator
constructor).
Number
(readonly) keygen.base : The base of the key that will be generated (which is the number of characters in the baseEncoding
).
Number
(readonly) keygen.keyLength : The length of the key that will be generated. The generated key will always be this length.
Calculated as such: keyLength = Math.ceil(bitSize / Math.log2(base))