Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion ssh-key/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use self::{

use crate::{
base64::{Decode, DecoderExt},
public, Algorithm, CipherAlg, Error, KdfAlg, KdfOptions, Result,
public, Algorithm, CipherAlg, Error, KdfAlg, KdfOptions, PublicKey, Result,
};
use core::str::FromStr;
use pem_rfc7468::{self as pem, PemLabel};
Expand Down Expand Up @@ -128,6 +128,27 @@ impl PrivateKey {
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
}

/// Get the [`PublicKey`] which corresponds to this private key.
pub fn public_key(&self) -> PublicKey {
PublicKey {
key_data: public::KeyData::from(&self.key_data),
#[cfg(feature = "alloc")]
comment: self.comment.clone(),
}
}
}

impl From<PrivateKey> for PublicKey {
fn from(private_key: PrivateKey) -> PublicKey {
private_key.public_key()
}
}

impl From<&PrivateKey> for PublicKey {
fn from(private_key: &PrivateKey) -> PublicKey {
private_key.public_key()
}
}

impl FromStr for PrivateKey {
Expand Down Expand Up @@ -263,3 +284,17 @@ impl Decode for KeypairData {
}
}
}

impl From<&KeypairData> for public::KeyData {
fn from(keypair_data: &KeypairData) -> public::KeyData {
match keypair_data {
#[cfg(feature = "alloc")]
KeypairData::Dsa(dsa) => public::KeyData::Dsa(dsa.into()),
#[cfg(feature = "ecdsa")]
KeypairData::Ecdsa(ecdsa) => public::KeyData::Ecdsa(ecdsa.into()),
KeypairData::Ed25519(ed25519) => public::KeyData::Ed25519(ed25519.into()),
#[cfg(feature = "alloc")]
KeypairData::Rsa(rsa) => public::KeyData::Rsa(rsa.into()),
}
}
}
12 changes: 12 additions & 0 deletions ssh-key/src/private/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ impl Decode for DsaKeypair {
}
}

impl From<DsaKeypair> for DsaPublicKey {
fn from(keypair: DsaKeypair) -> DsaPublicKey {
keypair.public
}
}

impl From<&DsaKeypair> for DsaPublicKey {
fn from(keypair: &DsaKeypair) -> DsaPublicKey {
keypair.public.clone()
}
}

impl fmt::Debug for DsaKeypair {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DsaKeypair")
Expand Down
12 changes: 12 additions & 0 deletions ssh-key/src/private/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ impl Decode for Ed25519Keypair {
}
}

impl From<Ed25519Keypair> for Ed25519PublicKey {
fn from(keypair: Ed25519Keypair) -> Ed25519PublicKey {
keypair.public
}
}

impl From<&Ed25519Keypair> for Ed25519PublicKey {
fn from(keypair: &Ed25519Keypair) -> Ed25519PublicKey {
keypair.public
}
}

impl fmt::Debug for Ed25519Keypair {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Ed25519Keypair")
Expand Down
12 changes: 12 additions & 0 deletions ssh-key/src/private/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ impl Decode for RsaKeypair {
}
}

impl From<RsaKeypair> for RsaPublicKey {
fn from(keypair: RsaKeypair) -> RsaPublicKey {
keypair.public
}
}

impl From<&RsaKeypair> for RsaPublicKey {
fn from(keypair: &RsaKeypair) -> RsaPublicKey {
keypair.public.clone()
}
}

impl fmt::Debug for RsaKeypair {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RsaKeypair")
Expand Down