From a9d4798bfe7174d2f25bf41a8e812142b53b8836 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Wed, 23 Oct 2019 12:15:58 +0000 Subject: [PATCH] Data: Cache public key in private key class --- core/java/src/net/i2p/data/PrivateKey.java | 26 +++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/core/java/src/net/i2p/data/PrivateKey.java b/core/java/src/net/i2p/data/PrivateKey.java index ddbd628113..e3a3b60f41 100644 --- a/core/java/src/net/i2p/data/PrivateKey.java +++ b/core/java/src/net/i2p/data/PrivateKey.java @@ -31,6 +31,8 @@ public class PrivateKey extends SimpleDataStructure implements Destroyable { public final static int KEYSIZE_BYTES = DEF_TYPE.getPrivkeyLen(); private final EncType _type; + // cache + private PublicKey _pubKey; public PrivateKey() { this(DEF_TYPE); @@ -61,6 +63,19 @@ public class PrivateKey extends SimpleDataStructure implements Destroyable { setData(data); } + /** + * @param type non-null + * @param data must be non-null + * @param pubKey corresponding pubKey to be cached + * @since 0.9.44 + */ + public PrivateKey(EncType type, byte data[], PublicKey pubKey) { + this(type, data); + if (type != pubKey.getType()) + throw new IllegalArgumentException("Pubkey mismatch"); + _pubKey = pubKey; + } + /** constructs from base64 * @param base64Data a string of base64 data (the output of .toBase64() called * on a prior instance of PrivateKey @@ -82,13 +97,18 @@ public class PrivateKey extends SimpleDataStructure implements Destroyable { return _type; } - /** derives a new PublicKey object derived from the secret contents - * of this PrivateKey + /** + * Derives a new PublicKey object derived from the secret contents + * of this PrivateKey. + * As of 0.9.44, the PublicKey is cached. + * * @return a PublicKey object * @throws IllegalArgumentException on bad key */ public PublicKey toPublic() { - return KeyGenerator.getPublicKey(this); + if (_pubKey == null) + _pubKey = KeyGenerator.getPublicKey(this); + return _pubKey; } /** -- GitLab