use ed25519_dalek::{Keypair, PublicKey, Signature};
use std::borrow::Cow;
use crate::{Algorithm, AlgorithmSignature, Renamed};
impl AlgorithmSignature for Signature {
fn try_from_slice(bytes: &[u8]) -> anyhow::Result<Self> {
Self::from_bytes(bytes).map_err(Into::into)
}
fn as_bytes(&self) -> Cow<[u8]> {
Cow::Owned(self.to_bytes().to_vec())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Ed25519;
impl Ed25519 {
pub fn with_specific_name() -> Renamed<Self> {
Renamed::new(Self, "Ed25519")
}
}
impl Algorithm for Ed25519 {
type SigningKey = Keypair;
type VerifyingKey = PublicKey;
type Signature = Signature;
fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("EdDSA")
}
fn sign(&self, signing_key: &Self::SigningKey, message: &[u8]) -> Self::Signature {
signing_key.sign(message)
}
fn verify_signature(
&self,
signature: &Self::Signature,
verifying_key: &Self::VerifyingKey,
message: &[u8],
) -> bool {
verifying_key.verify(message, signature).is_ok()
}
}