ptero/method/complex/
eluv.rs

1//! # Description
2//!
3//! This method implements the Extended Line Unicode Variant (ELUV) steganography algorithm. It consist of three
4//! simpler methods:
5//! * [RandomWhitespaceMethod](crate::method::random_whitespace::RandomWhitespaceMethod),
6//! * [LineExtendMethod](crate::method::line_extend::LineExtendMethod),
7//! * [TrailingUnicodeMethod](crate::method::trailing_unicode::TrailingUnicodeMethod).
8//!
9//! For more info read docs on each one of the above encoders.
10
11use trailing_unicode::character_sets::CharacterSetType;
12
13use crate::{
14    context::{PivotByLineContext, PivotByRawLineContext},
15    impl_complex_decoder, impl_complex_encoder,
16    method::{line_extend, random_whitespace, trailing_unicode, Method},
17};
18
19type ELUVSubmethod = Box<dyn Method<PivotByLineContext, PivotByRawLineContext>>;
20/// Structure representing the ELUV algorithm.
21/// Contains the vector of used methods. Uses macros to implement the required traits.
22pub struct ELUVMethod {
23    methods: Vec<ELUVSubmethod>,
24}
25
26#[derive(Debug, PartialEq)]
27pub enum ELUVMethodVariant {
28    Variant1,
29    Variant2,
30    Variant3,
31}
32
33pub struct ELUVMethodBuilder {
34    character_set: CharacterSetType,
35    variant: ELUVMethodVariant,
36}
37
38impl ELUVMethodBuilder {
39    pub fn new() -> Self {
40        ELUVMethodBuilder {
41            character_set: CharacterSetType::FullUnicodeSet,
42            variant: ELUVMethodVariant::Variant1,
43        }
44    }
45
46    pub fn character_set(mut self, set: CharacterSetType) -> Self {
47        self.character_set = set;
48        self
49    }
50
51    pub fn variant(mut self, variant: ELUVMethodVariant) -> Self {
52        self.variant = variant;
53        self
54    }
55
56    fn select_methods(&self) -> Vec<ELUVSubmethod> {
57        let indices = match self.variant {
58            ELUVMethodVariant::Variant1 => &[0, 1, 2],
59            ELUVMethodVariant::Variant2 => &[1, 0, 2],
60            ELUVMethodVariant::Variant3 => &[1, 2, 0],
61        };
62
63        indices
64            .iter()
65            .map(|i| {
66                let method: ELUVSubmethod = match i {
67                    0 => Box::new(random_whitespace::RandomWhitespaceMethod::default()),
68                    1 => Box::new(line_extend::LineExtendMethod::default()),
69                    _ => Box::new(trailing_unicode::TrailingUnicodeMethod::new(
70                        self.character_set,
71                    )),
72                };
73                method
74            })
75            .collect()
76    }
77
78    pub fn build(&self) -> ELUVMethod {
79        ELUVMethod {
80            methods: self.select_methods(),
81        }
82    }
83}
84
85impl Default for ELUVMethod {
86    fn default() -> Self {
87        ELUVMethodBuilder::new().build()
88    }
89}
90
91impl_complex_encoder!(ELUVMethod, PivotByLineContext);
92impl_complex_decoder!(ELUVMethod, PivotByRawLineContext);
93
94impl Method<PivotByLineContext, PivotByRawLineContext> for ELUVMethod {
95    fn method_name(&self) -> String {
96        format!(
97            "ELUVMethod({},{},{})",
98            self.methods[0].method_name(),
99            self.methods[1].method_name(),
100            self.methods[2].method_name(),
101        )
102    }
103}
104
105#[allow(unused_imports)]
106mod test {
107    use std::error::Error;
108
109    use crate::{
110        binary::BitIterator,
111        cli::encoder::EncodeSubCommand,
112        context::{PivotByLineContext, PivotByRawLineContext},
113        decoder::Decoder,
114        encoder::Encoder,
115        method::{random_whitespace::RandomWhitespaceMethod, Method},
116    };
117
118    use super::{ELUVMethod, ELUVMethodBuilder, ELUVMethodVariant};
119
120    #[test]
121    fn encodes_text_data() -> Result<(), Box<dyn Error>> {
122        let cover_input = "a b c ".repeat(5);
123        let data_input = "abc";
124        let pivot: usize = 3;
125
126        let mut data_iterator = BitIterator::new(&data_input.as_bytes());
127        let method = ELUVMethod::default();
128        let mut context = PivotByLineContext::new(&cover_input, pivot);
129        let stego_text = method.encode(&mut context, &mut data_iterator, None)?;
130
131        assert_eq!(
132            &stego_text,
133            "a b c\u{2028}\na  b\u{2062}\nc  a\u{200b}\nb c a\u{2028}\nb c\na b\nc \n"
134        );
135        Ok(())
136    }
137
138    #[test]
139    fn encodes_binary_data() -> Result<(), Box<dyn Error>> {
140        let cover_input = "a b c ".repeat(6);
141        let data_input: Vec<u8> = vec![0b11101010, 0b10000011, 0b01011110];
142        let pivot: usize = 3;
143
144        let mut data_iterator = BitIterator::new(&data_input);
145        let method = ELUVMethod::default();
146        let mut context = PivotByLineContext::new(&cover_input, pivot);
147        let stego_text = method.encode(&mut context, &mut data_iterator, None)?;
148
149        assert_eq!(
150            &stego_text,
151            "a  b c\u{205f}\na b c\na  b c\u{200a}\na  b c\na b\nc a\nb c \n"
152        );
153        Ok(())
154    }
155
156    #[test]
157    fn decodes_binary_data() -> Result<(), Box<dyn Error>> {
158        let stego_text = "a  bc\na bcd \na  b d\u{205f}\n";
159        let pivot: usize = 4;
160
161        let method = ELUVMethod::default();
162        let mut context = PivotByRawLineContext::new(&stego_text, pivot);
163        let secret_data = method.decode(&mut context, None)?;
164
165        assert_eq!(&secret_data, &[0b10_00000_0, 0b1_00001_11, 0b10101_000]);
166        Ok(())
167    }
168
169    #[test]
170    fn decodes_zeroes_if_no_data_encoded() -> Result<(), Box<dyn Error>> {
171        let stego_text = "a\n".repeat(5);
172        let pivot: usize = 4;
173
174        let method = ELUVMethod::default();
175        let mut context = PivotByRawLineContext::new(&stego_text, pivot);
176        let secret_data = method.decode(&mut context, None)?;
177
178        assert_eq!(&secret_data, &[0, 0, 0, 0, 0]);
179        Ok(())
180    }
181
182    #[test]
183    fn default_method_is_variant_1() -> Result<(), Box<dyn Error>> {
184        assert_eq!(
185            ELUVMethod::default().method_name(),
186            "ELUVMethod(RandomWhitespaceMethod,LineExtendMethod,TrailingUnicodeMethod)"
187        );
188        Ok(())
189    }
190
191    #[test]
192    fn builder_properly_constructs_variants() -> Result<(), Box<dyn Error>> {
193        assert_eq!(
194            ELUVMethodBuilder::new()
195                .variant(ELUVMethodVariant::Variant1)
196                .build()
197                .method_name(),
198            "ELUVMethod(RandomWhitespaceMethod,LineExtendMethod,TrailingUnicodeMethod)"
199        );
200        assert_eq!(
201            ELUVMethodBuilder::new()
202                .variant(ELUVMethodVariant::Variant2)
203                .build()
204                .method_name(),
205            "ELUVMethod(LineExtendMethod,RandomWhitespaceMethod,TrailingUnicodeMethod)"
206        );
207        assert_eq!(
208            ELUVMethodBuilder::new()
209                .variant(ELUVMethodVariant::Variant3)
210                .build()
211                .method_name(),
212            "ELUVMethod(LineExtendMethod,TrailingUnicodeMethod,RandomWhitespaceMethod)"
213        );
214        Ok(())
215    }
216}