From 1a9ac238e85f4a3a028b4a23dde671bcc2170272 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 24 Oct 2020 19:15:11 +0200 Subject: [PATCH] OpenPgpManager: Expose methods to generate and import keys --- .../smackx/ox/OpenPgpManager.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java index 61c86b61f..ddef3aee9 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java @@ -295,14 +295,9 @@ public final class OpenPgpManager extends Manager { throwIfNoProviderSet(); OpenPgpStore store = provider.getStore(); - PGPKeyRing keys = store.generateKeyRing(ourJid); - try { - store.importSecretKey(ourJid, keys.getSecretKeys()); - store.importPublicKey(ourJid, keys.getPublicKeys()); - } catch (MissingUserIdOnKeyException e) { - // This should never throw, since we set our jid literally one line above this comment. - throw new AssertionError(e); - } + + PGPKeyRing keys = generateKeyRing(ourJid); + importKeyRing(ourJid, keys); OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(keys.getSecretKeys()); @@ -311,6 +306,23 @@ public final class OpenPgpManager extends Manager { return fingerprint; } + public PGPKeyRing generateKeyRing(BareJid ourJid) + throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + throwIfNoProviderSet(); + PGPKeyRing keys = provider.getStore().generateKeyRing(ourJid); + return keys; + } + + private void importKeyRing(BareJid ourJid, PGPKeyRing keyRing) throws IOException, PGPException { + try { + provider.getStore().importSecretKey(ourJid, keyRing.getSecretKeys()); + provider.getStore().importPublicKey(ourJid, keyRing.getPublicKeys()); + } catch (MissingUserIdOnKeyException e) { + // This should never throw, since we set our jid literally one line above this comment. + throw new AssertionError(e); + } + } + /** * Return the upper-case hex encoded OpenPGP v4 fingerprint of our key pair. *