All Projects → nwoltman → node-uid-generator

nwoltman / node-uid-generator

Licence: MIT License
Generates cryptographically strong pseudo-random UIDs with custom size and base-encoding

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to node-uid-generator

x264
🎥 A safe x264 wrapper for Rust.
Stars: ✭ 35 (+66.67%)
Mutual labels:  encoding
AspNetCore.Weixin
An ASP.NET Core middleware for Wechat/Weixin message handling and apis. (微信公众平台/接口调用服务)
Stars: ✭ 24 (+14.29%)
Mutual labels:  token
jwtauth-plugin
JWTAuth Plugin for WinterCMS
Stars: ✭ 25 (+19.05%)
Mutual labels:  token
nginx-module-url
Nginx url encoding converting module
Stars: ✭ 17 (-19.05%)
Mutual labels:  encoding
ItroublveTSC
Official Source of ItroublveTSC, totally open source. No virus or anything. Feel free to have a look :)
Stars: ✭ 82 (+290.48%)
Mutual labels:  token
SublimeXssEncode
Converts characters from one encoding to another using a transformation.
Stars: ✭ 37 (+76.19%)
Mutual labels:  encoding
yii2-jwt-user
JWT (JSON Web Token) User component for Yii 2
Stars: ✭ 16 (-23.81%)
Mutual labels:  token
NEMPay
Adaptable Android & iOS Mosaic Wallet for NEM Blockchain
Stars: ✭ 36 (+71.43%)
Mutual labels:  token
tokensubscription.com
⏰💰🤠 Set-it-and-forget-it token subscriptions on the Ethereum mainnet. #Winner #WyoHackathon
Stars: ✭ 81 (+285.71%)
Mutual labels:  token
Zipangu
A library for compatibility about Japan.
Stars: ✭ 27 (+28.57%)
Mutual labels:  encoding
Crypto
封装多种CTF和平时常见加密及编码C#类库
Stars: ✭ 20 (-4.76%)
Mutual labels:  encoding
crowdsale-smart-contract
No description or website provided.
Stars: ✭ 39 (+85.71%)
Mutual labels:  token
EL1T3
🖤 Ƭ𝘩𝘦 𝘮𝘰𝘴𝘵 𝘱𝘰𝘸𝘦𝘳𝘧𝘶𝘭𝘭 𝘢𝘯𝘥 𝘉𝘦𝘵𝘵𝘦𝘳 𝘵𝘰𝘬𝘦𝘯 𝘴𝘵𝘦𝘢𝘭𝘦𝘳.
Stars: ✭ 41 (+95.24%)
Mutual labels:  token
cattonum
Encode Categorical Features
Stars: ✭ 31 (+47.62%)
Mutual labels:  encoding
jwt-token
Json web token generation and validation.
Stars: ✭ 14 (-33.33%)
Mutual labels:  token
VarintBitConverter
Varint encoding and decoding for .NET
Stars: ✭ 21 (+0%)
Mutual labels:  encoding
axios-token-interceptor
An interceptor which makes it easier to work with tokens in axios.
Stars: ✭ 34 (+61.9%)
Mutual labels:  token
reactjs-login-register-crud
ReactJS CRUD Application, ReactJS FileUpload, ReactJS Sample application, ReactJS Boilerplate, ReactJS Login, ReactJS FileUpload, ReactJS Register
Stars: ✭ 47 (+123.81%)
Mutual labels:  token
gulp-convert-encoding
Plugin for gulp to convert files from one encoding to another.
Stars: ✭ 15 (-28.57%)
Mutual labels:  encoding
tokenizr
String Tokenization Library for JavaScript
Stars: ✭ 70 (+233.33%)
Mutual labels:  token

uid-generator

NPM Version Build Status Coverage Status devDependency Status

Generates cryptographically strong, pseudo-random UIDs with custom size and base-encoding. Generated UIDs are strings that are guaranteed to always be the same length depending on the specified bit-size.

Great for generating things like compact API keys and UIDs that are safe to use in URLs and cookies.

Tip: The main benefit of this module is the ability to easily generate human-safe UIDs (using base58) and large UIDs that are more compact (using something like base94). If you’re just looking to generate URL-safe base64 IDs, the best package for that is uid-safe.

Installation

npm install uid-generator
# or
yarn add uid-generator

Usage

const UIDGenerator = require('uid-generator');
const uidgen = new UIDGenerator(); // Default is a 128-bit UID encoded in base58

// Async with `await`
await uidgen.generate(); // -> 'B1q2hUEKmeVp9zWepx9cnp'

// Async with promise
uidgen.generate()
  .then(uid => console.log(uid)); // -> 'PXmRJVrtzFAHsxjs7voD5R'

// Async with callback
uidgen.generate((err, uid) => {
  if (err) throw err;
  console.log(uid); // -> '4QhmRwHwwrgFqXULXNtx4d'
});

// Sync
uidgen.generateSync(); // -> '8Vw3bgbMMzeYfrQHQ8p3Jr'

API

new UIDGenerator([bitSize][, baseEncoding])

Creates a new UIDGenerator instance that generates bitSize-bit or uidLength-sized UIDs encoded using the characters in baseEncoding.

Param Type Default Description
[bitSize] number 128 The size of the UID to generate in bits. Must be a multiple of 8.
[baseEncoding] string UIDGenerator.BASE58 One of the UIDGenerator.BASE## constants or a custom string of characters to use to encode the UID.

Note: If a custom baseEncoding that has URL-unsafe characters is used, it is up to you to URL-encode the resulting UID.

Example

new UIDGenerator();
new UIDGenerator(256);
new UIDGenerator(UIDGenerator.BASE16);
new UIDGenerator(512, UIDGenerator.BASE62);
new UIDGenerator('01'); // Custom encoding (base2)

uidgen.generate([cb]) ⇒ ?Promise<string>

Asynchronously generates a UID.

Param Type Description
[cb] ?function(error, uid) An optional callback that will be called with the results of generating the UID.
If not specified, the function will return a promise.

Returns: ?Promise<string> - A promise that will resolve with the UID or reject with an error. Returns nothing if the cb parameter is specified.

async/await Example

const uidgen = new UIDGenerator();
// This must be inside an async function
const uid = await uidgen.generate();

Promise Example

const uidgen = new UIDGenerator();

uidgen.generate()
  .then(uid => {
    // Use uid here
  });

Callback Example

const uidgen = new UIDGenerator();

uidgen.generate((err, uid) => {
  if (err) throw err;
  // Use uid here
});

uidgen.generateSync() ⇒ string

Synchronously generates a UID.

Returns: string - The generated UID.

Example

const uidgen = new UIDGenerator();
const uid = uidgen.generateSync();

(readonly) uidgen.bitSize : number

The size of the UID that will be generated in bits (the bitSize value passed to the UIDGenerator constructor). If the uidLength parameter is passed to the constructor instead of bitSize, bitSize is calculated as follows:

bitSize = Math.ceil(length * Math.log2(base));

Example

new UIDGenerator().bitSize // -> 128
new UIDGenerator(256).bitSize // -> 256

(readonly) uidgen.uidLength : number

The length of the UID string that will be generated. The generated UID will always be this length. This will be the same as the uidLength parameter passed to the UIDGenerator constructor. If the uidLength parameter is not passed to the constructor, it will be calculated using the bitSize parameter as follows:

uidLength = Math.ceil(bitSize / Math.log2(base))

Example

new UIDGenerator().uidLength // -> 22
new UIDGenerator(256, UIDGenerator.BASE62).uidLength // -> 43

(readonly) uidgen.baseEncoding : string

The set of characters used to encode the UID string (the baseEncoding value passed to the UIDGenerator constructor).

Example

new UIDGenerator().baseEncoding // -> '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
new UIDGenerator(UIDGenerator.BASE16).baseEncoding // -> '0123456789abcdef'
new UIDGenerator('01').baseEncoding // -> '01'

(readonly) uidgen.base : number

The base of the UID that will be generated (which is the number of characters in the baseEncoding).

Example

new UIDGenerator().base // -> 58
new UIDGenerator(UIDGenerator.BASE16).base // -> 16
new UIDGenerator('01').base // -> 2

UIDGenerator.BASE16 : string

0123456789abcdef

UIDGenerator.BASE36 : string

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ

UIDGenerator.BASE58 : string

123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz

(all alphanumeric characters except for 0, O, I, and l — characters easily mistaken for each other)

The default base.

Tip: Use this base to create UIDs that are easy to type in manually.

UIDGenerator.BASE62 : string

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

UIDGenerator.BASE66 : string

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~

(all ASCII characters that do not need to be encoded in a URI as specified by RFC 3986)

UIDGenerator.BASE71 : string

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!'()*-._~

(all ASCII characters that are not encoded by encodeURIComponent())

UIDGenerator.BASE94 : string

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

(all readable ASCII characters)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].