generated from noahgift/rust-new-project-template
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:nogibjj/rust-data-engineering
- Loading branch information
Showing
30 changed files
with
941 additions
and
8 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,9 @@ | ||
[package] | ||
name = "caeser-cipher-cli" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
clap = { version = "4.3.17", features = ["derive"] } |
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,13 @@ | ||
format: | ||
cargo fmt --quiet | ||
|
||
lint: | ||
cargo clippy --quiet | ||
|
||
test: | ||
cargo test --quiet | ||
|
||
run: | ||
cargo run | ||
|
||
all: format lint test run |
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,24 @@ | ||
/* | ||
This code defines two functions: encrypt and decrypt. | ||
The encrypt function takes a plaintext string and a shift value, and returns the ciphertext string. The decrypt function takes a ciphertext string and a shift value, | ||
and returns the plaintext string. | ||
*/ | ||
|
||
pub fn encrypt(text: &str, shift: u8) -> String { | ||
let mut result = String::new(); | ||
for c in text.chars() { | ||
if c.is_ascii_alphabetic() { | ||
let base = if c.is_ascii_lowercase() { b'a' } else { b'A' }; | ||
let offset = (c as u8 - base + shift) % 26; | ||
result.push((base + offset) as char); | ||
} else { | ||
result.push(c); | ||
} | ||
} | ||
result | ||
} | ||
|
||
pub fn decrypt(text: &str, shift: u8) -> String { | ||
encrypt(text, 26 - shift) | ||
} |
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,49 @@ | ||
/* | ||
To run: | ||
cargo run -- --message "Off to the bunker. Every person for themselves" --encrypt --shift 10 | ||
To decrypt: | ||
cargo run -- --message "Ypp dy dro lexuob. Ofobi zobcyx pyb drowcovfoc" --decrypt --shift 10 | ||
*/ | ||
|
||
|
||
use caeser_cipher_cli::{decrypt, encrypt}; | ||
use clap::Parser; | ||
|
||
/// CLI tool to encrypt and decrypt messages using the caeser cipher | ||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Args { | ||
/// Encrypt the message | ||
#[arg(short, long)] | ||
encrypt: bool, | ||
|
||
/// decrypt the message | ||
#[arg(short, long)] | ||
decrypt: bool, | ||
|
||
/// The message to encrypt or decrypt | ||
#[arg(short, long)] | ||
message: String, | ||
|
||
/// The shift to use for the cipher | ||
/// Must be between 1 and 25, the default is 3 | ||
#[arg(short, long, default_value = "3")] | ||
shift: u8, | ||
} | ||
|
||
// run it | ||
fn main() { | ||
let args = Args::parse(); | ||
if args.encrypt { | ||
println!("{}", encrypt(&args.message, args.shift)); | ||
} else if args.decrypt { | ||
println!("{}", decrypt(&args.message, args.shift)); | ||
} else { | ||
println!("Please specify either --encrypt or --decrypt"); | ||
} | ||
} |
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,8 @@ | ||
[package] | ||
name = "caesar-cipher" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
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,13 @@ | ||
format: | ||
cargo fmt --quiet | ||
|
||
lint: | ||
cargo clippy --quiet | ||
|
||
test: | ||
cargo test --quiet | ||
|
||
run: | ||
cargo run | ||
|
||
all: format lint test run |
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,24 @@ | ||
/* | ||
This code defines two functions: encrypt and decrypt. | ||
The encrypt function takes a plaintext string and a shift value, and returns the ciphertext string. The decrypt function takes a ciphertext string and a shift value, | ||
and returns the plaintext string. | ||
*/ | ||
|
||
pub fn encrypt(text: &str, shift: u8) -> String { | ||
let mut result = String::new(); | ||
for c in text.chars() { | ||
if c.is_ascii_alphabetic() { | ||
let base = if c.is_ascii_lowercase() { b'a' } else { b'A' }; | ||
let offset = (c as u8 - base + shift) % 26; | ||
result.push((base + offset) as char); | ||
} else { | ||
result.push(c); | ||
} | ||
} | ||
result | ||
} | ||
|
||
pub fn decrypt(text: &str, shift: u8) -> String { | ||
encrypt(text, 26 - shift) | ||
} |
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,21 @@ | ||
/* | ||
This code defines two functions: encrypt and decrypt. | ||
The encrypt function takes a plaintext string and a shift value, | ||
and returns the ciphertext string. | ||
The decrypt function takes a ciphertext string and a shift value, | ||
and returns the plaintext string. | ||
*/ | ||
|
||
use caesar_cipher::decrypt; | ||
use caesar_cipher::encrypt; | ||
|
||
fn main() { | ||
let plaintext = "the quick brown fox jumps over the lazy dog"; | ||
let shift = 3; | ||
let ciphertext = encrypt(plaintext, shift); | ||
let decrypted_text = decrypt(&ciphertext, shift); | ||
println!("Plaintext: {}", plaintext); | ||
println!("Ciphertext: {}", ciphertext); | ||
println!("Decrypted text: {}", decrypted_text); | ||
} |
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
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,9 @@ | ||
[package] | ||
name = "decoder-ring" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
clap = { version = "4.3.17", features = ["derive"] } |
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,13 @@ | ||
format: | ||
cargo fmt --quiet | ||
|
||
lint: | ||
cargo clippy --quiet | ||
|
||
test: | ||
cargo test --quiet | ||
|
||
run: | ||
cargo run | ||
|
||
all: format lint test run |
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,115 @@ | ||
use std::collections::HashMap; | ||
|
||
fn gen_counts() -> HashMap<char, f32> { | ||
// Reference letter frequencies in English | ||
let mut eng_freq: HashMap<char, f32> = HashMap::new(); | ||
|
||
// Accounts for 80% of all letters in English | ||
eng_freq.insert('e', 12.7); | ||
eng_freq.insert('t', 9.1); | ||
eng_freq.insert('a', 8.2); | ||
eng_freq.insert('o', 7.5); | ||
eng_freq.insert('i', 7.0); | ||
eng_freq.insert('n', 6.7); | ||
eng_freq.insert('s', 6.3); | ||
eng_freq.insert('h', 6.1); | ||
eng_freq.insert('r', 6.0); | ||
eng_freq.insert('d', 4.3); | ||
|
||
eng_freq | ||
} | ||
|
||
fn stats_analysis(text: &str) -> Vec<(char, u32, f32, Option<f32>, f32)> { | ||
let mut counts: HashMap<char, u32> = HashMap::new(); | ||
|
||
for c in text.chars() { | ||
*counts.entry(c).or_insert(0) += 1; | ||
} | ||
|
||
let total: u32 = counts.values().sum(); | ||
|
||
let eng_freq_map = gen_counts(); | ||
let eng_freq_map: HashMap<char, f32> = eng_freq_map.iter().map(|(k, v)| (*k, *v)).collect(); | ||
|
||
let mut results = Vec::new(); | ||
|
||
for (letter, count) in &counts { | ||
let freq = (*count as f32 / total as f32) * 100.0; | ||
let eng_freq = eng_freq_map.get(&letter.to_ascii_lowercase()).cloned(); | ||
|
||
let eng_freq_diff = eng_freq.map_or(0.0, |f| (freq - f).abs()); | ||
|
||
results.push((*letter, *count, freq, eng_freq, eng_freq_diff)); | ||
} | ||
results | ||
} | ||
|
||
pub fn print_stats_analysis(text: &str) { | ||
let stats = stats_analysis(text); | ||
for (letter, count, freq, eng_freq, eng_freq_diff) in stats { | ||
println!( | ||
"{}: {} ({}%), English Freq: {} ({}%)", | ||
letter, | ||
count, | ||
freq, | ||
eng_freq.unwrap_or(0.0), | ||
eng_freq_diff | ||
); | ||
} | ||
} | ||
|
||
pub fn decrypt(text: &str, shift: u8) -> String { | ||
let mut result = String::new(); | ||
|
||
for c in text.chars() { | ||
if c.is_ascii_alphabetic() { | ||
let base = if c.is_ascii_lowercase() { b'a' } else { b'A' }; | ||
let offset = (c as u8 - base + shift) % 26; | ||
result.push((base + offset) as char); | ||
} else { | ||
result.push(c); | ||
} | ||
} | ||
|
||
result | ||
} | ||
|
||
/* | ||
Guess Shift: | ||
First, uses statistical analysis to determine the most likely shift. | ||
Then, uses the most likely shift to decrypt the message. | ||
Accepts: | ||
* text: the message to decrypt | ||
* depth: the number of shifts to try | ||
Returns: | ||
* depth: the number of shifts to tried | ||
* shift: the most likely shift | ||
* decrypted: the decrypted message | ||
*/ | ||
|
||
pub fn guess_shift(text: &str, depth: u8) -> (u8, u8, String, f32) { | ||
let mut max_score = 0.0; | ||
let mut best_shift = 0; | ||
let mut decrypted = String::new(); | ||
|
||
for shift in 0..depth { | ||
let decrypted_text = decrypt(text, shift); | ||
let stats = stats_analysis(&decrypted_text); | ||
|
||
let mut score = 0.0; | ||
for (_, _, freq, eng_freq, eng_freq_diff) in stats { | ||
if let Some(eng_freq) = eng_freq { | ||
score += (1.0 - eng_freq_diff / eng_freq) * freq; | ||
} | ||
} | ||
println!("Shift: {}, Score: {}", shift, score); | ||
if score > max_score { | ||
max_score = score; | ||
best_shift = shift; | ||
decrypted = decrypted_text; | ||
} | ||
} | ||
|
||
(depth, best_shift, decrypted, max_score) | ||
} |
Oops, something went wrong.