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
/// Scytale Cipher
///
/// the struct is generated through the new() function.
///
pub struct Scytale {
key: usize,
}
impl Scytale {
/// Initialize a scytale cipher with a key.
///
/// # Examples:
/// - Initialization with valid key:
/// ```
/// use cienli::ciphers::scytale::Scytale;
/// let scytale = Scytale::new(3);
///
/// assert!(scytale.is_ok());
/// ```
/// - Initialization with a zero key:
/// ```
/// use cienli::ciphers::scytale::Scytale;
/// let scytale = Scytale::new(0);
///
/// assert!(scytale.is_err());
/// ```
pub fn new(key: usize) -> Result<Scytale, &'static str> {
match key {
0 => Err("Key cannot be zero"),
_ => Ok(Scytale { key }),
}
}
/// Enciphers a message with the scytale cipher.
///
/// # Example:
/// ```
/// use cienli::ciphers::scytale::Scytale;
/// let scytale = Scytale::new(3).unwrap();
///
/// assert_eq!("Hl:eo)l ", scytale.encipher("Hello :)"))
/// ```
pub fn encipher(&self, message: &str) -> String {
if self.key >= message.chars().count() {
return message.to_string();
}
let table = Scytale::generate_table(self.key, message, false);
table
.iter()
.flatten()
.collect::<String>()
.trim_end_matches('\0')
.to_string()
}
/// Deciphers a message with the scytale cipher.
///
/// # Example:
/// ```
/// use cienli::ciphers::scytale::Scytale;
/// let scytale = Scytale::new(3).unwrap();
///
/// assert_eq!("Hello :)", scytale.decipher("Hl:eo)l "));
/// ```
pub fn decipher(&self, cipher: &str) -> String {
if self.key >= cipher.chars().count() || self.key == 1 {
return cipher.to_string();
}
let mut table = Scytale::generate_table(self.key, cipher, true);
let mut message = String::new();
while table
.iter()
.filter(|character| !character.is_empty())
.count()
> 0
{
for column in table.iter_mut() {
message.push(column.remove(0));
}
}
message.trim_end_matches('\0').to_string()
}
fn generate_table(height: usize, message: &str, decipher: bool) -> Vec<Vec<char>> {
let width = (message.chars().count() as f32 / height as f32).ceil() as usize;
let mut table = vec![vec!['\0'; width]; height];
for (position, element) in message.chars().enumerate() {
let (column, row) = match decipher {
true => (position / height, position % height),
false => (position % height, position / height),
};
table[column][row] = element;
}
table
}
}
#[cfg(test)]
mod tests {
use super::Scytale;
#[test]
fn invalid_key_test() {
assert!(Scytale::new(0).is_err());
}
#[test]
fn big_key_test() {
let scytale = Scytale::new(15).unwrap();
assert_eq!("Hello :)", scytale.encipher("Hello :)"))
}
#[test]
fn equal_key_test() {
let scytale = Scytale::new(8).unwrap();
assert_eq!("Hello :)", scytale.encipher("Hello :)"))
}
#[test]
fn encipher_test() {
let scytale = Scytale::new(3).unwrap();
assert_eq!("Hl:eo)l ", scytale.encipher("Hello :)"));
}
#[test]
fn decipher_test() {
let scytale = Scytale::new(3).unwrap();
assert_eq!("Hello :)", scytale.decipher("Hl:eo)l "));
}
}