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
//! A fixed-offset based caesar cipher middleware.

use Error;
use wire;

use std::num::Wrapping;

/// Middleware that rotates each transmitted byte by a fixed offset.
///
/// **NOTE**: This is not really useful in real life.
#[derive(Copy, Clone, Debug)]
pub struct RotateBytes {
    /// The integer offset to rotate by.
    pub amount: u8,
}

impl RotateBytes {
    /// The ROT13 cipher.
    ///
    /// [ROT13 on Wikipedia](https://en.wikipedia.org/wiki/ROT13)
    pub const ROT13: RotateBytes = RotateBytes { amount: 13 };
}

impl wire::Middleware for RotateBytes {
    fn encode_data(&mut self, data: Vec<u8>) -> Result<Vec<u8>, Error> {
        Ok(data.into_iter().map(|b| (Wrapping(b) + Wrapping(self.amount)).0).collect())
    }

    fn decode_data(&mut self, data: Vec<u8>) -> Result<Vec<u8>, Error> {
        Ok(data.into_iter().map(|b| (Wrapping(b) - Wrapping(self.amount)).0).collect())
    }
}

#[cfg(test)]
mod test {
    use super::RotateBytes;
    use wire::Middleware;

    #[test]
    fn bytes_encoded_correctly() {
        assert_eq!(RotateBytes::ROT13.encode_data(vec![0, 1, 2, 3, 4]).unwrap(),
                   vec![13, 14, 15, 16, 17]);
    }

    #[test]
    fn bytes_decoded_correctly() {
        assert_eq!(RotateBytes::ROT13.decode_data(vec![13, 14, 15, 16, 17]).unwrap(),
                   vec![0, 1, 2, 3, 4]);
    }

    #[test]
    fn bytes_read_back_correctly() {
        assert_eq!(RotateBytes::ROT13.encode_data(RotateBytes::ROT13.decode_data(vec![2, 4, 8, 32, 254]).unwrap()).unwrap(),
                   vec![2, 4, 8, 32, 254]);
    }
}