forge
forge copied to clipboard
Inconsistent Key Generation using seed value
I have an issue where depending on whether I use a callback or not, the keyPair that is generated is different. Any idea what may be causing this? I would expect the keyPair to be the same regardless, no?
export async function getAsymmetricKeyPair(
ctx: Ctx,
seedValue?: string
): Promise<forge.pki.rsa.KeyPair> {
const prng = forge.random.createInstance();
if (seedValue) {
prng.seedFileSync = (_n: number) => seedValue;
prng.seedFile = (_n: number, cb: forge.random.CB) => {
cb(null, seedValue);
};
}
const options: forge.pki.rsa.GenerateKeyPairOptions = {
bits: 2048,
e: 65537,
prng,
};
if (!ctx.disableWorkers) {
options.workLoad = 100;
// only using 1 web worker as more makes the call non-deterministic
// -1 uses optimal amount of web workers
options.workers = seedValue ? 1 : -1;
const workerRes = await fetch(`${getPortalBaseURL(ctx)}/static/js/prime.worker.min.js`);
const workerBlob = new Blob([await workerRes.text()], { type: 'application/javascript' });
options.workerScript = URL.createObjectURL(workerBlob);
}
console.log('generating key pair...');
// const keyPair = rsa.generateKeyPair(options);
const keyPair = await new Promise<forge.pki.rsa.KeyPair>((resolve, reject) => {
rsa.generateKeyPair(options, (err, keyPair) => {
if (err) {
reject(err);
} else {
resolve(keyPair);
}
});
}
);
console.log('public key:', keyPair.publicKey.n.data);
console.log('key pair generated');
return keyPair;
}
We haven't had much time to look at this. Which callback do you mean? Are calling in a way that ensures only one worker is in use? As the comment notes, it's non-deterministic with more than 1 worker. Is there an easy way to run this to show the issue?
Yeah I am sure it's setting workers to 1 when given a seed value. I've also recreated this outside of the project it's coming from
But I will double check again for sanity!
And I mean the callback that you can optionally include in the rsa.generateKeyPair function
Do you have a repo or gist or something that can be easily run?
Yeah, lemme get that for you, gotta look through my projects again