-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c73298f
commit 42922ec
Showing
8 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! A module of cipher algorithms | ||
pub mod morse_code; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
|
||
extern crate alloc; | ||
|
||
pub mod ciphers; | ||
pub mod dynamic_programming; | ||
pub mod macros; | ||
pub mod math; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
/// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,7 @@ where | |
#[cfg(test)] | ||
mod tests { | ||
use test_case::test_case; | ||
|
||
use alloc::vec; | ||
use alloc::vec::Vec; | ||
|
||
|