crypt_tool/
cipher.rs

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
use super::rng::LCG;

pub struct XorCipher {
    rng: LCG,
}

impl XorCipher {
    pub fn new(pwd: &[u8]) -> Self {
        Self {
            rng: LCG::from_seed(pwd),
        }
    }

    pub fn apply_xor(&self, data: &[u8]) -> Vec<u8> {
        let mut rng = self.rng.clone();
        data.iter().map(|byte1| byte1 ^ rng.generate_u8()).collect()
    }

    pub fn encode(&self, data: &[u8]) -> Vec<u8> {
        self.apply_xor(data)
    }
    pub fn decode(&self, data: &[u8]) -> Vec<u8> {
        self.apply_xor(data)
    }
}