make CSPRNG work on ES6

This commit is contained in:
Florian Dold 2021-06-14 18:19:18 +02:00
parent 1cde390c23
commit b4f97f4592
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
3 changed files with 13 additions and 5 deletions

View File

@ -68,7 +68,7 @@
},
"ava": {
"files": [
"lib/**/*test*"
"lib/**/*test.*"
]
}
}

View File

@ -5,6 +5,12 @@
// Implementation derived from TweetNaCl version 20140427.
// See for details: http://tweetnacl.cr.yp.to/
import { createRequire } from "module";
// We need this require function to synchronously
// import the "crypto" module in the CSPRNG initialization.
const require = createRequire(import.meta.url);
const gf = function (init: number[] = []): Float64Array {
const r = new Float64Array(16);
if (init) for (let i = 0; i < init.length; i++) r[i] = init[i];
@ -3021,7 +3027,7 @@ export function secretbox_open(
return m.subarray(crypto_secretbox_ZEROBYTES);
}
(function () {
function initPRNG() {
// Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw.
// @ts-ignore-error
@ -3050,4 +3056,6 @@ export function secretbox_open(
});
}
}
})();
}
initPRNG();

View File

@ -4,7 +4,7 @@
"composite": true,
"declaration": true,
"declarationMap": false,
"target": "ES6",
"target": "ES2017",
"module": "ESNext",
"moduleResolution": "node",
"sourceMap": true,
@ -21,7 +21,7 @@
"esModuleInterop": true,
"importHelpers": true,
"rootDir": "./src",
"typeRoots": ["./node_modules/@types"]
"typeRoots": ["./node_modules/@types"],
},
"references": [
{