Compare commits

...

2 Commits

Author SHA1 Message Date
Matéo Duparc 2fc56b6e6e
Order options in help message 2021-06-30 15:24:56 +02:00
Matéo Duparc 7cf87d5fa4
Headers viewer 2021-06-30 15:24:15 +02:00
3 changed files with 49 additions and 11 deletions

24
src/bin/headers.rs Normal file
View File

@ -0,0 +1,24 @@
use std::{env, fs::File, io::{self, Read}};
use doby::{MAGIC_BYTES, crypto::EncryptionParams};
fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
let mut file = File::open(&args[1])?;
let mut magic_bytes = vec![0; MAGIC_BYTES.len()];
file.read(&mut magic_bytes)?;
if magic_bytes == MAGIC_BYTES {
match EncryptionParams::read(&mut file)? {
Some(params) => {
println!("Argon2 time cost: {}", params.argon2.t_cost);
println!("Argon2 memory cost: {}KB", params.argon2.m_cost);
println!("Argon2 parallelism: {}", params.argon2.parallelism);
println!("Encryption cihpher: {}", params.cipher);
}
None => eprintln!("Invalid cipher")
}
} else {
eprintln!("Doby format not recognized.");
}
Ok(())
}

View File

@ -23,6 +23,7 @@ pub fn parse() -> Option<CliArgs> {
let app = App::new(crate_name!())
.version(crate_version!())
.setting(AppSettings::ColoredHelp)
.about("Secure symmetric encryption from the command line.")
.arg(Arg::with_name("INPUT").help("<PATH> | \"-\" or empty for stdin"))
.arg(Arg::with_name("OUTPUT").help("<PATH> | \"-\" or empty for stdout"))
.arg(
@ -32,14 +33,14 @@ pub fn parse() -> Option<CliArgs> {
.help(&format!("Encrypt even if {} format is recognized", crate_name!()))
)
.arg(
Arg::with_name("password")
Arg::with_name("1_password")
.short("p")
.long("password")
.value_name("password")
.help("Password used to derive encryption keys")
)
.arg(
Arg::with_name("t_cost")
Arg::with_name("2_t_cost")
.short("i")
.long("iterations")
.value_name("iterations")
@ -47,7 +48,7 @@ pub fn parse() -> Option<CliArgs> {
.default_value("10")
)
.arg(
Arg::with_name("m_cost")
Arg::with_name("3_m_cost")
.short("m")
.long("memory-cost")
.value_name("memory cost")
@ -55,7 +56,7 @@ pub fn parse() -> Option<CliArgs> {
.default_value("4096")
)
.arg(
Arg::with_name("parallelism")
Arg::with_name("4_parallelism")
.short("t")
.long("threads")
.value_name("threads")
@ -82,9 +83,9 @@ pub fn parse() -> Option<CliArgs> {
.get_matches();
let params = {
let t_cost = number(app.value_of("t_cost").unwrap())?;
let m_cost = number(app.value_of("m_cost").unwrap())?;
let parallelism = number(app.value_of("parallelism").unwrap())?;
let t_cost = number(app.value_of("2_t_cost").unwrap())?;
let m_cost = number(app.value_of("3_m_cost").unwrap())?;
let parallelism = number(app.value_of("4_parallelism").unwrap())?;
ArgonParams {
t_cost,
@ -129,7 +130,7 @@ pub fn parse() -> Option<CliArgs> {
})
.unwrap_or_else(|| Some(Box::new(stdout())))?;
let password = match app.value_of("password") {
let password = match app.value_of("1_password") {
Some(s) => s.to_string(),
None => rpassword::read_password_from_tty(Some("Password: ")).unwrap(),
};

View File

@ -1,4 +1,8 @@
use std::{convert::TryFrom, io::{self, Read, Write}};
use std::{
convert::TryFrom,
fmt::{self, Display, Formatter},
io::{self, Read, Write}
};
use num_enum::TryFromPrimitive;
use chacha20::XChaCha20;
use aes::{Aes256Ctr, cipher::{NewCipher, StreamCipher}};
@ -37,13 +41,22 @@ impl CipherAlgorithm {
}
}
impl Display for CipherAlgorithm {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
CipherAlgorithm::AesCtr => "AES-CTR",
CipherAlgorithm::XChaCha20 => "XChaCha20",
})
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct EncryptionParams {
password_salt: [u8; SALT_LEN],
argon2: ArgonParams,
pub argon2: ArgonParams,
hkdf_salt: [u8; SALT_LEN],
nonce: Vec<u8>,
cipher: CipherAlgorithm,
pub cipher: CipherAlgorithm,
}
impl EncryptionParams {