deadbolt_parser/
algorithms.rs

1#![allow(clippy::should_implement_trait)]
2
3#[derive(PartialEq)]
4pub enum EncryptionAlg {
5    AesGcm,
6    ChaCha20Poly1305,
7}
8
9impl EncryptionAlg {
10    pub fn value(&self) -> u8 {
11        match *self {
12            EncryptionAlg::AesGcm => 1,
13            EncryptionAlg::ChaCha20Poly1305 => 2,
14        }
15    }
16
17    pub fn from_str(input: &str) -> EncryptionAlg {
18        match input {
19            "aes-gcm" => EncryptionAlg::AesGcm,
20            "chacha20-poly1305" => EncryptionAlg::ChaCha20Poly1305,
21            _ => EncryptionAlg::AesGcm,
22        }
23    }
24
25    pub fn from_u8(input: u8) -> EncryptionAlg {
26        match input {
27            1 => EncryptionAlg::AesGcm,
28            2 => EncryptionAlg::ChaCha20Poly1305,
29            _ => EncryptionAlg::AesGcm,
30        }
31    }
32
33    pub fn to_str(&self) -> &str {
34        match *self {
35            EncryptionAlg::AesGcm => "aes-gcm",
36            EncryptionAlg::ChaCha20Poly1305 => "chacha20-poly1305",
37        }
38    }
39}
40
41#[derive(PartialEq)]
42pub enum HashAlg {
43    Sha256,
44}
45
46impl HashAlg {
47    pub fn value(&self) -> u8 {
48        match *self {
49            HashAlg::Sha256 => 1,
50        }
51    }
52
53    pub fn from_str(input: &str) -> HashAlg {
54        match input {
55            "sha-256" => HashAlg::Sha256,
56            _ => HashAlg::Sha256,
57        }
58    }
59
60    pub fn from_u8(input: u8) -> HashAlg {
61        match input {
62            1 => HashAlg::Sha256,
63            _ => HashAlg::Sha256,
64        }
65    }
66
67    pub fn to_str(&self) -> &str {
68        match *self {
69            HashAlg::Sha256 => "sha-256",
70        }
71    }
72}
73
74#[derive(PartialEq)]
75pub enum KdfAlg {
76    Argon2d,
77    Argon2id,
78}
79
80impl KdfAlg {
81    pub fn value(&self) -> u8 {
82        match *self {
83            KdfAlg::Argon2d => 1,
84            KdfAlg::Argon2id => 2,
85        }
86    }
87
88    pub fn from_u8(input: u8) -> KdfAlg {
89        match input {
90            1 => KdfAlg::Argon2d,
91            2 => KdfAlg::Argon2id,
92            _ => KdfAlg::Argon2d,
93        }
94    }
95
96    pub fn from_str(input: &str) -> KdfAlg {
97        match input {
98            "argon2d" => KdfAlg::Argon2d,
99            "argon2id" => KdfAlg::Argon2id,
100            _ => KdfAlg::Argon2d,
101        }
102    }
103
104    pub fn to_str(&self) -> &str {
105        match *self {
106            KdfAlg::Argon2d => "argon2d",
107            KdfAlg::Argon2id => "argon2id",
108        }
109    }
110}
111
112#[derive(PartialEq)]
113pub enum CompressionAlg {
114    None,
115    Gzip,
116}
117
118impl CompressionAlg {
119    pub fn value(&self) -> u8 {
120        match *self {
121            CompressionAlg::None => 0,
122            CompressionAlg::Gzip => 1,
123        }
124    }
125
126    pub fn from_bool(input: bool) -> CompressionAlg {
127        match input {
128            true => CompressionAlg::Gzip,
129            false => CompressionAlg::None,
130        }
131    }
132
133    pub fn from_u8(input: u8) -> CompressionAlg {
134        match input {
135            0 => CompressionAlg::None,
136            1 => CompressionAlg::Gzip,
137            _ => CompressionAlg::Gzip,
138        }
139    }
140
141    pub fn to_bool(&self) -> bool {
142        match *self {
143            CompressionAlg::Gzip => true,
144            CompressionAlg::None => false,
145        }
146    }
147}