![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
uuid-token-generator
Advanced tools
Generates random tokens with custom size and base-encoding using the RFC 4122 v4 UUID algorithm
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.
This package is no longer being maintained because a better one exists—uid-generator—which is better for the following reasons:
npm install uuid-token-generator --save
const TokenGenerator = require('uuid-token-generator');
const tokgen = new TokenGenerator(); // Default is a 128-bit token encoded in base58
tokgen.generate();
// -> '4QhmRwHwwrgFqXULXNtx4d'
const tokgen2 = new TokenGenerator(256, TokenGenerator.BASE62);
tokgen2.generate();
// -> 'x6GCX3aq9hIT8gjhvO96ObYj0W5HBVTsj64eqCuVc5X'
Object
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
new TokenGenerator();
new TokenGenerator(256);
new TokenGenerator(TokenGenerator.BASE36);
new TokenGenerator(512, TokenGenerator.BASE62);
new TokenGenerator('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'); // Custom encoding (base64)
String
0123456789abcdef
String
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
String
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
String
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
String
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~
(all ASCII characters that do not need to be encoded in a URI as specified by RFC 3986)
String
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!'()*-._~
(all ASCII characters that are not encoded by encodeURIComponent()
)
String
Generates a random token.
Returns: String
- A random token that is always tokgen.tokenLength
characters long.
Example
const tokgen = new TokenGenerator();
tokgen.generate();
// -> 'vf5NrETkUKCa6FhkyRSazD'
Number
The size of the token that will be generated in bits (the bitSize
value passed to the TokenGenerator
constructor).
Example
new TokenGenerator().bitSize // -> 128
new TokenGenerator(256).bitSize // -> 256
String
The set of characters used to encode the token (the baseEncoding
value passed to the TokenGenerator
constructor).
Example
new TokenGenerator().baseEncoding // -> '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
new TokenGenerator('abc').baseEncoding // -> 'abc'
Number
The base of the token that will be generated (which is the number of characters in the baseEncoding
).
Example
new TokenGenerator().base // -> 58
new TokenGenerator(TokenGenerator.BASE62).base // -> 62
new TokenGenerator('abc').base // -> 3
Number
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
new TokenGenerator().tokenLength // -> 22
new TokenGenerator(256, TokenGenerator.BASE62).tokenLength // -> 43
FAQs
Generates random tokens with custom size and base-encoding using the RFC 4122 v4 UUID algorithm
We found that uuid-token-generator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.