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
use crate::{EncryptedPayload, Payload, RawPayload};

pub struct XorCypher<T: Payload> {
    inner: RawPayload,
    _marker: std::marker::PhantomData<T>,
}

impl<P: Payload> From<RawPayload> for XorCypher<P> {
    fn from(inner: RawPayload) -> Self {
        Self {
            inner,
            _marker: std::marker::PhantomData,
        }
    }
}

impl<'a, P: Payload> EncryptedPayload for XorCypher<P> {
    type Key = u8;
    type InnerPayload = P;

    fn decrypt(self, key: &Self::Key) -> P {
        let vec = self.inner.to_vec();
        vec.into_iter().map(|x| x ^ key).collect::<Vec<u8>>().into()
    }
}

pub trait XorCypherExt {
    fn xor_encrypt(self, key: &u8) -> RawPayload;
}

impl XorCypherExt for RawPayload {
    fn xor_encrypt(self, key: &u8) -> RawPayload {
        let mut inner = self.to_vec();
        for byte in inner.iter_mut() {
            *byte ^= key;
        }
        inner
    }
}