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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2019 astonbitecode
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*!
# bacon-cipher
An implementation of the [Bacon's cipher](https://en.wikipedia.org/wiki/Bacon%27s_cipher).
The crate offers codecs that _encode / decode_ and steganographers that _hide / reveal_ encoded messages.
**Available codecs:**
* CharCodec: A codec that encodes data of type `char`.
The encoding is done by substituting with two given elements (`elem_a` and `elem_b`) of type `T`.
The substitution is done using the __first__ version of the Bacon's cipher.
* CharCodecV2: A codec that encodes data of type `char`.
The encoding is done by substituting with two given elements (`elem_a` and `elem_b`) of type `T`.
The substitution is done using the __second__ version of the Bacon's cipher.
**Available steganographers:**
* LetterCaseSteganographer: Applies steganography based on the case of the characters.
E.g. Lowercase for Bacon's element A, capital for Bacon's element B.
* MarkdownSteganographer: Applies steganography based on Markdown tags that surround elements.
E.g. Sourround an element with `**` for Bacon's element A and the rest of the elements are considered as Bacon's element B.
* SimpleTagSteganographer: Applies steganography based on HTML or XML tags that surround elements. (needs the feature `extended-steganography`)
E.g. Sourround an element with `<b>` and `</b>` for Bacon's element A and with `<i>` and `</i>` for Bacon's element B.
## Encoding - Decoding
### Encode a message to Bacon codes
```
use bacon_cipher::codecs::char_codec::CharCodec;
use bacon_cipher::BaconCodec;
use std::iter::FromIterator;
// Define a Bacon Codec that encodes using the characters 'A' and 'B'
let codec = CharCodec::new('A', 'B');
// This is the secret to encode
let secret: Vec<char> = "My secret".chars().collect();
// Get the encoded chars
let encoded_chars = codec.encode(&secret);
let encoded_string = String::from_iter(encoded_chars.iter());
assert_eq!("ABABBBABBABAAABAABAAAAABABAAAAAABAABAABA", encoded_string);
```
### Decode Bacon codes
```
use bacon_cipher::codecs::char_codec::CharCodec;
use bacon_cipher::BaconCodec;
use std::iter::FromIterator;
// Define a Bacon Codec that encodes using the characters 'A' and 'B'
let codec = CharCodec::new('A', 'B');
// These are the encoded characters
let encoded_chars: Vec<char> = "ABABBBABBABAAABAABAAAAABABAAAAAABAABAABA".chars().collect();
// Retrieve the decoded chars
let decoded = codec.decode(&encoded_chars);
let string = String::from_iter(decoded.iter());
assert_eq!("MYSECRET", string);
```
## Steganography
### Letter case
#### Disguise a hidden message into a public one
```
use bacon_cipher::codecs::char_codec::CharCodec;
use bacon_cipher::stega::letter_case::LetterCaseSteganographer;
use bacon_cipher::{BaconCodec, Steganographer};
use std::iter::FromIterator;
// Define a Bacon Codec that encodes using the characters 'A' and 'B'
let codec = CharCodec::new('a', 'b');
// Apply steganography based on the case of the characters
let s = LetterCaseSteganographer::new();
// This is the public message in which we want to hide the secret one.
let public_chars: Vec<char> = "This is a public message that contains a secret one".chars().collect();
// This is the message that we want to hide.
let secret_chars: Vec<char> = "My secret".chars().collect();
// This is the public message that contains the secret one
let disguised_public = s.disguise(&secret_chars, &public_chars, &codec);
let string = String::from_iter(disguised_public.unwrap().iter());
assert!(string == "tHiS IS a PUbLic mEssAge thaT cOntains A seCreT one");
```
#### Reveal a hidden message from a public one
```
use bacon_cipher::codecs::char_codec::CharCodec;
use bacon_cipher::stega::letter_case::LetterCaseSteganographer;
use bacon_cipher::{BaconCodec, Steganographer};
use std::iter::FromIterator;
// Define a Bacon Codec that encodes using the characters 'A' and 'B'
let codec = CharCodec::new('a', 'b');
// Apply steganography based on the case of the characters
let s = LetterCaseSteganographer::new();
// This is the public message that contains a hidden message
let public_chars: Vec<char> = "tHiS IS a PUbLic mEssAge thaT cOntains A seCreT one".chars().collect();
// This is the hidden message
let output = s.reveal(&public_chars, &codec);
let hidden_message = String::from_iter(output.unwrap().iter());
assert!(hidden_message.starts_with("MYSECRET"));
```
### Markdown
#### Disguise a hidden message into a public one
```
use bacon_cipher::codecs::char_codec::CharCodec;
use bacon_cipher::stega::markdown::{MarkdownSteganographer, Marker};
use bacon_cipher::{BaconCodec, Steganographer};
use std::iter::FromIterator;
// Define a Bacon Codec that encodes using the characters 'A' and 'B'
let codec = CharCodec::new('a', 'b');
// Apply steganography based on Markdown markers
let s = MarkdownSteganographer::new(
Marker::empty(),
Marker::new(
Some("*"),
Some("*"))).unwrap();
// This is the public message in which we want to hide the secret one.
let public = "This is a public message that contains a secret one";
// This is the message that we want to hide.
let secret_chars: Vec<char> = "My secret".chars().collect();
let output = s.disguise(
&secret_chars,
&Vec::from_iter(public.chars()),
&codec);
let string = String::from_iter(output.unwrap().iter());
assert!(string == "T*h*i*s* *is* a *pu*b*l*ic m*e*ss*a*ge tha*t* c*o*ntains *a* se*c*re*t* one");
```
#### Reveal a hidden message from a public one
```
use bacon_cipher::codecs::char_codec::CharCodec;
use bacon_cipher::stega::markdown::{MarkdownSteganographer, Marker};
use bacon_cipher::{BaconCodec, Steganographer};
use std::iter::FromIterator;
// Define a Bacon Codec that encodes using the characters 'A' and 'B'
let codec = CharCodec::new('a', 'b');
// Apply steganography based on Markdown markers
let s = MarkdownSteganographer::new(
Marker::empty(),
Marker::new(
Some("*"),
Some("*"))).unwrap();
// This is the public message that contains a hidden message
let public = "T*h*i*s* *is* a *pu*b*l*ic m*e*ss*a*ge tha*t* c*o*ntains *a* se*c*re*t* one";
// This is the hidden message
let output = s.reveal(
&Vec::from_iter(public.chars()),
&codec);
assert!(output.is_ok());
let string = String::from_iter(output.unwrap().iter());
assert!(string.starts_with("MYSECRET"));
```
## Licence
At your option, under:
* Apache License, Version 2.0, (http://www.apache.org/licenses/LICENSE-2.0)
* MIT license (http://opensource.org/licenses/MIT)
*/
/// A codec that enables encoding and decoding based on the [Bacon's cipher](https://en.wikipedia.org/wiki/Bacon%27s_cipher)
/// Transforms a given input of elements to / from a different form, based on a [BaconCodec](trait.BaconCodec.html).