diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-12-28 17:56:55 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-01-08 11:48:00 +0100 |
commit | 2dfff3a645f0b1256a0ad93510f48be996142032 (patch) | |
tree | cadf8b70b0d2b89efb7af3420eb40a8458062425 /openpgp | |
parent | 91e945291fc0e602a9675ed4bd6740603b94af73 (diff) |
openpgp: Add new convenience function.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/packet/key.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index a9240744..5510edc2 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -6,7 +6,7 @@ use std::cmp::Ordering; use time; use Error; -use crypto::mpis; +use crypto::{mpis, KeyPair}; use packet::Tag; use packet; use Packet; @@ -277,6 +277,27 @@ impl Key { tag)).into()), } } + + /// Creates a new key pair from a Key packet with an unencrypted + /// secret key. + /// + /// # Errors + /// + /// Fails if the secret key is missing, or encrypted. + pub fn into_keypair(mut self) -> Result<KeyPair> { + use packet::key::SecretKey; + let secret = match self.set_secret(None) { + Some(SecretKey::Unencrypted { mpis }) => mpis, + Some(SecretKey::Encrypted { .. }) => + return Err(Error::InvalidArgument( + "secret key is encrypted".into()).into()), + None => + return Err(Error::InvalidArgument( + "no secret key".into()).into()), + }; + + KeyPair::new(self, secret) + } } /// Holds the secret potion of a OpenPGP secret key or secret subkey packet. |