From 9a5fc3cb240da0f30674f7740c373b0e139c4ff0 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 31 Jan 2022 16:03:36 -0700 Subject: [PATCH] ssh-key: add `FromStr` impls to key types The `PublicKey` and `PrivateKey` types both have inherent `from_openssh` methods with the goal of potentially supporting some earlier legacy formats in addition to the most current OpenSSH formats. However, they didn't previously have `FromStr`. This commit adds a `FromStr` thunk which calls `from_openssh`. --- ssh-key/src/private.rs | 9 +++++++++ ssh-key/src/public.rs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/ssh-key/src/private.rs b/ssh-key/src/private.rs index 23c4d5f7e..922e9f077 100644 --- a/ssh-key/src/private.rs +++ b/ssh-key/src/private.rs @@ -26,6 +26,7 @@ use crate::{ base64::{self, Decode}, public, Algorithm, CipherAlg, Error, KdfAlg, KdfOptions, Result, }; +use core::str::FromStr; #[cfg(feature = "alloc")] use alloc::string::String; @@ -126,6 +127,14 @@ impl PrivateKey { } } +impl FromStr for PrivateKey { + type Err = Error; + + fn from_str(s: &str) -> Result { + Self::from_openssh(s) + } +} + /// Private key data. #[derive(Clone, Debug)] #[non_exhaustive] diff --git a/ssh-key/src/public.rs b/ssh-key/src/public.rs index 224062bde..e6f72654a 100644 --- a/ssh-key/src/public.rs +++ b/ssh-key/src/public.rs @@ -21,6 +21,7 @@ use crate::{ base64::{self, Decode}, Algorithm, Error, Result, }; +use core::str::FromStr; #[cfg(feature = "alloc")] use alloc::{borrow::ToOwned, string::String}; @@ -72,6 +73,14 @@ impl PublicKey { } } +impl FromStr for PublicKey { + type Err = Error; + + fn from_str(s: &str) -> Result { + Self::from_openssh(s) + } +} + /// Public key data. #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] #[non_exhaustive]