1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use aes_gcm::aead::{Aead, NewAead};
use aes_gcm::{Aes256Gcm, Key, Nonce};
use async_trait::async_trait;
use crypto::digest::Digest;
use crypto::md5::Md5;
use crypto::scrypt::{scrypt_simple, ScryptParams};
use crate::errors::encryption::EncryptionError;
use crate::Result;
const ALLOWED_SECRET_CHAR_LENGTH_RANGE: std::ops::RangeInclusive<usize> = 5..=50;
#[doc(hidden)]
const SCRYPT_PARAM_P: u32 = 1;
#[doc(hidden)]
const SCRYPT_PARAM_R: u32 = 8;
#[doc(hidden)]
const SCRYPT_PARAM_LOG_N: u8 = 2;
#[derive(Clone, Default, Debug)]
pub struct Secret {
inner: String,
}
impl Secret {
#[must_use]
pub fn new(secret: &str) -> Self {
Self {
inner: secret.to_owned(),
}
}
pub fn into_hashed(self) -> Result<HashedSecret> {
let (log_n, r, p) = (SCRYPT_PARAM_LOG_N, SCRYPT_PARAM_R, SCRYPT_PARAM_P);
let params = ScryptParams::new(log_n, r, p);
let scrypt_hash = scrypt_simple(self.inner.as_str(), ¶ms)?;
Ok(HashedSecret { inner: scrypt_hash })
}
pub fn check_consume(self) -> Result<Self> {
<Self as Check>::length(&self)?;
Ok(self)
}
}
#[derive(Clone, Default, Debug)]
pub struct HashedSecret {
inner: String,
}
impl HashedSecret {
#[must_use]
pub fn to_str(&self) -> &str {
&self.inner
}
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
self.inner.clone().into_bytes()
}
}
#[async_trait]
trait Check {
fn length(&self) -> Result<()>;
}
#[async_trait]
impl Check for Secret {
fn length(&self) -> Result<()> {
if ALLOWED_SECRET_CHAR_LENGTH_RANGE.contains(&self.inner.len()) {
Ok(())
} else {
Err(EncryptionError::SecretLength.into())
}
}
}
#[must_use]
pub fn digest_md5_multi(inputs: &[&[u8]]) -> String {
let mut hasher = Md5::new();
inputs.iter().for_each(|&i| hasher.input(i));
hasher.result_str()
}
#[must_use]
pub fn digest_md5(input: &[u8]) -> String {
let mut hasher = Md5::new();
hasher.input(input);
hasher.result_str()
}
pub fn encrypt_aes256_u12nonce(secret: &[u8], raw_data: &[u8]) -> Result<(Vec<u8>, Vec<u8>)> {
let hashed_key = digest_md5(secret);
let key = Key::from_slice(hashed_key.as_bytes());
let cipher = Aes256Gcm::new(key);
let magic: [u8; 12] = rand::random();
let nonce = Nonce::from_slice(&magic);
let encrypted = cipher
.encrypt(nonce, raw_data.as_ref())
.map_err(EncryptionError::Cipher)?;
Ok((encrypted, nonce.to_vec()))
}
pub fn decrypt_aes256_u12nonce(
secret: &[u8],
encrypted_data: &[u8],
magic: &[u8; 12],
) -> Result<Vec<u8>> {
let hashed_key = digest_md5(secret);
let key = Key::from_slice(hashed_key.as_bytes());
let cipher = Aes256Gcm::new(key);
let nonce = Nonce::from_slice(magic);
let decrypted = cipher
.decrypt(nonce, encrypted_data.as_ref())
.map_err(EncryptionError::Cipher)?;
Ok(decrypted)
}