← all posts
Posted on Sep 15, 2022

Cipher: Nomenclator

A Simple Code Example

Let’s try to write a simple Nomenclator program.

The following program makes a lot of assumptions on the input provided to the ciphering function. The program is only meant to illustrate the fundamental idea of the cipher.

 1use std::collections::HashMap;
 2
 3struct Nomenclator {
 4    nc: HashMap<&str, &str>,
 5}
 6
 7
 8impl Nomenclator {
 9    pub fn encipher(&self, message: &str) -> String {
10        let mut res = String::new();
11
12        for letter in message.chars() {
13            res.push_str(self.nc.get(&letter).to_string());
14        }
15
16        res
17    }
18
19    fn new() -> Self {
20        Nomenclator {
21            nc : HashMap::from([
22            ("A", "Alpha"),
23            ("B", "Bravo"),
24            ("C", "Charlie"),
25            ("D", "Delta"),
26            ("E", "Echo"),
27            ("F", "Foxtrot"),
28            ("G", "Golf"),
29            ("H", "Hotel"),
30            ("I", "India"),
31            ("J", "Juliet"),
32            ("K", "Kilo"),
33            ("L", "Lima"),
34            ("M", "Mike"),
35            ("N", "November"),
36            ("O", "Oscar"),
37            ("P", "Papa"),
38            ("Q", "Quebec"),
39            ("R", "Romeo"),
40            ("S", "Sierra"),
41            ("T", "Tango"),
42            ("U", "Uniform"),
43            ("V", "Victor"),
44            ("W", "Whiskey"),
45            ("X", "X-Ray"),
46            ("Y", "Yankee"),
47            ("Z", "Zulu"),
48            ])
49        }
50    }
51}
52
53fn main() {
54    let nomenclator = Nomenclator::new();
55    println!("{:?}", nomenclator.nc.get("Z").unwrap());
56    println!("{:?}", nomenclator.nc.get("A").unwrap());
57
58    let test = nomenclator.encipher("ABC");
59    println!("{}", test);
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn encipher() {
68        let nomenclator = Nomenclator::new();
69        assert_eq!(*nomenclator.nc.get("A").unwrap(), "Alpha");
70        assert_eq!(*nomenclator.nc.get("Z").unwrap(), "Zulu");
71    }
72}