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
/// Atbash Cipher
///
/// The struct is generated through the new() function
///
pub struct Atbash<'a> {
message: &'a str,
}
impl Atbash<'_> {
/// Initialize a atbash cipher with a message or a cipher.
///
/// # Examples:
/// - Initialization with a message:
/// ```
/// use cienli::ciphers::atbash::Atbash;
/// let atbash = Atbash::new("Hello Friend :)");
/// ```
///
/// - Initialization with a cipher:
/// ```
/// use cienli::ciphers::atbash::Atbash;
/// let atbash = Atbash::new("Svool Uirvmw :)");
/// ```
///
pub fn new(message: &str) -> Atbash {
Atbash { message }
}
/// Enciphers a message with the atbash cipher.
///
/// # Example:
/// ```
/// use cienli::ciphers::atbash::Atbash;
/// let atbash = Atbash::new("Hello Friend :)");
///
/// assert_eq!("Svool Uirvmw :)", atbash.encipher());
/// ```
pub fn encipher(&self) -> String {
self.message
.chars()
.map(|character| match character {
'A'..='Z' => ((90 - character as u8) + 65) as char,
'a'..='z' => ((122 - character as u8) + 97) as char,
_ => character,
})
.collect()
}
/// Deciphers a message with the atbash cipher.
///
/// # Example:
/// ```
/// use cienli::ciphers::atbash::Atbash;
/// let atbash = Atbash::new("Svool Uirvmw :)");
///
/// assert_eq!("Hello Friend :)", atbash.decipher());
/// ```
pub fn decipher(&self) -> String {
self.encipher()
}
}
#[cfg(test)]
mod tests {
use super::Atbash;
#[test]
fn atbash_encipher() {
let atbash = Atbash::new("Hello Friend :)");
assert_eq!("Svool Uirvmw :)", atbash.encipher())
}
#[test]
fn atbash_decipher() {
let atbash = Atbash::new("Svool Uirvmw :)");
assert_eq!("Hello Friend :)", atbash.decipher());
}
}