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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#[macro_use]
extern crate log;
use std::io::Read;
pub trait Xor {
fn xor(&mut self, key_bytes : Vec<u8>) -> Vec<u8>;
}
impl<'a, R: Read> Xor for &'a mut R {
fn xor(&mut self, key_bytes : Vec<u8>) -> Vec<u8> {
let mut key_idx = 0;
let mut warning_shown = false;
let mut encoded_bytes: Vec<u8> = Vec::new();
loop {
let mut data = [0; 1024];
let num_read = self.read(&mut data[..]).unwrap();
if num_read == 0 {
break;
}
let data_bytes = &data[0 .. num_read];
for b in data_bytes {
let k = key_bytes[key_idx];
let e = b ^ k;
encoded_bytes.push(e);
key_idx += 1;
if key_idx >= key_bytes.len() {
key_idx = key_idx % key_bytes.len();
if !warning_shown {
warning_shown = true;
warn!("Key wasn't long enough and had to be re-used to fully encode data, use a longer key to be secure.");
}
}
}
}
encoded_bytes
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn xor_works() {
let data : Vec<u8> = vec![0b11111111u8, 0b11111111u8, 0b00001111u8, 0b10101010u8, 0b11111111u8, 0b11111111u8, 0b00001111u8, 0b10101010u8];
let key : Vec<u8> = vec![0b11111111u8, 0b00000000u8, 0b11110000u8, 0b01010101u8];
let mut input = Cursor::new(data);
let mut reader = &mut input as &mut Read;
let cipher = reader.xor(key);
assert_eq!(0b00000000u8, cipher[0]);
assert_eq!(0b11111111u8, cipher[1]);
assert_eq!(0b11111111u8, cipher[2]);
assert_eq!(0b11111111u8, cipher[3]);
assert_eq!(0b00000000u8, cipher[4]);
assert_eq!(0b11111111u8, cipher[5]);
assert_eq!(0b11111111u8, cipher[6]);
assert_eq!(0b11111111u8, cipher[7]);
}
}