Cache key objects in Workers by armor (#1030)
This allows us to use the cached `verified` property on self-signatures, so that we don't have to repeatedly verify them.
This commit is contained in:
parent
fd6d7b6088
commit
44a90d9465
|
@ -103,6 +103,16 @@ function seedRandom(buffer) {
|
||||||
openpgp.crypto.random.randomBuffer.set(buffer);
|
openpgp.crypto.random.randomBuffer.set(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const keyCache = new Map();
|
||||||
|
function getCachedKey(key) {
|
||||||
|
const armor = key.armor();
|
||||||
|
if (keyCache.has(armor)) {
|
||||||
|
return keyCache.get(armor);
|
||||||
|
}
|
||||||
|
keyCache.set(armor, key);
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic proxy function that handles all commands from the public api.
|
* Generic proxy function that handles all commands from the public api.
|
||||||
* @param {String} method The public api function to be delegated to the worker thread
|
* @param {String} method The public api function to be delegated to the worker thread
|
||||||
|
@ -117,6 +127,13 @@ function delegate(id, method, options) {
|
||||||
openpgp.util.restoreStreams(options);
|
openpgp.util.restoreStreams(options);
|
||||||
// parse cloned packets
|
// parse cloned packets
|
||||||
options = openpgp.packet.clone.parseClonedPackets(options, method);
|
options = openpgp.packet.clone.parseClonedPackets(options, method);
|
||||||
|
// cache keys by armor, so that we don't have to repeatedly verify self-signatures
|
||||||
|
if (options.publicKeys) {
|
||||||
|
options.publicKeys = options.publicKeys.map(getCachedKey);
|
||||||
|
}
|
||||||
|
if (options.privateKeys) {
|
||||||
|
options.privateKeys = options.privateKeys.map(getCachedKey);
|
||||||
|
}
|
||||||
openpgp[method](options).then(function(data) {
|
openpgp[method](options).then(function(data) {
|
||||||
// clone packets (for web worker structured cloning algorithm)
|
// clone packets (for web worker structured cloning algorithm)
|
||||||
response({ id:id, event:'method-return', data:openpgp.packet.clone.clonePackets(data) });
|
response({ id:id, event:'method-return', data:openpgp.packet.clone.clonePackets(data) });
|
||||||
|
|
Loading…
Reference in New Issue
Block a user