fixed issue preventing generation of more than 65536 random bytes when run in a browser

This commit is contained in:
Juanra Dikal 2022-12-20 23:51:18 +01:00
parent b967f954b3
commit e99584aec4
11 changed files with 84 additions and 24 deletions

22
dist/bundles/esm.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/bundles/umd.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -155,7 +155,7 @@ A promise that resolves to a boolean that is either true (a probably prime numbe
#### Defined in #### Defined in
[src/ts/isProbablyPrime.ts:20](https://github.com/juanelas/bigint-crypto-utils/blob/40daed1/src/ts/isProbablyPrime.ts#L20) [src/ts/isProbablyPrime.ts:20](https://github.com/juanelas/bigint-crypto-utils/blob/b967f95/src/ts/isProbablyPrime.ts#L20)
___ ___
@ -322,7 +322,7 @@ A promise that resolves to a bigint probable prime of bitLength bits.
#### Defined in #### Defined in
[src/ts/prime.ts:28](https://github.com/juanelas/bigint-crypto-utils/blob/40daed1/src/ts/prime.ts#L28) [src/ts/prime.ts:28](https://github.com/juanelas/bigint-crypto-utils/blob/b967f95/src/ts/prime.ts#L28)
___ ___
@ -352,7 +352,7 @@ A bigint probable prime of bitLength bits.
#### Defined in #### Defined in
[src/ts/prime.ts:109](https://github.com/juanelas/bigint-crypto-utils/blob/40daed1/src/ts/prime.ts#L109) [src/ts/prime.ts:109](https://github.com/juanelas/bigint-crypto-utils/blob/b967f95/src/ts/prime.ts#L109)
___ ___
@ -381,7 +381,7 @@ A cryptographically secure random bigint between [min,max]
#### Defined in #### Defined in
[src/ts/randBetween.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/40daed1/src/ts/randBetween.ts#L14) [src/ts/randBetween.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/b967f95/src/ts/randBetween.ts#L14)
___ ___
@ -410,7 +410,7 @@ A Promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cry
#### Defined in #### Defined in
[src/ts/randBits.ts:13](https://github.com/juanelas/bigint-crypto-utils/blob/40daed1/src/ts/randBits.ts#L13) [src/ts/randBits.ts:13](https://github.com/juanelas/bigint-crypto-utils/blob/b967f95/src/ts/randBits.ts#L13)
___ ___
@ -439,7 +439,7 @@ A Uint8Array/Buffer (Browser/Node.js) filled with cryptographically secure rando
#### Defined in #### Defined in
[src/ts/randBits.ts:43](https://github.com/juanelas/bigint-crypto-utils/blob/40daed1/src/ts/randBits.ts#L43) [src/ts/randBits.ts:43](https://github.com/juanelas/bigint-crypto-utils/blob/b967f95/src/ts/randBits.ts#L43)
___ ___
@ -468,7 +468,7 @@ A promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cry
#### Defined in #### Defined in
[src/ts/randBytes.ts:13](https://github.com/juanelas/bigint-crypto-utils/blob/40daed1/src/ts/randBytes.ts#L13) [src/ts/randBytes.ts:13](https://github.com/juanelas/bigint-crypto-utils/blob/b967f95/src/ts/randBytes.ts#L13)
___ ___
@ -498,7 +498,7 @@ A UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure rando
#### Defined in #### Defined in
[src/ts/randBytes.ts:45](https://github.com/juanelas/bigint-crypto-utils/blob/40daed1/src/ts/randBytes.ts#L45) [src/ts/randBytes.ts:54](https://github.com/juanelas/bigint-crypto-utils/blob/b967f95/src/ts/randBytes.ts#L54)
___ ___

View File

@ -23,7 +23,16 @@ export function randBytes (byteLength: number, forceLength = false): Promise<Uin
}) })
} else { // browser } else { // browser
const buf = new Uint8Array(byteLength) const buf = new Uint8Array(byteLength)
// the maximum number of bytes of entropy available via self.crypto.getRandomValues is 65536
if (byteLength <= 65536) {
self.crypto.getRandomValues(buf) self.crypto.getRandomValues(buf)
} else {
for (let i = 0; i < Math.ceil(byteLength / 65536); i++) {
const begin = i * 65536
const end = ((begin + 65535) < byteLength) ? begin + 65535 : byteLength - 1
self.crypto.getRandomValues(buf.subarray(begin, end))
}
}
// If fixed length is required we put the first bit to 1 -> to get the necessary bitLength // If fixed length is required we put the first bit to 1 -> to get the necessary bitLength
if (forceLength) buf[0] = buf[0] | 128 if (forceLength) buf[0] = buf[0] | 128
resolve(buf) resolve(buf)
@ -53,7 +62,16 @@ export function randBytesSync (byteLength: number, forceLength: boolean = false)
return buf return buf
} else { // browser } else { // browser
const buf = new Uint8Array(byteLength) const buf = new Uint8Array(byteLength)
// the maximum number of bytes of entropy available via self.crypto.getRandomValues is 65536
if (byteLength <= 65536) {
self.crypto.getRandomValues(buf) self.crypto.getRandomValues(buf)
} else {
for (let i = 0; i < Math.ceil(byteLength / 65536); i++) {
const begin = i * 65536
const end = ((begin + 65535) < byteLength) ? begin + 65535 : byteLength - 1
self.crypto.getRandomValues(buf.subarray(begin, end))
}
}
// If fixed length is required we put the first bit to 1 -> to get the necessary bitLength // If fixed length is required we put the first bit to 1 -> to get the necessary bitLength
if (forceLength) buf[0] = buf[0] | 128 if (forceLength) buf[0] = buf[0] | 128
return buf return buf

View File

@ -2,7 +2,7 @@ import * as bcu from '#pkg'
const iterations = 10 const iterations = 10
const bitLengths = [-1, 0, 3, 8, 16, 511, 2048] const bitLengths = [-1, 0, 3, 8, 16, 511, 2048]
const byteLengths = [-7, 0, 1, 8, 33, 40] const byteLengths = [-7, 0, 1, 8, 33, 40, 65536, 67108864]
describe('testing randBits', function () { describe('testing randBits', function () {
for (const bitLength of bitLengths) { for (const bitLength of bitLengths) {
@ -81,12 +81,14 @@ describe('testing randBytes', function () {
}) })
describe(`${iterations} iterations of randBytes(${byteLength})`, function () { describe(`${iterations} iterations of randBytes(${byteLength})`, function () {
if (byteLength > 0) { if (byteLength > 0) {
it('should return buffers', async function () { it('should return buffers of the expected length', async function () {
let ret = true let ret = true
for (let i = 0; i < iterations; i++) { for (let i = 0; i < iterations; i++) {
const randbytes = await bcu.randBytes(byteLength) const randbytes = await bcu.randBytes(byteLength)
chai.expect(randbytes.length).to.equal(byteLength)
// console.log(JSON.stringify(randbits)) // console.log(JSON.stringify(randbits))
const randbytes2 = await bcu.randBytes(byteLength, true) const randbytes2 = await bcu.randBytes(byteLength, true)
chai.expect(randbytes2.length).to.equal(byteLength)
// console.log(JSON.stringify(randbits2)) // console.log(JSON.stringify(randbits2))
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) || if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) { ((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {

View File

@ -1 +1 @@
{"version":3,"file":"randBytes.d.ts","sourceRoot":"","sources":["../src/ts/randBytes.ts"],"names":[],"mappings":";AAEA;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAE,UAAU,EAAE,MAAM,EAAE,WAAW,UAAQ,GAAG,OAAO,CAAC,UAAU,GAAC,MAAM,CAAC,CAmB9F;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAE,UAAU,EAAE,MAAM,EAAE,WAAW,GAAE,OAAe,GAAG,UAAU,GAAC,MAAM,CAiBlG"} {"version":3,"file":"randBytes.d.ts","sourceRoot":"","sources":["../src/ts/randBytes.ts"],"names":[],"mappings":";AAEA;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAE,UAAU,EAAE,MAAM,EAAE,WAAW,UAAQ,GAAG,OAAO,CAAC,UAAU,GAAC,MAAM,CAAC,CA4B9F;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAE,UAAU,EAAE,MAAM,EAAE,WAAW,GAAE,OAAe,GAAG,UAAU,GAAC,MAAM,CA0BlG"}