ate_crypto/crypto/
key_size.rs

1use serde::{Deserialize, Serialize};
2use std::result::Result;
3#[allow(unused_imports)]
4use tracing::{debug, error, info, instrument, span, trace, warn, Level};
5
6/// Size of a cryptographic key, smaller keys are still very secure but
7/// have less room in the future should new attacks be found against the
8/// crypto algorithms used by ATE.
9#[repr(u8)]
10#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
11pub enum KeySize {
12    #[allow(dead_code)]
13    Bit128 = 16,
14    #[allow(dead_code)]
15    Bit192 = 24,
16    #[allow(dead_code)]
17    Bit256 = 32,
18}
19
20impl KeySize {
21    pub fn as_str(&self) -> &str {
22        match &self {
23            KeySize::Bit128 => "128",
24            KeySize::Bit192 => "192",
25            KeySize::Bit256 => "256",
26        }
27    }
28}
29
30impl std::str::FromStr for KeySize {
31    type Err = &'static str;
32
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        match s {
35            "128" => Ok(KeySize::Bit128),
36            "192" => Ok(KeySize::Bit192),
37            "256" => Ok(KeySize::Bit256),
38            _ => Err("valid values are '128', '192', '256'"),
39        }
40    }
41}
42
43impl std::fmt::Display for KeySize {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            KeySize::Bit128 => write!(f, "128"),
47            KeySize::Bit192 => write!(f, "192"),
48            KeySize::Bit256 => write!(f, "256"),
49        }
50    }
51}