Skip to main content

affinidi_crypto/
key_type.rs

1//! Key type enumeration
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6use zeroize::Zeroize;
7
8use crate::CryptoError;
9
10/// Known cryptographic key types
11#[derive(Debug, Default, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Zeroize)]
12pub enum KeyType {
13    Ed25519,
14    X25519,
15    P256,
16    P384,
17    P521,
18    Secp256k1,
19    #[default]
20    Unknown,
21}
22
23impl TryFrom<&str> for KeyType {
24    type Error = CryptoError;
25
26    fn try_from(value: &str) -> Result<Self, Self::Error> {
27        match value {
28            "Ed25519" => Ok(KeyType::Ed25519),
29            "X25519" => Ok(KeyType::X25519),
30            "P-256" => Ok(KeyType::P256),
31            "P-384" => Ok(KeyType::P384),
32            "P-521" => Ok(KeyType::P521),
33            "secp256k1" => Ok(KeyType::Secp256k1),
34            _ => Err(CryptoError::UnsupportedKeyType(value.to_string())),
35        }
36    }
37}
38
39impl fmt::Display for KeyType {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        match self {
42            KeyType::Ed25519 => write!(f, "Ed25519"),
43            KeyType::X25519 => write!(f, "X25519"),
44            KeyType::P256 => write!(f, "P-256"),
45            KeyType::P384 => write!(f, "P-384"),
46            KeyType::P521 => write!(f, "P-521"),
47            KeyType::Secp256k1 => write!(f, "secp256k1"),
48            KeyType::Unknown => write!(f, "Unknown"),
49        }
50    }
51}