ssh-key 0.7.0

Pure Rust implementation of SSH key file format decoders/encoders as described in RFC4251/RFC4253 and OpenSSH key formats, as well as "sshsig" signatures and certificates (including certificate validation and certificate authority support), with further support for the `authorized_keys` and `known_hosts` file formats.
Documentation
//! Ed25519 public keys.
//!
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{Error, Result};
use core::fmt;
use encoding::{Decode, Encode};

/// Ed25519 public key.
///
/// Encodings for Ed25519 public keys are described in [RFC8709 § 4]:
///
/// > The "ssh-ed25519" key format has the following encoding:
/// >
/// > **string** "ssh-ed25519"
/// >
/// > **string** key
/// >
/// > Here, 'key' is the 32-octet public key described in RFC8032
///
/// [RFC8709 § 4]: https://datatracker.ietf.org/doc/html/rfc8709#section-4
#[derive(Copy, Clone, Debug, Decode, Encode, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[ssh(length_prefixed, error = Error)]
pub struct Ed25519PublicKey(pub [u8; Self::BYTE_SIZE]);

impl Ed25519PublicKey {
    /// Size of an Ed25519 public key in bytes.
    pub const BYTE_SIZE: usize = 32;
}

impl AsRef<[u8; Self::BYTE_SIZE]> for Ed25519PublicKey {
    fn as_ref(&self) -> &[u8; Self::BYTE_SIZE] {
        &self.0
    }
}

impl TryFrom<&[u8]> for Ed25519PublicKey {
    type Error = Error;

    fn try_from(bytes: &[u8]) -> Result<Self> {
        Ok(Self(bytes.try_into()?))
    }
}

impl fmt::Display for Ed25519PublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:X}")
    }
}

impl fmt::LowerHex for Ed25519PublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in self.as_ref() {
            write!(f, "{byte:02x}")?;
        }
        Ok(())
    }
}

impl fmt::UpperHex for Ed25519PublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in self.as_ref() {
            write!(f, "{byte:02X}")?;
        }
        Ok(())
    }
}

#[cfg(feature = "ed25519")]
impl TryFrom<Ed25519PublicKey> for ed25519_dalek::VerifyingKey {
    type Error = Error;

    fn try_from(key: Ed25519PublicKey) -> Result<ed25519_dalek::VerifyingKey> {
        ed25519_dalek::VerifyingKey::try_from(&key)
    }
}

#[cfg(feature = "ed25519")]
impl TryFrom<&Ed25519PublicKey> for ed25519_dalek::VerifyingKey {
    type Error = Error;

    fn try_from(key: &Ed25519PublicKey) -> Result<ed25519_dalek::VerifyingKey> {
        ed25519_dalek::VerifyingKey::from_bytes(key.as_ref()).map_err(|_| Error::Crypto)
    }
}

#[cfg(feature = "ed25519")]
impl From<ed25519_dalek::VerifyingKey> for Ed25519PublicKey {
    fn from(key: ed25519_dalek::VerifyingKey) -> Ed25519PublicKey {
        Ed25519PublicKey::from(&key)
    }
}

#[cfg(feature = "ed25519")]
impl From<&ed25519_dalek::VerifyingKey> for Ed25519PublicKey {
    fn from(key: &ed25519_dalek::VerifyingKey) -> Ed25519PublicKey {
        Ed25519PublicKey(key.to_bytes())
    }
}