Skip to content

Commit 36a0c05

Browse files
committed
refactor: make clone_public_key infallible
1 parent 9fe7301 commit 36a0c05

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

russh-keys/src/backend_rust.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,11 @@ impl<'a> From<&RsaPrivate> for protocol::RsaPublicKey<'a> {
155155
}
156156
}
157157

158-
impl TryFrom<&RsaPrivate> for RsaPublic {
159-
type Error = Error;
160-
161-
fn try_from(key: &RsaPrivate) -> Result<Self, Self::Error> {
162-
Ok(Self {
158+
impl From<&RsaPrivate> for RsaPublic {
159+
fn from(key: &RsaPrivate) -> Self {
160+
Self {
163161
key: key.key.to_public_key(),
164-
})
162+
}
165163
}
166164
}
167165

russh-keys/src/key.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
use std::borrow::Cow;
16-
use std::convert::{TryFrom, TryInto};
16+
use std::convert::TryFrom;
1717

1818
pub use backend::{RsaPrivate, RsaPublic};
1919
use ed25519_dalek::{Signer, Verifier};
@@ -291,17 +291,17 @@ impl KeyPair {
291291
}
292292

293293
/// Copy the public key of this algorithm.
294-
pub fn clone_public_key(&self) -> Result<PublicKey, Error> {
295-
Ok(match self {
294+
pub fn clone_public_key(&self) -> PublicKey {
295+
match self {
296296
KeyPair::Ed25519(ref key) => PublicKey::Ed25519(key.verifying_key()),
297297
KeyPair::RSA { ref key, ref hash } => PublicKey::RSA {
298-
key: key.try_into()?,
298+
key: key.into(),
299299
hash: *hash,
300300
},
301301
KeyPair::EC { ref key } => PublicKey::EC {
302302
key: key.to_public_key(),
303303
},
304-
})
304+
}
305305
}
306306

307307
/// Name of this key algorithm.

russh-keys/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
//! russh_keys::agent::server::serve(tokio_stream::wrappers::UnixListenerStream::new(listener), X {}).await
4646
//! });
4747
//! let key = decode_secret_key(PKCS8_ENCRYPTED, Some("blabla")).unwrap();
48-
//! let public = key.clone_public_key().unwrap();
48+
//! let public = key.clone_public_key();
4949
//! core.block_on(async move {
5050
//! let stream = tokio::net::UnixStream::connect(&agent_path).await?;
5151
//! let mut client = agent::client::AgentClient::connect(stream);
@@ -758,7 +758,7 @@ ocyR
758758
}
759759
// Verify using public key derived from the secret key.
760760
{
761-
let public = key.clone_public_key().unwrap();
761+
let public = key.clone_public_key();
762762
assert!(public.verify_detached(buf, sig.as_ref()));
763763
}
764764
// Sanity check that it uses a different random number.
@@ -787,7 +787,7 @@ ocyR
787787
let mut sig = Vec::new();
788788
sig.extend_ssh_string(&[0]);
789789
sig.extend_ssh_string(&[0]);
790-
let public = key.clone_public_key().unwrap();
790+
let public = key.clone_public_key();
791791
assert!(!public.verify_detached(buf, &sig));
792792
}
793793
}
@@ -911,7 +911,7 @@ KJaj7gc0n6gmKY6r0/Ddufy1JZ6eihBCSJ64RARBXeg2rZpyT+xxhMEZLK5meOeR
911911
-----END RSA PRIVATE KEY-----
912912
";
913913
let key = decode_secret_key(key, Some("passphrase")).unwrap();
914-
let public = key.clone_public_key()?;
914+
let public = key.clone_public_key();
915915
let buf = b"blabla";
916916
let sig = key.sign_detached(buf).unwrap();
917917
assert!(public.verify_detached(buf, sig.as_ref()));
@@ -1052,7 +1052,7 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
10521052
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
10531053
}
10541054

1055-
let public = key.clone_public_key()?;
1055+
let public = key.clone_public_key();
10561056
let stream = tokio::net::UnixStream::connect(&agent_path).await?;
10571057
let mut client = agent::client::AgentClient::connect(stream);
10581058
client.add_identity(&key, &[]).await?;
@@ -1132,7 +1132,7 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
11321132
});
11331133
let key = decode_secret_key(PKCS8_ENCRYPTED, Some("blabla")).unwrap();
11341134
core.block_on(async move {
1135-
let public = key.clone_public_key()?;
1135+
let public = key.clone_public_key();
11361136
let stream = tokio::net::UnixStream::connect(&agent_path).await?;
11371137
let mut client = agent::client::AgentClient::connect(stream);
11381138
client

0 commit comments

Comments
 (0)