Comparing version 1.0.0 to 1.0.1
'use strict'; | ||
const { randomBytes } = require('crypto'); | ||
var _rbi; | ||
var _bb; | ||
if (globalThis && globalThis.crypto && globalThis.crypto.getRandomValues) { | ||
_rbi = function(bits) { | ||
if (bits < 1) { | ||
return 0n; | ||
} | ||
let bytes = bits >> 3; | ||
let rest = bits & 7; | ||
let mask; | ||
if (rest) { | ||
mask = 255 >> (8 - rest) | ||
bytes++; | ||
} else { | ||
mask = 255; | ||
} | ||
let b = globalThis.crypto.getRandomValues(new Uint8Array(bytes)); | ||
b[0] &= mask; | ||
return BigInt('0x' + b.reduce(function(a, x) { return a + x.toString(16).padStart(2, '0'); }, '')); | ||
} | ||
} else { | ||
let { randomBytes } = require('crypto'); | ||
_rbi = function(bits) { | ||
if (bits < 1) { | ||
return 0n; | ||
} | ||
let bytes = bits >> 3; | ||
let rest = bits & 7; | ||
let mask; | ||
if (rest) { | ||
mask = 255 >> (8 - rest) | ||
bytes++; | ||
} else { | ||
mask = 255; | ||
} | ||
let b = randomBytes(bytes); | ||
b[0] &= mask; | ||
return BigInt('0x' + b.toString('hex')); | ||
} | ||
} | ||
const _bigIntBitsCalc = 0; | ||
switch (_bigIntBitsCalc) { | ||
case 1: | ||
_bb = function(n) { | ||
let b = 0; | ||
while (n > 0n) { | ||
if (n >= 0x10000000000000000000000000000000000000000000000000000000000000000n) { | ||
n >>= 256n; | ||
b += 256; | ||
} else if (n >= 0x100000000000000000000000000000000n) { | ||
n >>= 128n; | ||
b += 128; | ||
} else if (n >= 0x10000000000000000n) { | ||
n >>= 64n; | ||
b += 64; | ||
} else if (n >= 0x100000000n) { | ||
n >>= 32n; | ||
b += 32; | ||
} else if (n >= 0x10000n) { | ||
n >>= 16n; | ||
b += 16; | ||
} else if (n >= 0x100n) { | ||
n >>= 8n; | ||
b += 8; | ||
} else if (n >= 0x10n) { | ||
n >>= 4n; | ||
b += 4; | ||
} else { | ||
n >>= 1n; | ||
b++; | ||
} | ||
} | ||
return b; | ||
} | ||
break; | ||
case 2: | ||
_bb = function(n) { | ||
let b = 0; | ||
while (n > 0n) { | ||
n >>= 1n; | ||
b++; | ||
} | ||
return b; | ||
} | ||
break; | ||
default: | ||
_bb = function(n) { | ||
if (n < 1n) { | ||
return 0; | ||
} | ||
return n.toString(2).length; | ||
} | ||
break; | ||
} | ||
if (1) { | ||
} else { | ||
} | ||
function _bigintify(n) { | ||
@@ -34,14 +134,5 @@ switch (typeof(n)) { | ||
n--; | ||
let r, h = n.toString(16); | ||
if (h.length % 2) { | ||
h = '0' + h; | ||
} | ||
let l = h.length / 2, p = 1; | ||
for (let m = parseInt('0x' + h.slice(0, 2)); p < m; p = (p << 1) | 1) { | ||
/*NOTHING*/ | ||
} | ||
let r, bits = _bb(n); | ||
do { | ||
let b = randomBytes(l); | ||
b[0] &= p; | ||
r = BigInt('0x' + b.toString('hex')); | ||
r = _rbi(bits); | ||
} while(r > n); | ||
@@ -48,0 +139,0 @@ return r; |
{ | ||
"name": "bigint-rnd", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Unbiased and crytpgraplically safe library for generating random BigInt", | ||
@@ -19,5 +19,8 @@ "main": "bigint-rnd.js", | ||
"keywords": [ | ||
"encryption", | ||
"cipher", | ||
"bitwise" | ||
"crypto", | ||
"random", | ||
"pseudorandom", | ||
"bigint", | ||
"secure", | ||
"unbiased" | ||
], | ||
@@ -24,0 +27,0 @@ "author": { |
@@ -53,1 +53,6 @@ 'use strict'; | ||
} | ||
for (let x = bigintRnd(10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n); | ||
x > 0n; | ||
x = bigintRnd(x)) { | ||
} |
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
10013
186