rework how 'keygen' works slightly

transfer
Avril 4 years ago
parent dca3e1cc19
commit 38dcccd001
Signed by: flanchan
GPG Key ID: 284488987C31F630

2
Cargo.lock generated

@ -34,7 +34,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "chacha20" name = "chacha20"
version = "1.1.1" version = "2.0.0"
dependencies = [ dependencies = [
"base64", "base64",
"getrandom", "getrandom",

@ -1,7 +1,7 @@
[package] [package]
name = "chacha20" name = "chacha20"
description = "chacha20_poly1305 encryption tool" description = "chacha20_poly1305 encryption tool"
version = "1.1.2" version = "2.0.0"
authors = ["Avril <flanchan@cumallover.me>"] authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018" edition = "2018"
license = "gpl-3.0-or-later" license = "gpl-3.0-or-later"

@ -30,10 +30,7 @@ fn keys() -> Result<(Mode, Key, IV), base64::DecodeError>
{ {
Some('e') => Mode::Encrypt, Some('e') => Mode::Encrypt,
Some('d') => Mode::Decrypt, Some('d') => Mode::Decrypt,
Some('k') => { Some('k') => Mode::Keygen,
let (key, iv) = cha::keygen();
return Ok((Mode::Keygen, key, iv));
},
other => { other => {
eprintln!("{} (v{}) - chacha20_poly1305 command line encryption tool", eprintln!("{} (v{}) - chacha20_poly1305 command line encryption tool",
env!("CARGO_PKG_NAME"), env!("CARGO_PKG_NAME"),
@ -43,14 +40,14 @@ fn keys() -> Result<(Mode, Key, IV), base64::DecodeError>
eprintln!(); eprintln!();
eprintln!("Usage: {} encrypt [<base64 key>] [<base64 iv>]", prog_name); eprintln!("Usage: {} encrypt [<base64 key>] [<base64 iv>]", prog_name);
eprintln!("Usage: {} decrypt [<base64 key>] [<base64 iv>]", prog_name); eprintln!("Usage: {} decrypt [<base64 key>] [<base64 iv>]", prog_name);
eprintln!("Usage: {} keygen", prog_name); eprintln!("Usage: {} keygen [<base64 key>] [<base64 iv>]", prog_name);
eprintln!("Usage: {} help", prog_name); eprintln!("Usage: {} help", prog_name);
eprintln!(); eprintln!();
eprintln!("(Key size is {}, IV size is {})", cha::KEY_SIZE, cha::IV_SIZE); eprintln!("(Key size is {}, IV size is {})", cha::KEY_SIZE, cha::IV_SIZE);
eprintln!("(requires OpenSSL 1.1.0 or newer)"); eprintln!("(requires OpenSSL 1.1.0 or newer)");
eprintln!("\nencrypt/decrypt:\n\tIf a key and/or IV are not provided, they are generated randomly and printed to stderr in order on one line each."); eprintln!("\nencrypt/decrypt:\n\tIf a key and/or IV are not provided, they are generated randomly and printed to stderr in order on one line each.");
eprintln!("\tIf the key and/or IV provided's size is lower than the cipher's key/IV size, the rest of the key/IV is padded with 0s. If the size is higher, the extra bytes are ignored."); eprintln!("\tIf the key and/or IV provided's size is lower than the cipher's key/IV size, the rest of the key/IV is padded with 0s. If the size is higher, the extra bytes are ignored.");
eprintln!("\nkeygen:\n\tThe key/iv is printed in the same way as auto-generated keys for the en/decryption modes, but to stdout instead of stderr."); eprintln!("\nkeygen:\n\tThe key/iv is printed in the same way as auto-generated keys for the en/decryption modes, but to stdout instead of stderr. If a key is given as parameter, the key is not printed. If the iv is given as a parameter also, nothing is printed.");
eprintln!("\nhelp:\n\tPrint this message to stderr then exit with code 0"); eprintln!("\nhelp:\n\tPrint this message to stderr then exit with code 0");
std::process::exit(if other == Some('h') {0} else {1}) std::process::exit(if other == Some('h') {0} else {1})
} }
@ -60,7 +57,11 @@ fn keys() -> Result<(Mode, Key, IV), base64::DecodeError>
Some(key) => key.parse()?, Some(key) => key.parse()?,
None => { None => {
let key = Key::new(); let key = Key::new();
eprintln!("{}", base64::encode(&key)); if mode == Mode::Keygen {
println!("{}", base64::encode(&key));
} else {
eprintln!("{}", base64::encode(&key));
}
key key
}, },
}; };
@ -68,7 +69,11 @@ fn keys() -> Result<(Mode, Key, IV), base64::DecodeError>
Some(iv) => iv.parse()?, Some(iv) => iv.parse()?,
None => { None => {
let iv = IV::new(); let iv = IV::new();
eprintln!("{}", base64::encode(&iv)); if mode == Mode::Keygen {
println!("{}", base64::encode(&iv));
} else {
eprintln!("{}", base64::encode(&iv));
}
iv iv
}, },
}; };
@ -98,8 +103,8 @@ fn main() {
output.flush().expect("Failed to flush stdout"); output.flush().expect("Failed to flush stdout");
}, },
Mode::Keygen => { Mode::Keygen => {
println!("{}", base64::encode(&key)); //println!("{}", base64::encode(&key));
println!("{}", base64::encode(&iv)); //println!("{}", base64::encode(&iv));
}, },
} }
} }

Loading…
Cancel
Save