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
use super::Error;
use aes_ctr::stream_cipher::generic_array::GenericArray;
use aes_ctr::stream_cipher::{NewFixStreamCipher, StreamCipherCore};
use aes_ctr::Aes128Ctr;
use std::fmt;
use std::str::FromStr;
pub const AES128_CTR_CIPHER_NAME: &str = "aes-128-ctr";
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cipher {
#[serde(rename = "aes-128-ctr")]
Aes128Ctr,
}
impl Cipher {
pub fn encrypt(&self, data: &[u8], key: &[u8], iv: &[u8]) -> Vec<u8> {
let key = GenericArray::from_slice(key);
let iv = GenericArray::from_slice(iv);
let mut buf = data.to_vec();
let mut ctr = Aes128Ctr::new(key, iv);
ctr.apply_keystream(&mut buf);
buf
}
}
impl Default for Cipher {
fn default() -> Self {
Cipher::Aes128Ctr
}
}
impl FromStr for Cipher {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
_ if s == AES128_CTR_CIPHER_NAME => Ok(Cipher::Aes128Ctr),
_ => Err(Error::UnsupportedCipher(s.to_string())),
}
}
}
impl fmt::Display for Cipher {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Cipher::Aes128Ctr => f.write_str(AES128_CTR_CIPHER_NAME),
}
}
}