mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2025-07-04 03:52:29 +00:00
Add Argon2S2K.reloadWasmModule()
for manually triggering memory deallocation (#14)
Also, make `ARGON2_WASM_MEMORY_THRESHOLD_RELOAD` a static class property, to be able to change its value.
This commit is contained in:
parent
bec09a16fa
commit
55f8ab2629
2
openpgp.d.ts
vendored
2
openpgp.d.ts
vendored
@ -942,6 +942,8 @@ export namespace enums {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export declare class Argon2S2K {
|
export declare class Argon2S2K {
|
||||||
|
static reloadWasmModule(): void;
|
||||||
|
static ARGON2_WASM_MEMORY_THRESHOLD_RELOAD: number;
|
||||||
constructor(config: Config);
|
constructor(config: Config);
|
||||||
salt: Uint8Array;
|
salt: Uint8Array;
|
||||||
/** @throws Argon2OutOfMemoryError */
|
/** @throws Argon2OutOfMemoryError */
|
||||||
|
@ -23,9 +23,26 @@ export class Argon2OutOfMemoryError extends Error {
|
|||||||
let loadArgonWasmModule;
|
let loadArgonWasmModule;
|
||||||
let argon2Promise;
|
let argon2Promise;
|
||||||
// reload wasm module above this treshold, to deallocated used memory
|
// reload wasm module above this treshold, to deallocated used memory
|
||||||
const ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = 2 << 19;
|
// (cannot be declared as a simple `static` field as its not supported by Safari 14)
|
||||||
|
let ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = 2 << 19;
|
||||||
|
|
||||||
class Argon2S2K {
|
class Argon2S2K {
|
||||||
|
static get ARGON2_WASM_MEMORY_THRESHOLD_RELOAD() {
|
||||||
|
return ARGON2_WASM_MEMORY_THRESHOLD_RELOAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
static set ARGON2_WASM_MEMORY_THRESHOLD_RELOAD(memoryThreshold) {
|
||||||
|
ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = memoryThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
static reloadWasmModule() {
|
||||||
|
if (!loadArgonWasmModule) return;
|
||||||
|
|
||||||
|
// it will be awaited if needed at the next `produceKey` invocation
|
||||||
|
argon2Promise = loadArgonWasmModule();
|
||||||
|
argon2Promise.catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
||||||
*/
|
*/
|
||||||
@ -125,10 +142,8 @@ class Argon2S2K {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// a lot of memory was used, reload to deallocate
|
// a lot of memory was used, reload to deallocate
|
||||||
if (decodedM > ARGON2_WASM_MEMORY_THRESHOLD_RELOAD) {
|
if (decodedM > Argon2S2K.ARGON2_WASM_MEMORY_THRESHOLD_RELOAD) {
|
||||||
// it will be awaited if needed at the next `produceKey` invocation
|
Argon2S2K.reloadWasmModule();
|
||||||
argon2Promise = loadArgonWasmModule();
|
|
||||||
argon2Promise.catch(() => {});
|
|
||||||
}
|
}
|
||||||
return hash;
|
return hash;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -335,6 +335,20 @@ class MemoryBenchamrkSuite {
|
|||||||
await decryptedData.pipeTo(sink);
|
await decryptedData.pipeTo(sink);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
suite.add('openpgp.encrypt/decryptSessionKeys (argon2)', async () => {
|
||||||
|
const config = { s2kType: openpgp.enums.s2k.argon2 };
|
||||||
|
const passwords = 'password';
|
||||||
|
const sessionKey = {
|
||||||
|
algorithm: 'aes128',
|
||||||
|
data: require('crypto').getRandomValues(new Uint8Array(16))
|
||||||
|
};
|
||||||
|
const encrypted = await openpgp.encryptSessionKey({ ...sessionKey, passwords, config, format: 'object' });
|
||||||
|
assert(encrypted.packets.length === 1);
|
||||||
|
const skesk = encrypted.packets[0];
|
||||||
|
assert(skesk.s2k.type === 'argon2');
|
||||||
|
await openpgp.decryptSessionKeys({ message: encrypted, passwords });
|
||||||
|
});
|
||||||
|
|
||||||
const stats = await suite.run();
|
const stats = await suite.run();
|
||||||
// Print JSON stats to stdout
|
// Print JSON stats to stdout
|
||||||
console.log(JSON.stringify(stats, null, 4));
|
console.log(JSON.stringify(stats, null, 4));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user