uuid-token-generator
Advanced tools
Comparing version 0.5.0 to 1.0.0
126
index.js
'use strict'; | ||
var uuid = require('node-uuid'); | ||
const uuid = require('uuid'); | ||
// Polyfill Math.log2() if necessary | ||
Math.log2 = Math.log2 || function(x) { | ||
return Math.log(x) / Math.log(2); | ||
}; | ||
class TokenGenerator { | ||
constructor(bitSize, baseEncoding) { | ||
if (typeof bitSize === 'string') { | ||
baseEncoding = bitSize; | ||
bitSize = null; | ||
} | ||
function TokenGenerator(bitSize, baseEncoding) { | ||
if (typeof bitSize === 'string') { | ||
baseEncoding = bitSize; | ||
bitSize = null; | ||
} | ||
bitSize = bitSize || 128; | ||
baseEncoding = baseEncoding || TokenGenerator.BASE58; | ||
bitSize = bitSize || 128; | ||
baseEncoding = baseEncoding || TokenGenerator.BASE58; | ||
if (bitSize % 128 !== 0 || bitSize < 0) { | ||
throw new Error('bitSize must be a positive integer that is a multiple of 128'); | ||
} | ||
if (typeof baseEncoding !== 'string') { | ||
throw new Error('baseEncoding must be a string'); | ||
} | ||
if (bitSize % 128 !== 0 || bitSize < 0) { | ||
throw new Error('bitSize must be a positive integer that is a multiple of 128'); | ||
this.bitSize = bitSize; | ||
this.baseEncoding = baseEncoding; | ||
this.base = baseEncoding.length; | ||
this.tokenLength = Math.ceil(bitSize / Math.log2(this.base)); | ||
this._bytes = bitSize / 8; | ||
} | ||
if (typeof baseEncoding !== 'string') { | ||
throw new Error('baseEncoding must be a string'); | ||
} | ||
this.bitSize = bitSize; | ||
this.baseEncoding = baseEncoding; | ||
this.base = baseEncoding.length; | ||
this.tokenLength = Math.ceil(bitSize / Math.log2(this.base)); | ||
generate() { | ||
const buffer = Buffer.allocUnsafe(this._bytes); | ||
var i; | ||
this._bytes = bitSize / 8; | ||
} | ||
for (i = 0; i < this._bytes; i += 16) { | ||
uuid.v4(null, buffer, i); | ||
} | ||
TokenGenerator.BASE16 = '0123456789abcdef'; | ||
TokenGenerator.BASE36 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
TokenGenerator.BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | ||
TokenGenerator.BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | ||
TokenGenerator.BASE66 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~'; | ||
TokenGenerator.BASE71 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!'()*-._~"; | ||
if (this.baseEncoding === TokenGenerator.BASE16) { | ||
return buffer.toString('hex'); | ||
} | ||
TokenGenerator.prototype.generate = function() { | ||
var buffer = new Buffer(this._bytes); | ||
var digits = [0]; | ||
var i; | ||
var j; | ||
const digits = [0]; | ||
for (i = 0; i < this._bytes; i += 16) { | ||
uuid.v4(null, buffer, i); | ||
} | ||
for (i = 0; i < buffer.length; ++i) { | ||
var carry = buffer[i]; | ||
if (this.baseEncoding === TokenGenerator.BASE16) { | ||
return buffer.toString('hex'); | ||
} | ||
for (var j = 0; j < digits.length; ++j) { | ||
carry += digits[j] << 8; | ||
digits[j] = carry % this.base; | ||
carry = (carry / this.base) | 0; | ||
} | ||
for (i = 0; i < buffer.length; i++) { | ||
for (j = 0; j < digits.length; j++) { | ||
digits[j] <<= 8; | ||
while (carry > 0) { | ||
digits.push(carry % this.base); | ||
carry = (carry / this.base) | 0; | ||
} | ||
} | ||
digits[0] += buffer[i]; | ||
var token = digits.length < this.tokenLength | ||
? this.baseEncoding[0].repeat(this.tokenLength - digits.length) // Leading zeros | ||
: ''; | ||
var carry = 0; | ||
for (j = 0; j < digits.length; j++) { | ||
digits[j] += carry; | ||
carry = digits[j] / this.base | 0; | ||
digits[j] %= this.base; | ||
} | ||
i = digits.length; | ||
while (carry) { | ||
digits.push(carry % this.base); | ||
carry = carry / this.base | 0; | ||
while (i--) { | ||
token += this.baseEncoding[digits[i]]; | ||
} | ||
} | ||
// Deal with leading zeros | ||
for (i = 0; buffer[i] === 0 && i < buffer.length - 1; i++) { | ||
digits.push(0); | ||
return token; | ||
} | ||
} | ||
// Fill with random numbers to get the full token length | ||
while (digits.length < this.tokenLength) { | ||
digits.push(this.base * Math.random() | 0); | ||
} | ||
TokenGenerator.BASE16 = '0123456789abcdef'; | ||
TokenGenerator.BASE36 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
TokenGenerator.BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | ||
TokenGenerator.BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | ||
TokenGenerator.BASE66 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~'; | ||
TokenGenerator.BASE71 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!'()*-._~"; | ||
// Convert digits to a string | ||
var token = ''; | ||
for (i = 0; i < digits.length; i++) { | ||
token += this.baseEncoding[digits[i]]; | ||
} | ||
return token; | ||
}; | ||
module.exports = TokenGenerator; |
{ | ||
"name": "uuid-token-generator", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"description": "Generates random tokens with custom size and base-encoding using the RFC 4122 v4 UUID algorithm", | ||
@@ -12,2 +12,5 @@ "homepage": "https://github.com/nwoltman/uuid-token-generator", | ||
], | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"repository": { | ||
@@ -32,3 +35,3 @@ "type": "git", | ||
"dependencies": { | ||
"node-uuid": "^1.4.7" | ||
"uuid": "^3.0.0" | ||
}, | ||
@@ -38,10 +41,10 @@ "devDependencies": { | ||
"grunt": "^1.0.1", | ||
"grunt-eslint": "^18.0.0", | ||
"grunt-eslint": "^19.0.0", | ||
"grunt-mocha-istanbul": "^5.0.1", | ||
"grunt-mocha-test": "^0.12.7", | ||
"grunt-mocha-test": "^0.13.2", | ||
"istanbul": "^0.4.3", | ||
"jit-grunt": "^0.10.0", | ||
"mocha": "^2.4.5", | ||
"should": "^9.0.0" | ||
"mocha": "^3.1.2", | ||
"should": "^11.1.1" | ||
} | ||
} |
@@ -13,3 +13,17 @@ # UUID Token Generator | ||
--- | ||
### WARNING | ||
--- | ||
**This package is no longer being maintained because a better one exists—[uid-generator](https://www.npmjs.com/package/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 | ||
@@ -25,9 +39,9 @@ | ||
```js | ||
var TokenGenerator = require('uuid-token-generator'); | ||
const TokenGenerator = require('uuid-token-generator'); | ||
var tokgen = new TokenGenerator(); // Default is a 128-bit token encoded in base58 | ||
const tokgen = new TokenGenerator(); // Default is a 128-bit token encoded in base58 | ||
tokgen.generate(); | ||
// -> '4QhmRwHwwrgFqXULXNtx4d' | ||
var tokgen2 = new TokenGenerator(256, TokenGenerator.BASE62); | ||
const tokgen2 = new TokenGenerator(256, TokenGenerator.BASE62); | ||
tokgen2.generate(); | ||
@@ -44,5 +58,5 @@ // -> 'x6GCX3aq9hIT8gjhvO96ObYj0W5HBVTsj64eqCuVc5X' | ||
| 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. | | ||
|:------|:--------|:-----|:------------| | ||
| [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. | | ||
@@ -91,3 +105,3 @@ **Example** | ||
```js | ||
var tokgen = new TokenGenerator(); | ||
const tokgen = new TokenGenerator(); | ||
tokgen.generate(); | ||
@@ -94,0 +108,0 @@ // -> 'vf5NrETkUKCa6FhkyRSazD' |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9484
1
146
61
+ Addeduuid@^3.0.0
+ Addeduuid@3.4.0(transitive)
- Removednode-uuid@^1.4.7
- Removednode-uuid@1.4.8(transitive)