Skip to content

Commit

Permalink
add morse code cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
martial-plains committed Mar 11, 2024
1 parent c73298f commit 42922ec
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2021"
num = "0.4.0"
rand = "0.8.5"
hashbrown = { version = "0.14.3" }
lazy_static = "1.4.0"


[dev-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions src/ciphers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! A module of cipher algorithms
pub mod morse_code;
151 changes: 151 additions & 0 deletions src/ciphers/morse_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//! This module provides functions to convert characters to Morse code and vice versa,
//! as well as functions to encrypt and decrypt text using Morse code.
use alloc::{string::String, vec::Vec};
use hashbrown::HashMap;

use crate::macros::hashmap;

lazy_static::lazy_static! {
static ref MORSE_TABLE: HashMap<char, &'static str> = hashmap! {
'a' => ".-",
'b' => "-...",
'c' => "-.-.",
'd' => "-..",
'e' => ".",
'f' => "..-.",
'g' => "--.",
'h' => "....",
'i' => "..",
'j' => ".---",
'k' => "-.-",
'l' => ".-..",
'm' => "--",
'n' => "-.",
'o' => "---",
'p' => ".--.",
'q' => "--.-",
'r' => ".-.",
's' => "...",
't' => "-",
'u' => "..-",
'v' => "...-",
'w' => ".--",
'x' => "-..-",
'y' => "-.--",
'z' => "--..",
'1' => ".----",
'2' => "..---",
'3' => "...--",
'4' => "....-",
'5' => ".....",
'6' => "-....",
'7' => "--...",
'8' => "---..",
'9' => "----.",
'0' => "-----",
'.' => ".-.-.-",
',' => "--..--",
'?' => "..--..",
'\'' => ".----.",
'!' => "-.-.--",
'/' => "-..-.",
'(' => "-.--.",
')' => "-.--.-",
'&' => ".-...",
':' => "---...",
';' => "-.-.-.",
'=' => "-...-",
'+' => ".-.-.",
'-' => "-....-",
'_' => "..--.-",
'"' => ".-..-.",
'$' => "...-..-",
'@' => ".--.-.",
'¿' => "..-.-",
'¡' => "--...-",
};
}

/// Encrypts a text by converting each character to Morse code.
///
/// # Arguments
///
/// * `text` - A string slice that represents the text to be encrypted.
///
/// # Returns
///
/// * A string that represents the encrypted text.
#[must_use]
pub fn encrypt(text: &str) -> String {
let text_lowered = text.to_lowercase();
let words = text_lowered.split(' ').collect::<Vec<_>>();
let mut morse = String::new();

for (index, word) in words.iter().enumerate() {
for (index, character) in word.char_indices() {
morse.push_str(MORSE_TABLE[&character]);

if index == word.len() {
morse.push_str("");
} else {
morse.push_str(" ");
}
}

if index == words.len() - 1 {
morse.push_str("");
} else {
morse.push_str(" / ");
}
}

morse
}

/// Decrypts a text by converting each Morse code to its character equivalent.
///
/// # Arguments
///
/// * `text` - A string slice that represents the Morse code to be decrypted.
///
/// # Returns
///
/// * A string that represents the decrypted text.
#[must_use]
pub fn decrypt(text: &str) -> String {
let mut decrypted_text = String::new();

let splits = text
.split(char::is_whitespace)
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();

for split in &splits {
for (key, value) in MORSE_TABLE.iter() {
if split == value {
decrypted_text.push(*key);
}
}
}

decrypted_text
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn test_morse() {
let text = "01234567890";
let encrypted = encrypt(text);
let decrypted = decrypt(&encrypted);
assert_eq!(text, decrypted);

let text = "abcdefghijklmnopqrstuvwxyz";
let encrypted = encrypt(text);
let decrypted = decrypt(&encrypted);
assert_eq!(text, decrypted);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

extern crate alloc;

pub mod ciphers;
pub mod dynamic_programming;
pub mod macros;
pub mod math;
Expand Down
2 changes: 1 addition & 1 deletion src/sorts/bubble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ where
#[cfg(test)]
mod tests {

use alloc::vec::Vec;
use alloc::vec;
use alloc::vec::Vec;

use test_case::test_case;

Expand Down
2 changes: 1 addition & 1 deletion src/sorts/bucket.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::vec::Vec;
use alloc::vec;
use alloc::vec::Vec;

/// A Rust bucket sort implementation
///
Expand Down
2 changes: 1 addition & 1 deletion src/sorts/counting.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::vec::Vec;
use alloc::vec;
use alloc::vec::Vec;

use core::ops::AddAssign;

Expand Down
2 changes: 1 addition & 1 deletion src/sorts/insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
#[cfg(test)]
mod tests {
use test_case::test_case;

use alloc::vec;
use alloc::vec::Vec;

Expand Down

0 comments on commit 42922ec

Please sign in to comment.