make cli sub-crate

This commit is contained in:
Neemek 2025-10-09 19:54:05 +02:00
parent acc0b5e616
commit 9d0de8eb96
3 changed files with 0 additions and 271 deletions

42
cli/src/main.rs Normal file
View file

@ -0,0 +1,42 @@
use clap::Parser;
use clap_num::maybe_hex;
use std::fs::File;
use std::io::{Read, Write, stdin, stdout};
use chacha::do_chacha;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Where the key is stored
#[arg(short, long)]
key_file: String,
/// The amount of chacha rounds to do
#[arg(short, long, default_value = "20")]
rounds: usize,
/// The nonce in the initial key value
#[arg(short, long, default_value = "0", value_parser=maybe_hex::<u64>)]
nonce: u128,
}
fn main() {
let args = Args::parse();
let stdin = stdin();
let mut f = File::open(args.key_file).expect("failed to open key file");
let key_bytes = &mut [0u8; 32];
f.read_exact(key_bytes).expect("couldn't read key file");
let mut input = Vec::new();
let mut inp = stdin.lock();
inp.read_to_end(&mut input)
.expect("failed to read from stdin");
let bytes = do_chacha(input.as_slice(), key_bytes, [(args.nonce >> 64& 0xFFFFFFFF) as u32, (args.nonce >> 32 & 0xFFFFFFFF) as u32, args.nonce as u32], 0, args.rounds);
let stdout = stdout();
let mut out = stdout.lock();
out.write_all(bytes.as_slice()).unwrap();
}