Skip to content

Commit

Permalink
Utilize clap for mutually exclusive flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Barquadd committed Jan 1, 2023
1 parent 8aa4071 commit 2e87345
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ strip = true

[dependencies]
aes-gcm = "0.10.1"
clap = {version="4.0.32", features=["derive"]}
clap = {version="4.0.32", features=["cargo"]}
rand = "0.8.5"
sha2 = "0.10.6"
62 changes: 30 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@ use aes_gcm::{
aead::{generic_array::GenericArray, AeadInPlace, KeyInit},
Aes256Gcm, Nonce,
};
use clap::Parser;
use clap::{arg, command, value_parser};
use sha2::{Digest, Sha256};
use std::io::{stdin, stdout};
use std::{fs::write, io::Write};

#[derive(Parser)]
struct Cli {
#[arg(short, long)]
encrypt: bool,
#[arg(short, long)]
decrypt: bool,
#[arg(short, long)]
keyfile: Option<std::path::PathBuf>,
path: std::path::PathBuf,
}
use std::{fs::write, io::Write, path::PathBuf};

fn hash_vec_n_times(v: &Vec<u8>, n: u32) -> Vec<u8> {
let mut hasher = Sha256::new();
Expand All @@ -44,20 +33,29 @@ fn get_user_pass() -> Vec<u8> {
}

fn main() {
let args = Cli::parse();
if { args.encrypt } == { args.decrypt } {
panic!("You must specify either --encrypt or --decrypt");
}
println!(
"--> {} <--",
if args.encrypt {
"ENCRYPTING"
} else {
"DECRYPTING"
}
);
let m = command!()
.arg(arg!(encrypt: -e --encrypt)
.conflicts_with("decrypt")
.required(true))
.arg(arg!(decrypt: -d --decrypt)
.conflicts_with("encrypt")
.required(true))
.arg(arg!(keyfile: -k --keyfile <keyfile>)
.value_parser(value_parser!(PathBuf))
.required(false))
.arg(arg!(input: <path>)
.value_parser(value_parser!(PathBuf))
.required(true))
.get_matches();

let encrypt: &bool = m.get_one::<bool>("encrypt").unwrap();
let decrypt: &bool = m.get_one::<bool>("decrypt").unwrap();
let keyfile: Option<&PathBuf> = m.get_one::<PathBuf>("keyfile");
let path: PathBuf = m.get_one::<PathBuf>("input").unwrap().to_path_buf();

println!("--> {} <--", if *encrypt { "ENCRYPTING" } else { "DECRYPTING" });

let key = match args.keyfile {
let key = match keyfile {
Some(keyfile) => {
println!("Reading keyfile...");
let key = std::fs::read(keyfile).unwrap();
Expand All @@ -72,7 +70,7 @@ fn main() {
let key_g: GenericArray<_, U32> = GenericArray::clone_from_slice(&key);
let cipher = Aes256Gcm::new(&key_g);

if args.encrypt {
if *encrypt {
// there's certainly a better way to do this
let mut nonce_vec: Vec<u8> = vec![];
for _ in 0..12 {
Expand All @@ -81,17 +79,17 @@ fn main() {
let nonce = Nonce::from_slice(&nonce_vec);

println!("Reading file...");
let mut buffer: Vec<u8> = std::fs::read(args.path.clone()).expect("Failed to read file.");
let mut buffer: Vec<u8> = std::fs::read(path.clone()).expect("Failed to read file.");
println!("Encrypting...");
cipher
.encrypt_in_place(nonce, b"", &mut buffer)
.expect("Encryption failed.");
nonce_vec.append(&mut buffer); // we want the nonce to be the first 96 bits in the file
println!("Writing file...");
write(args.path, nonce_vec).expect("Failed to write file.");
} else if args.decrypt {
write(path, nonce_vec).expect("Failed to write file.");
} else if *decrypt {
println!("Reading file...");
let buffer: Vec<u8> = std::fs::read(args.path.clone()).expect("Failed to read file.");
let buffer: Vec<u8> = std::fs::read(path.clone()).expect("Failed to read file.");
// the first 12 bytes of the buffer is (should be) the nonce
let (nonce_bytes, buffer) = buffer.split_at(12);
let nonce = Nonce::from_slice(&nonce_bytes);
Expand All @@ -101,6 +99,6 @@ fn main() {
.decrypt_in_place(nonce, b"", &mut buffer)
.expect("Decryption failed.");
println!("Writing file...");
write(args.path, buffer).expect("Failed to write file.");
write(path, buffer).expect("Failed to write file.");
}
}

0 comments on commit 2e87345

Please sign in to comment.