fixed browser tests
This commit is contained in:
parent
2179954b1e
commit
04cad8250b
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -137,7 +137,7 @@ function primeSync (bitLength, iterations = 16) {
|
||||||
* @returns {bigint} A cryptographically secure random bigint between [min,max]
|
* @returns {bigint} A cryptographically secure random bigint between [min,max]
|
||||||
*/
|
*/
|
||||||
function randBetween (max, min = 1n) {
|
function randBetween (max, min = 1n) {
|
||||||
if (max <= 0n || min < 0n || max <= min) throw new Error('inputs should be max > 0, min >= 0; max > min')
|
if (max <= 0n || min < 0n || max <= min) throw new RangeError('inputs should be max > 0, min >= 0; max > min')
|
||||||
const interval = max - min
|
const interval = max - min
|
||||||
const bitLen = bitLength(interval)
|
const bitLen = bitLength(interval)
|
||||||
let rnd
|
let rnd
|
||||||
|
@ -156,15 +156,14 @@ function randBetween (max, min = 1n) {
|
||||||
*
|
*
|
||||||
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
||||||
*/
|
*/
|
||||||
async function randBits (bitLength, forceLength = false) {
|
function randBits (bitLength, forceLength = false) {
|
||||||
if (bitLength < 1) {
|
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
|
||||||
throw new RangeError('bitLength MUST be > 0')
|
|
||||||
}
|
|
||||||
|
|
||||||
const byteLength = Math.ceil(bitLength / 8)
|
const byteLength = Math.ceil(bitLength / 8)
|
||||||
const bitLengthMod8 = bitLength % 8
|
const bitLengthMod8 = bitLength % 8
|
||||||
|
|
||||||
const rndBytes = await randBytes(byteLength, false)
|
return new Promise((resolve) => {
|
||||||
|
randBytes(byteLength, false).then(function (rndBytes) {
|
||||||
if (bitLengthMod8) {
|
if (bitLengthMod8) {
|
||||||
// Fill with 0's the extra bits
|
// Fill with 0's the extra bits
|
||||||
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
|
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
|
||||||
|
@ -173,7 +172,9 @@ async function randBits (bitLength, forceLength = false) {
|
||||||
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
|
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
|
||||||
rndBytes[0] = rndBytes[0] | mask
|
rndBytes[0] = rndBytes[0] | mask
|
||||||
}
|
}
|
||||||
return rndBytes
|
resolve(rndBytes)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -184,9 +185,7 @@ async function randBits (bitLength, forceLength = false) {
|
||||||
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
||||||
*/
|
*/
|
||||||
function randBitsSync (bitLength, forceLength = false) {
|
function randBitsSync (bitLength, forceLength = false) {
|
||||||
if (bitLength < 1) {
|
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
|
||||||
throw new RangeError('bitLength MUST be > 0')
|
|
||||||
}
|
|
||||||
|
|
||||||
const byteLength = Math.ceil(bitLength / 8)
|
const byteLength = Math.ceil(bitLength / 8)
|
||||||
const rndBytes = randBytesSync(byteLength, false)
|
const rndBytes = randBytesSync(byteLength, false)
|
||||||
|
|
|
@ -145,7 +145,7 @@ function primeSync (bitLength, iterations = 16) {
|
||||||
* @returns {bigint} A cryptographically secure random bigint between [min,max]
|
* @returns {bigint} A cryptographically secure random bigint between [min,max]
|
||||||
*/
|
*/
|
||||||
function randBetween (max, min = 1n) {
|
function randBetween (max, min = 1n) {
|
||||||
if (max <= 0n || min < 0n || max <= min) throw new Error('inputs should be max > 0, min >= 0; max > min')
|
if (max <= 0n || min < 0n || max <= min) throw new RangeError('inputs should be max > 0, min >= 0; max > min')
|
||||||
const interval = max - min
|
const interval = max - min
|
||||||
const bitLen = bigintModArith.bitLength(interval)
|
const bitLen = bigintModArith.bitLength(interval)
|
||||||
let rnd
|
let rnd
|
||||||
|
@ -164,15 +164,14 @@ function randBetween (max, min = 1n) {
|
||||||
*
|
*
|
||||||
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
||||||
*/
|
*/
|
||||||
async function randBits (bitLength, forceLength = false) {
|
function randBits (bitLength, forceLength = false) {
|
||||||
if (bitLength < 1) {
|
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
|
||||||
throw new RangeError('bitLength MUST be > 0')
|
|
||||||
}
|
|
||||||
|
|
||||||
const byteLength = Math.ceil(bitLength / 8)
|
const byteLength = Math.ceil(bitLength / 8)
|
||||||
const bitLengthMod8 = bitLength % 8
|
const bitLengthMod8 = bitLength % 8
|
||||||
|
|
||||||
const rndBytes = await randBytes(byteLength, false)
|
return new Promise((resolve) => {
|
||||||
|
randBytes(byteLength, false).then(function (rndBytes) {
|
||||||
if (bitLengthMod8) {
|
if (bitLengthMod8) {
|
||||||
// Fill with 0's the extra bits
|
// Fill with 0's the extra bits
|
||||||
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
|
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
|
||||||
|
@ -181,7 +180,9 @@ async function randBits (bitLength, forceLength = false) {
|
||||||
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
|
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
|
||||||
rndBytes[0] = rndBytes[0] | mask
|
rndBytes[0] = rndBytes[0] | mask
|
||||||
}
|
}
|
||||||
return rndBytes
|
resolve(rndBytes)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -192,9 +193,7 @@ async function randBits (bitLength, forceLength = false) {
|
||||||
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
||||||
*/
|
*/
|
||||||
function randBitsSync (bitLength, forceLength = false) {
|
function randBitsSync (bitLength, forceLength = false) {
|
||||||
if (bitLength < 1) {
|
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
|
||||||
throw new RangeError('bitLength MUST be > 0')
|
|
||||||
}
|
|
||||||
|
|
||||||
const byteLength = Math.ceil(bitLength / 8)
|
const byteLength = Math.ceil(bitLength / 8)
|
||||||
const rndBytes = randBytesSync(byteLength, false)
|
const rndBytes = randBytesSync(byteLength, false)
|
||||||
|
|
|
@ -171,7 +171,7 @@ export function primeSync (bitLength, iterations = 16) {
|
||||||
* @returns {bigint} A cryptographically secure random bigint between [min,max]
|
* @returns {bigint} A cryptographically secure random bigint between [min,max]
|
||||||
*/
|
*/
|
||||||
export function randBetween (max, min = 1n) {
|
export function randBetween (max, min = 1n) {
|
||||||
if (max <= 0n || min < 0n || max <= min) throw new Error('inputs should be max > 0, min >= 0; max > min')
|
if (max <= 0n || min < 0n || max <= min) throw new RangeError('inputs should be max > 0, min >= 0; max > min')
|
||||||
const interval = max - min
|
const interval = max - min
|
||||||
const bitLen = bitLength(interval)
|
const bitLen = bitLength(interval)
|
||||||
let rnd
|
let rnd
|
||||||
|
@ -190,15 +190,14 @@ export function randBetween (max, min = 1n) {
|
||||||
*
|
*
|
||||||
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
||||||
*/
|
*/
|
||||||
export async function randBits (bitLength, forceLength = false) {
|
export function randBits (bitLength, forceLength = false) {
|
||||||
if (bitLength < 1) {
|
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
|
||||||
throw new RangeError('bitLength MUST be > 0')
|
|
||||||
}
|
|
||||||
|
|
||||||
const byteLength = Math.ceil(bitLength / 8)
|
const byteLength = Math.ceil(bitLength / 8)
|
||||||
const bitLengthMod8 = bitLength % 8
|
const bitLengthMod8 = bitLength % 8
|
||||||
|
|
||||||
const rndBytes = await randBytes(byteLength, false)
|
return new Promise((resolve) => {
|
||||||
|
randBytes(byteLength, false).then(function (rndBytes) {
|
||||||
if (bitLengthMod8) {
|
if (bitLengthMod8) {
|
||||||
// Fill with 0's the extra bits
|
// Fill with 0's the extra bits
|
||||||
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
|
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
|
||||||
|
@ -207,7 +206,9 @@ export async function randBits (bitLength, forceLength = false) {
|
||||||
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
|
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
|
||||||
rndBytes[0] = rndBytes[0] | mask
|
rndBytes[0] = rndBytes[0] | mask
|
||||||
}
|
}
|
||||||
return rndBytes
|
resolve(rndBytes)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -218,9 +219,7 @@ export async function randBits (bitLength, forceLength = false) {
|
||||||
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
|
||||||
*/
|
*/
|
||||||
export function randBitsSync (bitLength, forceLength = false) {
|
export function randBitsSync (bitLength, forceLength = false) {
|
||||||
if (bitLength < 1) {
|
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
|
||||||
throw new RangeError('bitLength MUST be > 0')
|
|
||||||
}
|
|
||||||
|
|
||||||
const byteLength = Math.ceil(bitLength / 8)
|
const byteLength = Math.ceil(bitLength / 8)
|
||||||
const rndBytes = randBytesSync(byteLength, false)
|
const rndBytes = randBytesSync(byteLength, false)
|
||||||
|
|
|
@ -111,30 +111,27 @@ const bitLengths = [
|
||||||
3072
|
3072
|
||||||
];
|
];
|
||||||
|
|
||||||
describe('prime', function () {
|
describe('Testing prime generation', function () {
|
||||||
this.timeout(120000);
|
this.timeout(120000);
|
||||||
for (const bitLength of bitLengths) {
|
for (const bitLength of bitLengths) {
|
||||||
describe(`prime(${bitLength})`, function () {
|
describe(`prime(${bitLength})`, function () {
|
||||||
|
if (bitLength > 0) {
|
||||||
it(`should return a random ${bitLength}-bits probable prime`, async function () {
|
it(`should return a random ${bitLength}-bits probable prime`, async function () {
|
||||||
let primeBitLength = bitLength;
|
|
||||||
try {
|
|
||||||
const prime = await _pkg.prime(bitLength);
|
const prime = await _pkg.prime(bitLength);
|
||||||
primeBitLength = _pkg.bitLength(prime);
|
chai.expect(_pkg.bitLength(prime)).to.equal(bitLength);
|
||||||
} catch {}
|
|
||||||
chai.expect(primeBitLength).to.equal(bitLength);
|
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
it('should throw error', async function () {
|
||||||
|
chai.expect(() => _pkg.prime(bitLength)).to.throw(RangeError);
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
describe('Testing sync (NOT-RECOMMENDED) version: primeSync()', function () {
|
describe('Testing sync (NOT-RECOMMENDED) version: primeSync()', function () {
|
||||||
it('should return a random 1024-bits probable prime', function () {
|
it('should return a random 1024-bits probable prime', function () {
|
||||||
const prime = _pkg.primeSync(1024, 16);
|
const prime = _pkg.primeSync(1024, 16);
|
||||||
const primeBitLength = _pkg.bitLength(prime);
|
chai.expect(_pkg.bitLength(prime)).to.equal(1024);
|
||||||
chai.expect(primeBitLength).to.equal(1024);
|
chai.expect(() => _pkg.primeSync(0)).to.throw(RangeError);
|
||||||
try {
|
|
||||||
_pkg.primeSync(0);
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(true).to.equal(true);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -204,13 +201,8 @@ describe('randBetween', function () {
|
||||||
chai.expect(ret).to.equal(true);
|
chai.expect(ret).to.equal(true);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
it('should return error (max <=0 || min <0 || min>=max)', function () {
|
it('should throw RangeError (max <=0 || min <0 || min>=max)', function () {
|
||||||
try {
|
chai.expect(() => _pkg.randBetween(num.max, num.min)).to.throw(RangeError);
|
||||||
_pkg.randBetween(num.max, num.min);
|
|
||||||
chai.expect(num.error).to.equal(false);
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(num.error).to.equal(true);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -225,13 +217,8 @@ describe('randBetween', function () {
|
||||||
chai.expect(ret).to.equal(true);
|
chai.expect(ret).to.equal(true);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
it('should return error (max <=0)', function () {
|
it('should throw RangeError (max <=0)', function () {
|
||||||
try {
|
chai.expect(() => _pkg.randBetween(num.max)).to.throw(RangeError);
|
||||||
_pkg.randBetween(num.max);
|
|
||||||
chai.expect(num.errorMax).to.equal(false);
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(num.errorMax).to.equal(true);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -245,44 +232,54 @@ describe('randBetween', function () {
|
||||||
// <--
|
// <--
|
||||||
|
|
||||||
const iterations = 10;
|
const iterations = 10;
|
||||||
const bitLengths$1 = [0, 3, 8, 16, 511, 2048];
|
const bitLengths$1 = [-1, 0, 3, 8, 16, 511, 2048];
|
||||||
const byteLengths = [0, 1, 8, 33, 40];
|
const byteLengths = [-7, 0, 1, 8, 33, 40];
|
||||||
|
|
||||||
describe('testing randBits', async function () {
|
describe('testing randBits', async function () {
|
||||||
for (const bitLength of bitLengths$1) {
|
for (const bitLength of bitLengths$1) {
|
||||||
describe(`${iterations} of randBitsSync(${bitLength})`, function () {
|
describe(`${iterations} iterations of randBitsSync(${bitLength})`, function () {
|
||||||
|
if (bitLength > 0) {
|
||||||
|
it('should return buffers', function () {
|
||||||
|
let ret = true;
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
it('should return a buffer', function () {
|
|
||||||
try {
|
|
||||||
const randbits = _pkg.randBitsSync(bitLength);
|
const randbits = _pkg.randBitsSync(bitLength);
|
||||||
// console.log(JSON.stringify(randbits))
|
// console.log(JSON.stringify(randbits))
|
||||||
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array);
|
|
||||||
else chai.expect(randbits).to.be.an.instanceOf(Buffer);
|
|
||||||
const randbits2 = _pkg.randBitsSync(bitLength, true);
|
const randbits2 = _pkg.randBitsSync(bitLength, true);
|
||||||
// console.log(JSON.stringify(randbits2))
|
// console.log(JSON.stringify(randbits2))
|
||||||
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array);
|
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
|
||||||
else chai.expect(randbits2).to.be.an.instanceOf(Buffer);
|
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
|
||||||
} catch (error) {
|
ret = false;
|
||||||
chai.expect(bitLength).to.be.lte(0);
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
chai.expect(ret).to.equal(true);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
it('should throw RangeError', function () {
|
||||||
|
chai.expect(() => _pkg.randBitsSync(bitLength)).to.throw(RangeError);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
describe(`${iterations} of randBits(${bitLength})`, async function () {
|
describe(`${iterations} iterations of randBits(${bitLength})`, async function () {
|
||||||
|
if (bitLength > 0) {
|
||||||
|
it('should return buffers', async function () {
|
||||||
|
let ret = true;
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
it('should return a buffer', async function () {
|
|
||||||
try {
|
|
||||||
const randbits = await _pkg.randBits(bitLength);
|
const randbits = await _pkg.randBits(bitLength);
|
||||||
// console.log(JSON.stringify(randbits))
|
// console.log(JSON.stringify(randbits))
|
||||||
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array);
|
|
||||||
else chai.expect(randbits).to.be.an.instanceOf(Buffer);
|
|
||||||
const randbits2 = await _pkg.randBits(bitLength, true);
|
const randbits2 = await _pkg.randBits(bitLength, true);
|
||||||
// console.log(JSON.stringify(randbits2))
|
// console.log(JSON.stringify(randbits2))
|
||||||
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array);
|
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
|
||||||
else chai.expect(randbits2).to.be.an.instanceOf(Buffer);
|
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
|
||||||
} catch (error) {
|
ret = false;
|
||||||
chai.expect(bitLength).to.be.lte(0);
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
chai.expect(ret).to.equal(true);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
it('should throw RangeError', function () {
|
||||||
|
chai.expect(() => _pkg.randBits(bitLength)).to.throw(RangeError);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -291,39 +288,47 @@ describe('testing randBits', async function () {
|
||||||
|
|
||||||
describe('testing randBytes', async function () {
|
describe('testing randBytes', async function () {
|
||||||
for (const byteLength of byteLengths) {
|
for (const byteLength of byteLengths) {
|
||||||
describe(`${iterations} of randBytesSync(${byteLength})`, function () {
|
describe(`${iterations} iterations of randBytesSync(${byteLength})`, function () {
|
||||||
|
if (byteLength > 0) {
|
||||||
|
it('should return buffers', function () {
|
||||||
|
let ret = true;
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
it('should return a buffer', function () {
|
const randbytes = _pkg.randBytesSync(byteLength);
|
||||||
try {
|
// console.log(JSON.stringify(randbits))
|
||||||
const randbits = _pkg.randBytesSync(byteLength);
|
const randbytes2 = _pkg.randBytesSync(byteLength, true);
|
||||||
console.log(JSON.stringify(randbits));
|
// console.log(JSON.stringify(randbits2))
|
||||||
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array);
|
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
|
||||||
else chai.expect(randbits).to.be.an.instanceOf(Buffer);
|
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
|
||||||
const randbits2 = _pkg.randBytesSync(byteLength, true);
|
ret = false;
|
||||||
console.log(JSON.stringify(randbits2));
|
|
||||||
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array);
|
|
||||||
else chai.expect(randbits2).to.be.an.instanceOf(Buffer);
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(byteLength).to.be.lte(0);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
chai.expect(ret).to.equal(true);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
it('should throw RangeError', function () {
|
||||||
|
chai.expect(() => _pkg.randBytesSync(byteLength)).to.throw(RangeError);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
describe(`${iterations} of randBytes(${byteLength})`, async function () {
|
describe(`${iterations} iterations of randBytes(${byteLength})`, async function () {
|
||||||
|
if (byteLength > 0) {
|
||||||
|
it('should return buffers', async function () {
|
||||||
|
let ret = true;
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
it('should return a buffer', async function () {
|
const randbytes = await _pkg.randBytes(byteLength);
|
||||||
try {
|
// console.log(JSON.stringify(randbits))
|
||||||
const randbits = await _pkg.randBytes(byteLength);
|
const randbytes2 = await _pkg.randBytes(byteLength, true);
|
||||||
console.log(JSON.stringify(randbits));
|
// console.log(JSON.stringify(randbits2))
|
||||||
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array);
|
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
|
||||||
else chai.expect(randbits).to.be.an.instanceOf(Buffer);
|
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
|
||||||
const randbits2 = await _pkg.randBytes(byteLength, true);
|
ret = false;
|
||||||
console.log(JSON.stringify(randbits2));
|
|
||||||
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array);
|
|
||||||
else chai.expect(randbits2).to.be.an.instanceOf(Buffer);
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(byteLength).to.be.lte(0);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
chai.expect(ret).to.equal(true);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
it('should throw RangeError', function () {
|
||||||
|
chai.expect(() => _pkg.randBytes(byteLength)).to.throw(RangeError);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,30 +18,27 @@ const bitLengths = [
|
||||||
3072
|
3072
|
||||||
]
|
]
|
||||||
|
|
||||||
describe('prime', function () {
|
describe('Testing prime generation', function () {
|
||||||
this.timeout(120000)
|
this.timeout(120000)
|
||||||
for (const bitLength of bitLengths) {
|
for (const bitLength of bitLengths) {
|
||||||
describe(`prime(${bitLength})`, function () {
|
describe(`prime(${bitLength})`, function () {
|
||||||
|
if (bitLength > 0) {
|
||||||
it(`should return a random ${bitLength}-bits probable prime`, async function () {
|
it(`should return a random ${bitLength}-bits probable prime`, async function () {
|
||||||
let primeBitLength = bitLength
|
|
||||||
try {
|
|
||||||
const prime = await _pkg.prime(bitLength)
|
const prime = await _pkg.prime(bitLength)
|
||||||
primeBitLength = _pkg.bitLength(prime)
|
chai.expect(_pkg.bitLength(prime)).to.equal(bitLength)
|
||||||
} catch {}
|
|
||||||
chai.expect(primeBitLength).to.equal(bitLength)
|
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
it('should throw error', async function () {
|
||||||
|
chai.expect(() => _pkg.prime(bitLength)).to.throw(RangeError)
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
describe('Testing sync (NOT-RECOMMENDED) version: primeSync()', function () {
|
describe('Testing sync (NOT-RECOMMENDED) version: primeSync()', function () {
|
||||||
it('should return a random 1024-bits probable prime', function () {
|
it('should return a random 1024-bits probable prime', function () {
|
||||||
const prime = _pkg.primeSync(1024, 16)
|
const prime = _pkg.primeSync(1024, 16)
|
||||||
const primeBitLength = _pkg.bitLength(prime)
|
chai.expect(_pkg.bitLength(prime)).to.equal(1024)
|
||||||
chai.expect(primeBitLength).to.equal(1024)
|
chai.expect(() => _pkg.primeSync(0)).to.throw(RangeError)
|
||||||
try {
|
|
||||||
_pkg.primeSync(0)
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(true).to.equal(true)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -65,13 +65,8 @@ describe('randBetween', function () {
|
||||||
chai.expect(ret).to.equal(true)
|
chai.expect(ret).to.equal(true)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
it('should return error (max <=0 || min <0 || min>=max)', function () {
|
it('should throw RangeError (max <=0 || min <0 || min>=max)', function () {
|
||||||
try {
|
chai.expect(() => _pkg.randBetween(num.max, num.min)).to.throw(RangeError)
|
||||||
_pkg.randBetween(num.max, num.min)
|
|
||||||
chai.expect(num.error).to.equal(false)
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(num.error).to.equal(true)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -86,13 +81,8 @@ describe('randBetween', function () {
|
||||||
chai.expect(ret).to.equal(true)
|
chai.expect(ret).to.equal(true)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
it('should return error (max <=0)', function () {
|
it('should throw RangeError (max <=0)', function () {
|
||||||
try {
|
chai.expect(() => _pkg.randBetween(num.max)).to.throw(RangeError)
|
||||||
_pkg.randBetween(num.max)
|
|
||||||
chai.expect(num.errorMax).to.equal(false)
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(num.errorMax).to.equal(true)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
110
test/random.js
110
test/random.js
|
@ -7,44 +7,54 @@ const chai = require('chai')
|
||||||
// <--
|
// <--
|
||||||
|
|
||||||
const iterations = 10
|
const iterations = 10
|
||||||
const bitLengths = [0, 3, 8, 16, 511, 2048]
|
const bitLengths = [-1, 0, 3, 8, 16, 511, 2048]
|
||||||
const byteLengths = [0, 1, 8, 33, 40]
|
const byteLengths = [-7, 0, 1, 8, 33, 40]
|
||||||
|
|
||||||
describe('testing randBits', async function () {
|
describe('testing randBits', async function () {
|
||||||
for (const bitLength of bitLengths) {
|
for (const bitLength of bitLengths) {
|
||||||
describe(`${iterations} of randBitsSync(${bitLength})`, function () {
|
describe(`${iterations} iterations of randBitsSync(${bitLength})`, function () {
|
||||||
|
if (bitLength > 0) {
|
||||||
|
it('should return buffers', function () {
|
||||||
|
let ret = true
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
it('should return a buffer', function () {
|
|
||||||
try {
|
|
||||||
const randbits = _pkg.randBitsSync(bitLength)
|
const randbits = _pkg.randBitsSync(bitLength)
|
||||||
// console.log(JSON.stringify(randbits))
|
// console.log(JSON.stringify(randbits))
|
||||||
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array)
|
|
||||||
else chai.expect(randbits).to.be.an.instanceOf(Buffer)
|
|
||||||
const randbits2 = _pkg.randBitsSync(bitLength, true)
|
const randbits2 = _pkg.randBitsSync(bitLength, true)
|
||||||
// console.log(JSON.stringify(randbits2))
|
// console.log(JSON.stringify(randbits2))
|
||||||
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array)
|
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
|
||||||
else chai.expect(randbits2).to.be.an.instanceOf(Buffer)
|
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
|
||||||
} catch (error) {
|
ret = false
|
||||||
chai.expect(bitLength).to.be.lte(0)
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
chai.expect(ret).to.equal(true)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
it('should throw RangeError', function () {
|
||||||
|
chai.expect(() => _pkg.randBitsSync(bitLength)).to.throw(RangeError)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
describe(`${iterations} of randBits(${bitLength})`, async function () {
|
describe(`${iterations} iterations of randBits(${bitLength})`, async function () {
|
||||||
|
if (bitLength > 0) {
|
||||||
|
it('should return buffers', async function () {
|
||||||
|
let ret = true
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
it('should return a buffer', async function () {
|
|
||||||
try {
|
|
||||||
const randbits = await _pkg.randBits(bitLength)
|
const randbits = await _pkg.randBits(bitLength)
|
||||||
// console.log(JSON.stringify(randbits))
|
// console.log(JSON.stringify(randbits))
|
||||||
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array)
|
|
||||||
else chai.expect(randbits).to.be.an.instanceOf(Buffer)
|
|
||||||
const randbits2 = await _pkg.randBits(bitLength, true)
|
const randbits2 = await _pkg.randBits(bitLength, true)
|
||||||
// console.log(JSON.stringify(randbits2))
|
// console.log(JSON.stringify(randbits2))
|
||||||
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array)
|
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
|
||||||
else chai.expect(randbits2).to.be.an.instanceOf(Buffer)
|
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
|
||||||
} catch (error) {
|
ret = false
|
||||||
chai.expect(bitLength).to.be.lte(0)
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
chai.expect(ret).to.equal(true)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
it('should throw RangeError', function () {
|
||||||
|
chai.expect(() => _pkg.randBits(bitLength)).to.throw(RangeError)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -53,39 +63,47 @@ describe('testing randBits', async function () {
|
||||||
|
|
||||||
describe('testing randBytes', async function () {
|
describe('testing randBytes', async function () {
|
||||||
for (const byteLength of byteLengths) {
|
for (const byteLength of byteLengths) {
|
||||||
describe(`${iterations} of randBytesSync(${byteLength})`, function () {
|
describe(`${iterations} iterations of randBytesSync(${byteLength})`, function () {
|
||||||
|
if (byteLength > 0) {
|
||||||
|
it('should return buffers', function () {
|
||||||
|
let ret = true
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
it('should return a buffer', function () {
|
const randbytes = _pkg.randBytesSync(byteLength)
|
||||||
try {
|
// console.log(JSON.stringify(randbits))
|
||||||
const randbits = _pkg.randBytesSync(byteLength)
|
const randbytes2 = _pkg.randBytesSync(byteLength, true)
|
||||||
console.log(JSON.stringify(randbits))
|
// console.log(JSON.stringify(randbits2))
|
||||||
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array)
|
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
|
||||||
else chai.expect(randbits).to.be.an.instanceOf(Buffer)
|
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
|
||||||
const randbits2 = _pkg.randBytesSync(byteLength, true)
|
ret = false
|
||||||
console.log(JSON.stringify(randbits2))
|
|
||||||
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array)
|
|
||||||
else chai.expect(randbits2).to.be.an.instanceOf(Buffer)
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(byteLength).to.be.lte(0)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
chai.expect(ret).to.equal(true)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
it('should throw RangeError', function () {
|
||||||
|
chai.expect(() => _pkg.randBytesSync(byteLength)).to.throw(RangeError)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
describe(`${iterations} of randBytes(${byteLength})`, async function () {
|
describe(`${iterations} iterations of randBytes(${byteLength})`, async function () {
|
||||||
|
if (byteLength > 0) {
|
||||||
|
it('should return buffers', async function () {
|
||||||
|
let ret = true
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
it('should return a buffer', async function () {
|
const randbytes = await _pkg.randBytes(byteLength)
|
||||||
try {
|
// console.log(JSON.stringify(randbits))
|
||||||
const randbits = await _pkg.randBytes(byteLength)
|
const randbytes2 = await _pkg.randBytes(byteLength, true)
|
||||||
console.log(JSON.stringify(randbits))
|
// console.log(JSON.stringify(randbits2))
|
||||||
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array)
|
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
|
||||||
else chai.expect(randbits).to.be.an.instanceOf(Buffer)
|
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
|
||||||
const randbits2 = await _pkg.randBytes(byteLength, true)
|
ret = false
|
||||||
console.log(JSON.stringify(randbits2))
|
|
||||||
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array)
|
|
||||||
else chai.expect(randbits2).to.be.an.instanceOf(Buffer)
|
|
||||||
} catch (error) {
|
|
||||||
chai.expect(byteLength).to.be.lte(0)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
chai.expect(ret).to.equal(true)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
it('should throw RangeError', function () {
|
||||||
|
chai.expect(() => _pkg.randBytes(byteLength)).to.throw(RangeError)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue