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
use crate::{input, Cipher, CipherInputError, CipherResult};
pub struct FourSquare {
key1: String,
key2: String,
alphabet: String,
pad: u8,
}
impl FourSquare {
pub fn new(key1: &str, key2: &str, alphabet: &str, pad: char) -> Self {
assert_eq!(alphabet.len(), 25, "alphabet` must be 25 chars in length");
input::is_ascii(alphabet).expect("`alphabet` must be valid ascii");
input::no_repeated_chars(alphabet).expect("`alphabet` cannot contain repeated chars");
assert_eq!(key1.len(), 25, "`key1` must be 25 chars in length");
input::no_repeated_chars(key1).expect("`key1` cannot contain repeated chars");
input::in_alphabet(key1, alphabet)
.expect("all chars in `key1` must be contained in `alphabet`");
assert_eq!(key2.len(), 25, "`key2` must be 25 chars in length");
input::no_repeated_chars(key2).expect("`key2` cannot contain repeated chars");
input::in_alphabet(key2, alphabet)
.expect("all chars in `key2` must be contained in `alphabet`");
assert_ne!(alphabet.find(pad), None, "`alphabet` must contain `pad`");
Self {
key1: String::from(key1),
key2: String::from(key2),
alphabet: String::from(alphabet),
pad: pad as u8,
}
}
}
impl Cipher for FourSquare {
fn encipher(&self, ptext: &str) -> CipherResult {
input::in_alphabet(ptext, &self.alphabet)?;
let mut ptext: Vec<u8> = ptext.bytes().collect();
if ptext.len() % 2 != 0 {
ptext.push(self.pad);
}
let key1 = self.key1.as_bytes();
let key2 = self.key2.as_bytes();
let mut ctext = Vec::with_capacity(ptext.len());
for i in (0..ptext.len()).step_by(2) {
let yx1 = match self.alphabet.bytes().position(|c| c == ptext[i]) {
Some(val) => val,
None => return Err(CipherInputError::NotInAlphabet),
};
let yx2 = match self.alphabet.bytes().position(|c| c == ptext[i + 1]) {
Some(val) => val,
None => return Err(CipherInputError::NotInAlphabet),
};
let (y1, x1) = (yx1 / 5, yx1 % 5);
let (y2, x2) = (yx2 / 5, yx2 % 5);
ctext.push(key1[y1 * 5 + x2]);
ctext.push(key2[y2 * 5 + x1]);
}
Ok(String::from_utf8(ctext).unwrap())
}
fn decipher(&self, ctext: &str) -> CipherResult {
input::in_alphabet(ctext, &self.alphabet)?;
if ctext.len() % 2 != 0 {
return Err(CipherInputError::BadInput(String::from(
"`ctext` must contain an even number of chars",
)));
}
let ctext = ctext.as_bytes();
let mut ptext = Vec::with_capacity(ctext.len());
for i in (0..ctext.len()).step_by(2) {
let yx1 = match self.key1.find(|c| c == ctext[i] as char) {
Some(val) => val,
None => return Err(CipherInputError::NotInAlphabet),
};
let yx2 = match self.key2.find(|c| c == ctext[i + 1] as char) {
Some(val) => val,
None => return Err(CipherInputError::NotInAlphabet),
};
let (y1, x2) = (yx1 / 5, yx1 % 5);
let (y2, x1) = (yx2 / 5, yx2 % 5);
ptext.push(self.alphabet.as_bytes()[y1 * 5 + x1]);
ptext.push(self.alphabet.as_bytes()[y2 * 5 + x2]);
}
Ok(String::from_utf8(ptext).unwrap())
}
}