Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: command line interface #21

Open
Dustin-Ray opened this issue Oct 18, 2023 · 4 comments
Open

feature: command line interface #21

Dustin-Ray opened this issue Oct 18, 2023 · 4 comments
Labels
feature Add new functionality to the library good first issue Good for newcomers Larger Project Larger project sizing affecting multiple areas of the library. Not difficult, but more to consider.

Comments

@Dustin-Ray
Copy link
Owner

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

@Dustin-Ray Dustin-Ray added good first issue Good for newcomers feature Add new functionality to the library Larger Project Larger project sizing affecting multiple areas of the library. Not difficult, but more to consider. labels Oct 20, 2023
@Dustin-Ray
Copy link
Owner Author

Dustin-Ray commented Oct 27, 2023

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.

@Dustin-Ray Dustin-Ray added good first issue Good for newcomers and removed good first issue Good for newcomers labels Oct 27, 2023
@omibo
Copy link
Collaborator

omibo commented Mar 8, 2024

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
Copy link
Owner Author

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
Copy link
Owner Author

Dustin-Ray commented Mar 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Add new functionality to the library good first issue Good for newcomers Larger Project Larger project sizing affecting multiple areas of the library. Not difficult, but more to consider.
Projects
None yet
Development

No branches or pull requests

2 participants