1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
pub use clap::Parser;

/// A simple to use command line cryptography tool for ree
#[derive(Parser, Debug)]
pub struct Cpal {
    #[clap(subcommand)]
    pub operation: Option<Operation>,
    /// Print version information
    #[clap(short, long)]
    pub version: bool,
}

#[derive(Debug, Clone, clap::Subcommand)]
pub enum Operation {
    /// Uses caesar decoding on input with the given offset
    Caesar {
        /// Example: $cpal caesar -s abc 1 => bcd
        #[clap(short, long)]
        string: Option<String>,
        /// Example: $cpal caesar -b 97,98,99 1 => bcd
        #[clap(short, long)]
        bytes: Option<String>,
        offset: i32,
        /// Set the hex flag if the bytes are hex encoded
        #[arg(long)]
        hex: bool,
        /// use input as file
        #[clap(short, long)]
        file: bool,
    },
    /// Uses Xor encoding on input with key
    /// the input can either be a string or a list of bytes
    Xor {
        /// Example: $cpal xor -s abc -k xxx
        #[clap(short, long)]
        string: Option<String>,
        /// Example: $cpal xor -b 97, 98, 99 -k xxx
        #[clap(short, long)]
        bytes: Option<String>,
        /// Set the hex flag if the bytes are hex encoded
        #[arg(long)]
        hex: bool,
        #[clap(short, long)]
        key: String,
    },
}

pub fn parse_hex(s: &str) -> u8 {
    u8::from_str_radix(s.split("0x").last().unwrap(), 16).unwrap()
}