turing_cipher/
lib.rs

1mod turing;
2mod turing_tables;
3
4pub use turing::Turing;
5
6#[cfg(test)]
7#[allow(dead_code)]
8mod tests {
9    use crate::Turing;
10
11    // 测试向量
12    const TEST_KEY: &[u8] = b"test key 128bits";
13    const TEST_FRAME: &[u8] = &[0, 0, 0, 0];
14    const TEST_OUT1: &str = "69 66 26 bb dc 6e 09 f6 da 9a ba b5 b5 6c 14 87 82 46 df 18";
15    const STREAM_OUT: &str = "5d a8 8c ed 8a a6 55 ba 78 08 ef f8 cf 32 63 c0 75 e4 40 3c";
16
17    // 十六进制工具函数
18    fn hex_to_bytes(hex: &str) -> Vec<u8> {
19        hex.split_whitespace()
20            .map(|b| u8::from_str_radix(b, 16).unwrap())
21            .collect()
22    }
23
24    fn bytes_to_hex(bytes: &[u8]) -> String {
25        bytes
26            .iter()
27            .map(|b| format!("{:02x}", b))
28            .collect::<Vec<_>>()
29            .join(" ")
30    }
31
32    #[test]
33    fn test_turing_basic() {
34        let mut cipher = Turing::new();
35        let mut output = [0u8; 340];
36
37        cipher.turing_key(TEST_KEY, TEST_KEY.len()).unwrap();
38        cipher.turing_iv(TEST_FRAME, TEST_FRAME.len()).unwrap();
39        let n = cipher.turing_gen(&mut output).unwrap();
40
41        assert_eq!(n, 340);
42
43        let first_20 = &output[..20];
44        let hex_output = bytes_to_hex(first_20);
45        assert_eq!(hex_output, TEST_OUT1);
46        println!("hex_output: {}", hex_output);
47    }
48}