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
pub enum RotType {
Rot5,
Rot13,
Rot18,
Rot47,
}
/// Rot Cipher
///
/// The struct is generated through the new() function.
///
pub struct Rot<'a> {
message: &'a str,
rot_type: RotType,
}
impl Rot<'_> {
/// Initialize a rot cipher with a message and rot type.
///
/// # Examples:
/// - Initialization with Rot13 type.:
/// ```
/// use cienli::ciphers::rot::{Rot, RotType};
/// let rot = Rot::new("• Hello Friend 83110 :) •", RotType::Rot13);
/// ```
pub fn new(message: &str, rot_type: RotType) -> Rot {
Rot { message, rot_type }
}
/// Enciphers a message with the rot cipher.
///
/// # Examples:
///
/// - Encipher with Rot47:
/// ```
/// use cienli::ciphers::rot::{Rot, RotType};
/// let rot47 = Rot::new("• Hello Friend 83110 :) •", RotType::Rot47);
///
/// assert_eq!("• w6==@ uC:6?5 gb``_ iX •", rot47.encipher());
/// ```
///
/// - Encipher with Rot13:
/// ```
/// use cienli::ciphers::rot::{Rot, RotType};
/// let rot13 = Rot::new("• Hello Friend 83110 :) •", RotType::Rot13);
///
/// assert_eq!("• Uryyb Sevraq 83110 :) •", rot13.encipher());
/// ```
pub fn encipher(&self) -> String {
match self.rot_type {
RotType::Rot5 => Rot::rot5(self.message),
RotType::Rot13 => Rot::rot13(self.message),
RotType::Rot18 => Rot::rot13(&(Rot::rot5(self.message))),
RotType::Rot47 => Rot::rot47(self.message),
}
}
/// Deciphers a cipher with the rot cipher.
///
/// # Examples:
///
/// - Decipher with Rot47:
/// ```
/// use cienli::ciphers::rot::{Rot, RotType};
/// let rot47 = Rot::new("• w6==@ uC:6?5 gb``_ iX •", RotType::Rot47);
///
/// assert_eq!("• Hello Friend 83110 :) •", rot47.decipher());
/// ```
///
/// - Decipher with Rot13:
/// ```
/// use cienli::ciphers::rot::{Rot, RotType};
/// let rot13 = Rot::new("• Uryyb Sevraq 83110 :) •", RotType::Rot13);
///
/// assert_eq!("• Hello Friend 83110 :) •", rot13.decipher());
/// ```
pub fn decipher(&self) -> String {
self.encipher()
}
fn rot5(message: &str) -> String {
message
.chars()
.map(|digit| match digit {
'0'..='4' => ((digit as u8) + 5) as char,
'5'..='9' => ((digit as u8) - 5) as char,
_ => digit,
})
.collect()
}
fn rot13(message: &str) -> String {
message
.chars()
.map(|character| match character {
'A'..='M' | 'a'..='m' => ((character as u8) + 13) as char,
'N'..='Z' | 'n'..='z' => ((character as u8) - 13) as char,
_ => character,
})
.collect()
}
fn rot47(message: &str) -> String {
message
.chars()
.map(|character| match character {
'!'..='O' => ((character as u8) + 47) as char,
'P'..='~' => ((character as u8) - 47) as char,
_ => character,
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::{Rot, RotType};
#[test]
fn rot47_encipher() {
let rot47 = Rot::new("• Hello Friend 83110 :) •", RotType::Rot47);
assert_eq!("• w6==@ uC:6?5 gb``_ iX •", rot47.encipher());
}
#[test]
fn rot47_decipher() {
let rot47 = Rot::new("• w6==@ uC:6?5 gb``_ iX •", RotType::Rot47);
assert_eq!("• Hello Friend 83110 :) •", rot47.decipher());
}
#[test]
fn rot18_encipher() {
let rot18 = Rot::new("• Hello Friend 83110 :) •", RotType::Rot18);
assert_eq!("• Uryyb Sevraq 38665 :) •", rot18.encipher());
}
#[test]
fn rot18_decipher() {
let rot18 = Rot::new("• Uryyb Sevraq 38665 :) •", RotType::Rot18);
assert_eq!("• Hello Friend 83110 :) •", rot18.decipher());
}
#[test]
fn rot13_encipher() {
let rot13 = Rot::new("• Hello Friend 83110 :) •", RotType::Rot13);
assert_eq!("• Uryyb Sevraq 83110 :) •", rot13.encipher());
}
#[test]
fn rot13_decipher() {
let rot13 = Rot::new("• Uryyb Sevraq 83110 :) •", RotType::Rot13);
assert_eq!("• Hello Friend 83110 :) •", rot13.decipher());
}
#[test]
fn rot5_encipher() {
let rot5 = Rot::new("• Hello Friend 83110 :) •", RotType::Rot5);
assert_eq!("• Hello Friend 38665 :) •", rot5.encipher());
}
#[test]
fn rot5_decipher() {
let rot5 = Rot::new("• Hello Friend 38665 :) •", RotType::Rot5);
assert_eq!("• Hello Friend 83110 :) •", rot5.decipher());
}
}