Dustin-Ray / capyCRYPT

An experimental high-performance cryptosystem.
MIT License
12 stars 1 forks source link

feature: command line interface #21

Open Dustin-Ray opened 1 year ago

Dustin-Ray commented 1 year ago

creating an openssl style cli would increase the accessibility and usability of this library

Dustin-Ray commented 1 year ago

This one wont be too hard. It will simply involve a lot of console input scanning and println!. Our ultimate goal is produce something similar to the openssl CLI. Heres an example of hashing a file with openssl:

openssl dgst -sha256 /path/to/your/file.txt

We want our library for now to expose a CLI that would allow it to be used in a similar manner above:

capycrypt compute_sha3_hash "So long and thanks for all the fish" -256

Where 256 is the security strength passed after the message to digest.

omibo commented 8 months ago

For writing a CLI for the library, or other functionalities that use the public API of the library, one needs to define a SecParam enum in their own code and pass it as an argument to the function (for example, compute_hash_sha3).

However, since functions like compute_hash_sha3 are intended for public access, I believe it would be better to accept an integer as input (instead of SecParam). This integer can then be converted to SecParam inside the compute_hash_sha3 function.

If this is correct, I can proceed to open an issue on this.

Dustin-Ray commented 8 months ago

Yeah I think thats exactly correct. The CLI can have machinery behind the scenes that can parse the input from the arguments passed into it. So we really want this syntax for a good CLI:

capycrypt compute_sha3_hash "Test message" -256

The CLI can parse that 256 into the supported enum behind the scenes and then throw an error back if it was an unsupported value

Dustin-Ray commented 8 months ago

heres a more up-to-date example of how the initial CLI can be built:

use structopt::StructOpt;
use capycrypt::{Hashable, Message, SecParam, OperationError};

#[derive(Debug, StructOpt)]
#[structopt(name = "capycrypt-cli", about = "A simple hashing CLI")]
enum Command {
    #[structopt(name = "compute_sha3_hash")]
    Sha3 {
        #[structopt(help = "The input string to hash")]
        input: String,

        #[structopt(help = "Security parameter for the SHA3 hash", short, long, default_value = "256")]
        sec_param: String, // Changed to String to directly parse into SecParam later
    },
}

fn main() {
    let result = Command::from_args();
    match result {
        Command::Sha3 { input, sec_param } => {
            let mut data = Message::new(&mut input.into_bytes());
            let sec_param = match sec_param.as_str() {
                "224" => SecParam::D224,
                "256" => SecParam::D256,
                "384" => SecParam::D384,
                "512" => SecParam::D512,
                _ => {
                    eprintln!("Unsupported security parameter. Use 224, 256, 384, or 512.");
                    return;
                }
            };

            match data.compute_hash_sha3(&sec_param) {
                Ok(_) => match data.digest {
                    Some(digest) => println!("Hash: {}", hex::encode(digest.to_vec())),
                    None => eprintln!("Error: Hash computation failed"),
                },
                Err(OperationError) => eprintln!("An error occurred during hash computation."),
            }
        } // Next command here, i.e., compute tagged hash
    }
}

This how you could build it out while ensuring that the security parameter is instantiated correctly