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:
Daniel Huigens 2020-01-14 18:06:09 +01:00 committed by GitHub
parent fd6d7b6088
commit 44a90d9465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -103,6 +103,16 @@ function seedRandom(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.
* @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);
// parse cloned packets
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) {
// clone packets (for web worker structured cloning algorithm)
response({ id:id, event:'method-return', data:openpgp.packet.clone.clonePackets(data) });