prople_crypto/aead/
types.rs

1use rst_common::standard::bytes::Bytes;
2
3use crate::errors::CommonError;
4use crate::types::{BytesValue, Value, VectorValue};
5
6pub struct Nonce(Vec<u8>);
7
8impl From<Vec<u8>> for Nonce {
9    fn from(value: Vec<u8>) -> Self {
10        Nonce(value)
11    }
12}
13
14impl VectorValue<u8> for Nonce {
15    fn vec(&self) -> Vec<u8> {
16        self.0.to_owned()
17    }
18}
19
20pub struct MessagePlain(Vec<u8>);
21
22impl From<Vec<u8>> for MessagePlain {
23    fn from(value: Vec<u8>) -> Self {
24        MessagePlain(value)
25    }
26}
27
28impl From<String> for MessagePlain {
29    fn from(value: String) -> Self {
30        MessagePlain(value.as_bytes().to_vec())
31    }
32}
33
34impl VectorValue<u8> for MessagePlain {
35    fn vec(&self) -> Vec<u8> {
36        self.0.to_owned()
37    }
38}
39
40pub struct MessageCipher(Vec<u8>);
41
42impl From<Vec<u8>> for MessageCipher {
43    fn from(value: Vec<u8>) -> Self {
44        MessageCipher(value)
45    }
46}
47
48impl From<String> for MessageCipher {
49    fn from(value: String) -> Self {
50        MessageCipher(value.as_bytes().to_vec())
51    }
52}
53
54impl VectorValue<u8> for MessageCipher {
55    fn vec(&self) -> Vec<u8> {
56        self.0.to_owned()
57    }
58}
59
60#[derive(Clone, Debug)]
61pub struct KeyEncryption([u8; 32]);
62
63impl BytesValue for KeyEncryption {
64    fn bytes(&self) -> Bytes {
65        Bytes::from(self.0.to_vec())
66    }
67}
68
69impl Value<[u8; 32]> for KeyEncryption {
70    fn get(&self) -> Result<[u8; 32], CommonError> {
71        let byte_slice = self.bytes().slice(0..32);
72        let byte_output = &byte_slice[..];
73
74        let output: Result<[u8; 32], CommonError> = <&[u8; 32]>::try_from(byte_output)
75            .map(|val| val.to_owned())
76            .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
77
78        output
79    }
80}
81
82impl TryFrom<Bytes> for KeyEncryption {
83    type Error = CommonError;
84
85    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
86        let mut val32 = Bytes::new();
87
88        if value.len() < 32 || value.is_empty() {
89            return Err(CommonError::ParseValueError(
90                "invalid bytes length".to_string(),
91            ));
92        }
93
94        if value.len() >= 32 {
95            let val_slice = value.slice(0..32);
96            val32 = Bytes::copy_from_slice(&val_slice);
97        }
98
99        let byte_slice = &val32[..];
100        let bytes_output: Result<&[u8; 32], CommonError> = <&[u8; 32]>::try_from(byte_slice)
101            .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
102
103        bytes_output.map(|val| KeyEncryption(val.to_owned()))
104    }
105}
106
107impl From<[u8; 32]> for KeyEncryption {
108    fn from(value: [u8; 32]) -> Self {
109        KeyEncryption(value)
110    }
111}
112
113#[derive(Clone, Debug)]
114pub struct KeyNonce([u8; 24]);
115
116impl Value<[u8; 24]> for KeyNonce {
117    fn get(&self) -> Result<[u8; 24], CommonError> {
118        let byte_slice = self.bytes().slice(0..24);
119        let byte_output = &byte_slice[..];
120
121        let output: Result<[u8; 24], CommonError> = <&[u8; 24]>::try_from(byte_output)
122            .map(|val| val.to_owned())
123            .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
124
125        output
126    }
127}
128
129impl TryFrom<Bytes> for KeyNonce {
130    type Error = CommonError;
131
132    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
133        let mut val24 = Bytes::new();
134
135        if value.len() < 24 || value.is_empty() {
136            return Err(CommonError::ParseValueError(
137                "mismatch bytes length".to_string(),
138            ));
139        }
140
141        if value.len() >= 24 {
142            let val_slice = value.slice(0..24);
143            val24 = Bytes::copy_from_slice(&val_slice);
144        }
145
146        let byte_slice = &val24[..];
147        let bytes_output: Result<&[u8; 24], CommonError> = <&[u8; 24]>::try_from(byte_slice)
148            .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
149
150        bytes_output.map(|val| KeyNonce(val.to_owned()))
151    }
152}
153
154impl BytesValue for KeyNonce {
155    fn bytes(&self) -> Bytes {
156        Bytes::from(self.0.to_vec())
157    }
158}
159
160impl From<[u8; 24]> for KeyNonce {
161    fn from(value: [u8; 24]) -> Self {
162        KeyNonce(value)
163    }
164}