UUID Token Generator
Provides a class that generates random tokens with custom size and base-encoding using the RFC 4122 v4 UUID algorithm. Generated tokens are strings that are guaranteed to always be the same length, depending on the bit-size specified for the token.
Great for generating things like API keys and compact UIDs.
WARNING
This package is no longer being maintained because a better one exists—uid-generator—which is better for the following reasons:
- It has more flexible token generation options
- i.e. You can specify the length of the token that you'd like to generate
- It has both a synchronous and asynchronous interface
- It is less likely to produce colliding tokens
- It's more performant
Installation
npm install uuid-token-generator --save
Usage
const TokenGenerator = ; const tokgen = ; // Default is a 128-bit token encoded in base58tokgen;// -> '4QhmRwHwwrgFqXULXNtx4d' const tokgen2 = 256 TokenGeneratorBASE62;tokgen2;// -> 'x6GCX3aq9hIT8gjhvO96ObYj0W5HBVTsj64eqCuVc5X'
API
Object
new TokenGenerator([bitSize][, baseEncoding]) ⇒ Creates a new TokenGenerator instance that generates bitSize
-bit tokens encoded using the characters in baseEncoding
.
Param | Default | Type | Description |
---|---|---|---|
[bitSize] | 128 |
number | The size of the token to generate in bits. Must be a multiple of 128. |
[baseEncoding] | TokenGenerator.BASE58 |
string | One of the TokenGenerator.BASE## constants or a custom string of characters to use to encode the token. |
Example
;256;TokenGeneratorBASE36;512 TokenGeneratorBASE62;'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'; // Custom encoding (base64)
String
TokenGenerator.BASE16 : 0123456789abcdef
String
TokenGenerator.BASE36 : 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
String
TokenGenerator.BASE58 : 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
String
TokenGenerator.BASE62 : 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
String
TokenGenerator.BASE66 : 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~
(all ASCII characters that do not need to be encoded in a URI as specified by RFC 3986)
String
TokenGenerator.BASE71 : 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!'()*-._~
(all ASCII characters that are not encoded by encodeURIComponent()
)
String
tokgen.generate() ⇒ Generates a random token.
Returns: String
- A random token that is always tokgen.tokenLength
characters long.
Example
const tokgen = ;tokgen;// -> 'vf5NrETkUKCa6FhkyRSazD'
Number
(readonly) tokgen.bitSize : The size of the token that will be generated in bits (the bitSize
value passed to the TokenGenerator
constructor).
Example
bitSize // -> 128256bitSize // -> 256
String
(readonly) tokgen.baseEncoding : The set of characters used to encode the token (the baseEncoding
value passed to the TokenGenerator
constructor).
Example
baseEncoding // -> '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz''abc'baseEncoding // -> 'abc'
Number
(readonly) tokgen.base : The base of the token that will be generated (which is the number of characters in the baseEncoding
).
Example
base // -> 58TokenGeneratorBASE62base // -> 62'abc'base // -> 3
Number
(readonly) tokgen.tokenLength : The length of the token that will be generated. The generated token will always be this length.
Calculated as such: tokenLength = Math.ceil(bitSize / Math.log2(base))
Example
tokenLength // -> 22256 TokenGeneratorBASE62tokenLength // -> 43