Comparing version 1.0.7 to 1.0.8
@@ -1,26 +0,104 @@ | ||
var os = require('os') | ||
var crypto = require('webcrypto') | ||
const os = require('os') | ||
const crypto = require('crypto') | ||
var toString = Object.prototype.toString | ||
const toString = Object.prototype.toString | ||
let ABC = [ | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' | ||
const ABC_CLEAN = [ | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I', | ||
'J', | ||
'K', | ||
'L', | ||
'M', | ||
'N', | ||
'O', | ||
'P', | ||
'Q', | ||
'R', | ||
'S', | ||
'T', | ||
'U', | ||
'V', | ||
'W', | ||
'X', | ||
'Y', | ||
'Z', | ||
] | ||
let ABC_WITH_NUM = [ | ||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' | ||
].concat( ABC ) | ||
const ABC = [ | ||
'a', | ||
'b', | ||
'c', | ||
'd', | ||
'e', | ||
'f', | ||
'g', | ||
'h', | ||
'i', | ||
'j', | ||
'k', | ||
'l', | ||
'm', | ||
'n', | ||
'o', | ||
'p', | ||
'q', | ||
'r', | ||
's', | ||
't', | ||
'u', | ||
'v', | ||
'w', | ||
'x', | ||
'y', | ||
'z', | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I', | ||
'J', | ||
'K', | ||
'L', | ||
'M', | ||
'N', | ||
'O', | ||
'P', | ||
'Q', | ||
'R', | ||
'S', | ||
'T', | ||
'U', | ||
'V', | ||
'W', | ||
'X', | ||
'Y', | ||
'Z', | ||
] | ||
function isNumber (obj) { | ||
return (toString.call(obj) === '[object ' + Number + ']') || !isNaN(obj) | ||
const NUMS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] | ||
const ABC_WITH_NUM = [].concat(NUMS).concat(ABC) | ||
function isNumber(obj) { | ||
return toString.call(obj) === '[object ' + Number + ']' || !isNaN(obj) | ||
} | ||
function minmax (value, min, max) { | ||
return value < min ? min : (value > max ? max : value) | ||
function minmax(value, min, max) { | ||
return value < min ? min : value > max ? max : value | ||
} | ||
function createConfig (length, useSystemRefs) { | ||
if ( length < 4 || length > 256 ) | ||
throw new Error('Length must be a number between 4 and 256.') | ||
function createConfig(length, useSystemRefs) { | ||
if (length < 4 || length > 256) throw new Error('Length must be a number between 4 and 256.') | ||
@@ -30,6 +108,7 @@ var config = { | ||
networkLength: length > 16 && useSystemRefs ? 2 : 0, | ||
timeLength: minmax( Math.floor( length / 8 ), 2, 8 ), | ||
useSystemRefs: !!useSystemRefs | ||
timeLength: minmax(Math.floor(length / 8), 2, 8), | ||
useSystemRefs: !!useSystemRefs, | ||
} | ||
config.randomLength = length - config.processLength - config.networkLength - 2 * config.timeLength | ||
config.randomLength = | ||
length - config.processLength - config.networkLength - 2 * config.timeLength | ||
@@ -39,6 +118,10 @@ return config | ||
function getNetworkAddress (networkInterfaces) { | ||
function getNetworkAddress(networkInterfaces) { | ||
for (var i in networkInterfaces) { | ||
for (var v in i) { | ||
if (networkInterfaces[i][v] && networkInterfaces[i][v].address && !networkInterfaces[i][v].internal) { | ||
if ( | ||
networkInterfaces[i][v] && | ||
networkInterfaces[i][v].address && | ||
!networkInterfaces[i][v].internal | ||
) { | ||
return networkInterfaces[i][v].address | ||
@@ -52,119 +135,171 @@ } | ||
var DEFAULT_LENGTH = 32 | ||
var Clerobee = function Clerobee ( length, useSystemRefs ) { | ||
this.config = createConfig( length || DEFAULT_LENGTH, useSystemRefs ) | ||
var Clerobee = function Clerobee(length, useSystemRefs) { | ||
this.config = createConfig(length || DEFAULT_LENGTH, useSystemRefs) | ||
} | ||
var clerobee = Clerobee.prototype | ||
clerobee.generateNumericString = function ( length ) { | ||
return (Math.random() + '').split('.')[1].substring(0, length) | ||
function pushIfNotIncluded(array, ...elements) { | ||
for (let element of elements) { | ||
if (!array.includes(element)) array.push(element) | ||
} | ||
return array | ||
} | ||
clerobee.generateAbcString = function ( length, options = {} ) { | ||
let str = options.prefix || '' | ||
for (let i = 0; i < length; ++i) | ||
str += ABC[ Math.floor(Math.random() * ABC.length) ] | ||
return options.upperCaseOnly ? str.toUpperCase() : str | ||
} | ||
clerobee.generateAbcNumString = function ( length, options = {} ) { | ||
let str = options.prefix || '' | ||
for (let i = 0; i < length; ++i) | ||
str += ABC_WITH_NUM[ Math.floor(Math.random() * ABC.length) ] | ||
return options.upperCaseOnly ? str.toUpperCase() : str | ||
} | ||
Object.assign(Clerobee.prototype, { | ||
ABC_CLEAN, | ||
ABC, | ||
NUMS, | ||
ABC_WITH_NUM, | ||
clerobee.getRandom = function ( randomLength ) { | ||
return crypto.randomBytes( (randomLength || this.config.randomLength) / 2 ).toString('hex') | ||
} | ||
rand(limit) { | ||
return Math.floor(Math.random() * limit) | ||
}, | ||
clerobee.padHex = function (hex, digits) { | ||
return hex.length >= digits ? hex.substring(0, digits) : this.getRandom(digits - hex.length) + hex | ||
} | ||
randomElement(array) { | ||
if (!array || !Array.isArray(array)) throw Error(`Invalid array argument: ${array}`) | ||
clerobee.getSliceRandom = function ( config, uuid ) { | ||
var lengthP = config.timeLength + config.processLength + config.networkLength | ||
var lengthR = lengthP + config.randomLength | ||
return array[this.rand(array.length)] | ||
}, | ||
return uuid.substring( lengthP, lengthR ) | ||
} | ||
randomElements(array, minNumber, maxNumber) { | ||
const elements = [] | ||
const count = this.rand(Math.abs(Math.floor(maxNumber - minNumber)) + 1) | ||
clerobee.cryptify = function ( string, length ) { | ||
return crypto.createHash('md5') | ||
.update( string, 'utf8') | ||
.digest('hex') | ||
.slice( -length ) | ||
} | ||
let maxTry = 1000 | ||
while (maxTry-- > 0 && elements.length < count) { | ||
pushIfNotIncluded(elements, this.randomElement(array)) | ||
} | ||
clerobee.generateByLength = function ( length ) { | ||
var config = length ? createConfig( length, this.config.useSystemRefs ) : this.config | ||
if ( !config.useSystemRefs ) | ||
return this.getTimestamp( config.timeLength ) + this.getRandom( config.randomLength ) + this.getNanoTime( config.timeLength ) | ||
else | ||
return this.getTimestamp( config.timeLength ) + this.getProcessId( config.processLength ) + this.getNetworkId( config.networkLength ) + this.getRandom( config.randomLength ) + this.getNanoTime( config.timeLength ) | ||
} | ||
return elements | ||
}, | ||
clerobee.generateBySource = function ( reference, length ) { | ||
var self = this | ||
var config = length ? createConfig( length, this.config.useSystemRefs ) : this.config | ||
var refString = this.cryptify( JSON.stringify( reference ), config.randomLength ) | ||
generateNumericString(length) { | ||
return (Math.random() + '').split('.')[1].substring(0, length) | ||
}, | ||
if ( !config.useSystemRefs ) | ||
return this.getTimestamp( config.timeLength ) + self.padHex(refString, config.randomLength) + this.getNanoTime( config.timeLength ) | ||
else | ||
return this.getTimestamp( config.timeLength ) + this.getProcessId( config.processLength ) + this.getNetworkId( config.networkLength ) + self.padHex(refString, config.randomLength) + this.getNanoTime( config.timeLength ) | ||
} | ||
generateAbcString(length, options = {}) { | ||
let str = options.prefix || '' | ||
for (let i = 0; i < length; ++i) str += this.randomElement(ABC) | ||
return options.upperCaseOnly ? str.toUpperCase() : str | ||
}, | ||
generateAbcNumString(length, options = {}) { | ||
let str = options.prefix || '' | ||
for (let i = 0; i < length; ++i) str += this.randomElement(ABC_WITH_NUM) | ||
return options.upperCaseOnly ? str.toUpperCase() : str | ||
}, | ||
clerobee.generate = function ( basis, extra ) { | ||
return !basis || isNumber(basis) ? this.generateByLength(basis) : this.generateBySource(basis, extra) | ||
} | ||
getRandom(randomLength) { | ||
return crypto.randomBytes((randomLength || this.config.randomLength) / 2).toString('hex') | ||
}, | ||
clerobee.isSourced = function ( reference, length, uuid ) { | ||
if ( !reference || !length || !uuid ) | ||
return false | ||
padHex(hex, digits) { | ||
return hex.length >= digits | ||
? hex.substring(0, digits) | ||
: this.getRandom(digits - hex.length) + hex | ||
}, | ||
var config = createConfig( length, this.config.useSystemRefs ) | ||
getSliceRandom(config, uuid) { | ||
var lengthP = config.timeLength + config.processLength + config.networkLength | ||
var lengthR = lengthP + config.randomLength | ||
var refString = this.cryptify( JSON.stringify( reference ), config.randomLength ) | ||
return uuid.substring(lengthP, lengthR) | ||
}, | ||
var refRandom = this.getSliceRandom(config, uuid) | ||
cryptify(string, length) { | ||
return crypto.createHash('md5').update(string, 'utf8').digest('hex').slice(-length) | ||
}, | ||
return refRandom.substring(refRandom.length - refString.length) === refString | ||
} | ||
generateByLength(length) { | ||
var config = length ? createConfig(length, this.config.useSystemRefs) : this.config | ||
if (!config.useSystemRefs) | ||
return ( | ||
this.getTimestamp(config.timeLength) + | ||
this.getRandom(config.randomLength) + | ||
this.getNanoTime(config.timeLength) | ||
) | ||
else | ||
return ( | ||
this.getTimestamp(config.timeLength) + | ||
this.getProcessId(config.processLength) + | ||
this.getNetworkId(config.networkLength) + | ||
this.getRandom(config.randomLength) + | ||
this.getNanoTime(config.timeLength) | ||
) | ||
}, | ||
clerobee.getProcessId = function ( processLength ) { | ||
processLength = processLength || this.config.processLength | ||
generateBySource(reference, length) { | ||
var self = this | ||
var config = length ? createConfig(length, this.config.useSystemRefs) : this.config | ||
var refString = this.cryptify(JSON.stringify(reference), config.randomLength) | ||
var pid = (process && process.pid) ? process.pid : Date.now() | ||
if (!config.useSystemRefs) | ||
return ( | ||
this.getTimestamp(config.timeLength) + | ||
self.padHex(refString, config.randomLength) + | ||
this.getNanoTime(config.timeLength) | ||
) | ||
else | ||
return ( | ||
this.getTimestamp(config.timeLength) + | ||
this.getProcessId(config.processLength) + | ||
this.getNetworkId(config.networkLength) + | ||
self.padHex(refString, config.randomLength) + | ||
this.getNanoTime(config.timeLength) | ||
) | ||
}, | ||
return this.toBase36String( pid, processLength ) | ||
} | ||
generate(basis, extra) { | ||
return !basis || isNumber(basis) | ||
? this.generateByLength(basis) | ||
: this.generateBySource(basis, extra) | ||
}, | ||
clerobee.getNetworkId = function ( networkLength ) { | ||
networkLength = networkLength || this.config.networkLength | ||
isSourced(reference, length, uuid) { | ||
if (!reference || !length || !uuid) return false | ||
return this.cryptify( getNetworkAddress( os.networkInterfaces() ), networkLength ) | ||
} | ||
var config = createConfig(length, this.config.useSystemRefs) | ||
clerobee.getTimestamp = function ( timeLength ) { | ||
timeLength = timeLength || this.config - timeLength | ||
var refString = this.cryptify(JSON.stringify(reference), config.randomLength) | ||
return this.toBase36String( Date.now(), timeLength ) | ||
} | ||
var refRandom = this.getSliceRandom(config, uuid) | ||
clerobee.getNanoTime = function ( timeLength ) { | ||
timeLength = timeLength || this.config.timeLength | ||
return refRandom.substring(refRandom.length - refString.length) === refString | ||
}, | ||
var time = (process && process.hrtime) ? process.hrtime() : [1, Date.now()] | ||
var nano = time[0] * 1e9 + time[1] | ||
getProcessId(processLength) { | ||
processLength = processLength || this.config.processLength | ||
// return this.toBase36String( nano, timeLength ) | ||
return this.toBase36String( (nano + '').split('').reverse().join(''), timeLength ) | ||
} | ||
var pid = process && process.pid ? process.pid : Date.now() | ||
clerobee.toBase36String = function (value, length) { | ||
value = value || 0 | ||
length = length || 4 | ||
return this.padHex( Number( value ).toString(16), length) | ||
} | ||
return this.toBase36String(pid, processLength) | ||
}, | ||
getNetworkId(networkLength) { | ||
networkLength = networkLength || this.config.networkLength | ||
return this.cryptify(getNetworkAddress(os.networkInterfaces()), networkLength) | ||
}, | ||
getTimestamp(timeLength) { | ||
timeLength = timeLength || this.config - timeLength | ||
return this.toBase36String(Date.now(), timeLength) | ||
}, | ||
getNanoTime(timeLength) { | ||
timeLength = timeLength || this.config.timeLength | ||
var time = process && process.hrtime ? process.hrtime() : [1, Date.now()] | ||
var nano = time[0] * 1e9 + time[1] | ||
// return this.toBase36String( nano, timeLength ) | ||
return this.toBase36String((nano + '').split('').reverse().join(''), timeLength) | ||
}, | ||
toBase36String(value, length) { | ||
value = value || 0 | ||
length = length || 4 | ||
return this.padHex(Number(value).toString(16), length) | ||
}, | ||
}) | ||
module.exports = exports = Clerobee |
{ | ||
"name": "clerobee", | ||
"version": "1.0.7", | ||
"description": "A featureful dependency-free UID generator", | ||
"keywords": [ | ||
"uid", | ||
"generator", | ||
"oid", | ||
"uid", | ||
"uuid", | ||
"uniqueid", | ||
"unique id", | ||
"primary key", | ||
"license key", | ||
"distributed environment", | ||
"security" | ||
], | ||
"homepage": "https://github.com/imrefazekas/clerobee", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/imrefazekas/clerobee.git" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/imrefazekas/clerobee/issues" | ||
}, | ||
"author": { | ||
"name": "Imre Fazekas", | ||
"email": "mail@imrefazekas.me" | ||
}, | ||
"license": "MIT", | ||
"main": "./lib/clerobee.js", | ||
"dependencies": { | ||
"webcrypto": "^0.1.1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.3.7" | ||
}, | ||
"engines": { | ||
"node": ">= 8.0.0" | ||
} | ||
"name": "clerobee", | ||
"version": "1.0.8", | ||
"description": "A featureful dependency-free UID generator", | ||
"keywords": [ | ||
"uid", | ||
"generator", | ||
"oid", | ||
"uid", | ||
"uuid", | ||
"uniqueid", | ||
"unique id", | ||
"primary key", | ||
"license key", | ||
"distributed environment", | ||
"security" | ||
], | ||
"homepage": "https://github.com/imrefazekas/clerobee", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/imrefazekas/clerobee.git" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/imrefazekas/clerobee/issues" | ||
}, | ||
"author": { | ||
"name": "Imre Fazekas", | ||
"email": "mail@imrefazekas.me" | ||
}, | ||
"license": "MIT", | ||
"main": "./lib/clerobee.js", | ||
"devDependencies": { | ||
"chai": "^4.5.0", | ||
"eslint": "^9.6.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-prettier": "^5.1.3", | ||
"prettier": "^3.3.3", | ||
"prettier-plugin-packagejson": "^2.5.1" | ||
}, | ||
"engines": { | ||
"node": ">= 16.0.0" | ||
} | ||
} |
@@ -1,5 +0,5 @@ | ||
var crypto = require('webcrypto') | ||
var crypto = require('crypto') | ||
var Cerobee = require('../lib/clerobee') | ||
var cerobee = new Cerobee( 256 ) | ||
var cerobee = new Cerobee(256) | ||
@@ -10,21 +10,20 @@ let chai = require('chai') | ||
describe('clerobee', function () { | ||
var reference | ||
before(function (done) { | ||
console.log('------', cerobee.getNanoTime(4) ) | ||
console.log('------', cerobee.getNanoTime(4) ) | ||
console.log('------', cerobee.getNanoTime(4) ) | ||
console.log('------', cerobee.getNanoTime(4) ) | ||
console.log('------', cerobee.getNanoTime(4) ) | ||
console.log('------', cerobee.getNanoTime(4)) | ||
console.log('------', cerobee.getNanoTime(4)) | ||
console.log('------', cerobee.getNanoTime(4)) | ||
console.log('------', cerobee.getNanoTime(4)) | ||
console.log('------', cerobee.getNanoTime(4)) | ||
console.log( '64::', cerobee.generate(64) ) | ||
console.log( '48::', cerobee.generate(48) ) | ||
console.log( '32::', cerobee.generate(32) ) | ||
console.log('64::', cerobee.generate(64)) | ||
console.log('48::', cerobee.generate(48)) | ||
console.log('32::', cerobee.generate(32)) | ||
console.log( 'AbcNum 8::', cerobee.generateAbcNumString(8) ) | ||
console.log('AbcNum 8::', cerobee.generateAbcNumString(8)) | ||
reference = { email: 'test@provider.org' } | ||
console.log( '\n Reference:', reference ) | ||
console.log('\n Reference:', reference) | ||
@@ -47,9 +46,9 @@ done() | ||
it('should be referred correctly', function (done) { | ||
var pID = cerobee.generate( reference, 128 ) | ||
var pID = cerobee.generate(reference, 128) | ||
pID.should.to.have.length(128) | ||
cerobee.isSourced( { email: 'tests@provi.org' }, 128, pID ).should.to.be.false | ||
cerobee.isSourced({ email: 'tests@provi.org' }, 128, pID).should.to.be.false | ||
cerobee.isSourced( reference, 128, pID ).should.to.be.true | ||
cerobee.isSourced(reference, 128, pID).should.to.be.true | ||
@@ -56,0 +55,0 @@ done() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
9
1892
277405
6
- Removedwebcrypto@^0.1.1
- Removedasn1.js@4.10.1(transitive)
- Removedbn.js@4.12.15.2.1(transitive)
- Removedbrorand@1.1.0(transitive)
- Removedbrowserify-aes@1.2.0(transitive)
- Removedbrowserify-cipher@1.0.1(transitive)
- Removedbrowserify-des@1.0.2(transitive)
- Removedbrowserify-rsa@4.1.1(transitive)
- Removedbrowserify-sign@4.2.3(transitive)
- Removedbuffer-xor@1.0.3(transitive)
- Removedcipher-base@1.0.6(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedcreate-ecdh@4.0.4(transitive)
- Removedcreate-hash@1.2.0(transitive)
- Removedcreate-hmac@1.1.7(transitive)
- Removedcrypto-browserify@3.12.1(transitive)
- Removeddes.js@1.1.0(transitive)
- Removeddetect-node@2.1.0(transitive)
- Removeddiffie-hellman@5.0.3(transitive)
- Removedelliptic@6.6.1(transitive)
- Removedevp_bytestokey@1.0.3(transitive)
- Removedhash-base@3.0.5(transitive)
- Removedhash.js@1.1.7(transitive)
- Removedhmac-drbg@1.0.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@1.0.0(transitive)
- Removedmd5.js@1.3.5(transitive)
- Removedmiller-rabin@4.0.1(transitive)
- Removedminimalistic-assert@1.0.1(transitive)
- Removedminimalistic-crypto-utils@1.0.1(transitive)
- Removedparse-asn1@5.1.7(transitive)
- Removedpbkdf2@3.1.2(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedpublic-encrypt@4.0.3(transitive)
- Removedrandombytes@2.1.0(transitive)
- Removedrandomfill@1.0.4(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedripemd160@2.0.2(transitive)
- Removedsafe-buffer@5.1.25.2.1(transitive)
- Removedsha.js@2.4.11(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwebcrypto@0.1.1(transitive)