happy_cracking/crypto/
atbash.rs1use anyhow::Result;
2use clap::Subcommand;
3
4#[derive(Subcommand)]
5pub enum AtbashAction {
6 #[command(about = "Apply Atbash cipher (A↔Z, B↔Y, ...)")]
7 Transform {
8 #[arg(help = "Input text")]
9 input: String,
10 },
11}
12
13pub fn run(action: AtbashAction) -> Result<()> {
14 match action {
15 AtbashAction::Transform { input } => {
16 println!("{}", transform(&input));
17 }
18 }
19 Ok(())
20}
21
22pub fn transform(input: &str) -> String {
23 let mut bytes = input.as_bytes().to_vec();
24 for b in &mut bytes {
25 if b.is_ascii_uppercase() {
26 *b = b'Z' - (*b - b'A');
27 } else if b.is_ascii_lowercase() {
28 *b = b'z' - (*b - b'a');
29 }
30 }
31 unsafe { String::from_utf8_unchecked(bytes) }
34}