From 2dfff3a645f0b1256a0ad93510f48be996142032 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Fri, 28 Dec 2018 17:56:55 +0100 Subject: openpgp: Add new convenience function. --- openpgp/src/packet/key.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'openpgp') 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 { + 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. -- cgit v1.2.3