1use crate::Block;
2
3pub struct NullCipher;
5
6impl NullCipher {
7 pub fn new() -> Self {
9 NullCipher
10 }
11}
12
13impl Block for NullCipher {
14 fn block_size(&self) -> usize {
16 1
17 }
18
19 fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize {
21 dst[..src.len()].copy_from_slice(src);
22 src.len()
23 }
24
25 fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize {
27 dst[..src.len()].copy_from_slice(src);
28 src.len()
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 #[test]
37 fn test_new_cipher() {
38 let _ = NullCipher::new();
39 }
40
41 #[test]
42 fn test_null_cipher_block_size() {
43 let c = NullCipher::new();
44 assert_eq!(c.block_size(), 1);
45 }
46
47 #[test]
48 fn test_null_cipher_encrypt() {
49 let c = NullCipher::new();
50 let tests = [
51 ("ABCDE", "ABCDE"),
52 ("COUCOU", "COUCOU"),
53 ];
54
55 for (pt, ct) in tests {
56 let plain = pt.as_bytes();
57 let mut cipher = vec![0u8; plain.len()];
58 c.encrypt(&mut cipher, plain);
59 assert_eq!(cipher, ct.as_bytes());
60 }
61 }
62
63 #[test]
64 fn test_null_cipher_decrypt() {
65 let c = NullCipher::new();
66 let tests = [
67 ("ABCDE", "ABCDE"),
68 ("COUCOU", "COUCOU"),
69 ];
70
71 for (pt, ct) in tests {
72 let cipher = ct.as_bytes();
73 let mut plain = vec![0u8; cipher.len()];
74 c.decrypt(&mut plain, cipher);
75 assert_eq!(plain, pt.as_bytes());
76 }
77 }
78}