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

Implement paris crate to cleanup command-line output #24

Merged
merged 15 commits into from
May 29, 2022
Merged
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ maintenance = { status = "actively-developed" }

[dependencies]
aes-gcm = "0.9.4"
clap = { version = "3.1.18", features = ["cargo"] }
anyhow = "1.0.57"
rand = "0.8.5"
termion = "1.5.6"
argon2 = "0.4.0"
blake3 = "1.3.1"
chacha20poly1305 = "0.9.0"
deoxys = "0.0.2"
zeroize = "1.3.0"
argon2 = "0.4.0"
blake3 = "1.3.1"
aead = { version = "0.4.3", features = ["stream"] }
rand = "0.8.5"

clap = { version = "3.1.18", features = ["cargo"] }
anyhow = "1.0.57"
paris = { version = "1.5.13", features = ["macros"] }
termion = "1.5.6"

zip = { version = "0.6.2", default-features = false, features = ["deflate"] }
zeroize = "1.3.0"
56 changes: 35 additions & 21 deletions src/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::global::structs::CryptoParams;
use crate::key::get_secret;
use crate::prompt::overwrite_check;
use anyhow::{Context, Ok, Result};
use paris::Logger;
use std::fs::File;

use std::io::Read;
Expand All @@ -25,6 +26,8 @@ pub fn memory_mode(
header_file: &HeaderFile,
params: &CryptoParams,
) -> Result<()> {
let mut logger = Logger::new();

if !overwrite_check(output, params.skip, params.bench)? {
exit(0);
}
Expand All @@ -51,14 +54,20 @@ pub fn memory_mode(
.read_to_end(&mut encrypted_data)
.with_context(|| format!("Unable to read encrypted data from file: {}", input))?;
let read_duration = read_start_time.elapsed();
println!("Read {} [took {:.2}s]", input, read_duration.as_secs_f32());
logger.success(format!(
"Read {} [took {:.2}s]",
input,
read_duration.as_secs_f32()
));

let raw_key = get_secret(&params.keyfile, false, params.password)?;

println!(
"Decrypting {} in memory mode with {} (this may take a while)",
input, header.header_type.algorithm,
);
logger.info(format!("Using {} for decryption", header.header_type.algorithm));

logger.loading(format!(
"Decrypting {} (this may take a while)",
input
));

let mut output_file = if params.bench == BenchMode::WriteToFilesystem {
OutputFile::Some(
Expand All @@ -79,10 +88,10 @@ pub fn memory_mode(
params.hash_mode,
)?;
let decrypt_duration = decrypt_start_time.elapsed();
println!(
logger.done().success(format!(
"Decryption successful! [took {:.2}s]",
decrypt_duration.as_secs_f32()
);
));

if params.erase != EraseMode::IgnoreFile(0) {
crate::erase::secure_erase(input, params.erase.get_passes())?;
Expand All @@ -100,6 +109,14 @@ pub fn stream_mode(
header_file: &HeaderFile,
params: &CryptoParams,
) -> Result<()> {
let mut logger = Logger::new();

if input == output {
return Err(anyhow::anyhow!(
"Input and output files cannot have the same name."
));
}

let mut input_file =
File::open(input).with_context(|| format!("Unable to open input file: {}", input))?;

Expand All @@ -124,12 +141,6 @@ pub fn stream_mode(
exit(0);
}

if input == output {
return Err(anyhow::anyhow!(
"Input and output files cannot have the same name in stream mode."
));
}

let raw_key = get_secret(&params.keyfile, false, params.password)?;

let mut output_file = if params.bench == BenchMode::WriteToFilesystem {
Expand All @@ -141,10 +152,13 @@ pub fn stream_mode(
OutputFile::None
};

println!(
"Decrypting {} in stream mode with {} (this may take a while)",
input, header.header_type.algorithm,
);
logger.info(format!("Using {} for decryption", header.header_type.algorithm));

logger.loading(format!(
"Decrypting {} (this may take a while)",
input
));

let decrypt_start_time = Instant::now();
let decryption_result = decrypt_bytes_stream_mode(
&mut input_file,
Expand All @@ -167,17 +181,17 @@ pub fn stream_mode(

match params.bench {
BenchMode::WriteToFilesystem => {
println!(
logger.done().success(format!(
"Decryption successful! File saved as {} [took {:.2}s]",
output,
decrypt_duration.as_secs_f32(),
);
));
}
BenchMode::BenchmarkInMemory => {
println!(
logger.done().success(format!(
"Decryption successful! [took {:.2}s]",
decrypt_duration.as_secs_f32(),
);
));
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/decrypt/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use anyhow::Result;
use blake3::Hasher;
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use deoxys::DeoxysII256;
use paris::success;
use std::fs::File;
use std::io::Read;
use std::result::Result::Ok;
Expand Down Expand Up @@ -88,7 +89,7 @@ pub fn decrypt_bytes_memory_mode(
hasher.update(data);
let hash = hasher.finalize().to_hex().to_string();
let hash_duration = hash_start_time.elapsed();
println!(
success!(
"Hash of the encrypted file is: {} [took {:.2}s]",
hash,
hash_duration.as_secs_f32()
Expand All @@ -99,7 +100,7 @@ pub fn decrypt_bytes_memory_mode(
let write_start_time = Instant::now();
output.write_all(&decrypted_bytes)?;
let write_duration = write_start_time.elapsed();
println!("Wrote to file [took {:.2}s]", write_duration.as_secs_f32());
success!("Wrote to file [took {:.2}s]", write_duration.as_secs_f32());
}

Ok(())
Expand Down Expand Up @@ -167,7 +168,7 @@ pub fn decrypt_bytes_stream_mode(

if hash == HashMode::CalculateHash {
let hash = hasher.finalize().to_hex().to_string();
println!("Hash of the encrypted file is: {}. If this doesn't match with the original, something very bad has happened.", hash);
success!("Hash of the encrypted file is: {}. If this doesn't match with the original, something very bad has happened.", hash);
}

Ok(())
Expand Down
59 changes: 38 additions & 21 deletions src/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::key::get_secret;
use crate::prompt::overwrite_check;
use anyhow::Context;
use anyhow::{Ok, Result};
use paris::Logger;
use std::fs::File;
use std::process::exit;
use std::time::Instant;
Expand All @@ -25,6 +26,8 @@ pub fn memory_mode(
params: &CryptoParams,
algorithm: Algorithm,
) -> Result<()> {
let mut logger = Logger::new();

if !overwrite_check(output, params.skip, params.bench)? {
exit(0);
}
Expand All @@ -34,12 +37,19 @@ pub fn memory_mode(
let read_start_time = Instant::now();
let file_contents = get_bytes(input)?;
let read_duration = read_start_time.elapsed();
println!("Read {} [took {:.2}s]", input, read_duration.as_secs_f32());

println!(
"Encrypting {} in memory mode with {} (this may take a while)",
input, algorithm
);
logger.success(format!(
"Read {} [took {:.2}s]",
input,
read_duration.as_secs_f32()
));

logger.info(format!("Using {} for encryption", algorithm));

logger.loading(format!(
"Encrypting {} (this may take a while)",
input
));

let mut output_file = if params.bench == BenchMode::WriteToFilesystem {
OutputFile::Some(
Expand All @@ -60,10 +70,11 @@ pub fn memory_mode(
algorithm,
)?;
let encrypt_duration = encrypt_start_time.elapsed();
println!(

logger.done().success(format!(
"Encryption successful! [took {:.2}s]",
encrypt_duration.as_secs_f32()
);
));

if params.erase != EraseMode::IgnoreFile(0) {
crate::erase::secure_erase(input, params.erase.get_passes())?;
Expand All @@ -80,6 +91,14 @@ pub fn stream_mode(
params: &CryptoParams,
algorithm: Algorithm,
) -> Result<()> {
let mut logger = Logger::new();

if input == output {
return Err(anyhow::anyhow!(
"Input and output files cannot have the same name."
));
}

let mut input_file =
File::open(input).with_context(|| format!("Unable to open input file: {}", input))?;
let file_size = input_file
Expand All @@ -92,6 +111,7 @@ pub fn stream_mode(
.try_into()
.context("Unable to parse stream block size as u64")?
{
drop(logger);
drop(input_file);
return memory_mode(input, output, params, algorithm);
}
Expand All @@ -100,12 +120,6 @@ pub fn stream_mode(
exit(0);
}

if input == output {
return Err(anyhow::anyhow!(
"Input and output files cannot have the same name in stream mode."
));
}

let raw_key = get_secret(&params.keyfile, true, params.password)?;

let mut output_file = if params.bench == BenchMode::WriteToFilesystem {
Expand All @@ -117,10 +131,13 @@ pub fn stream_mode(
OutputFile::None
};

println!(
"Encrypting {} in stream mode with {} (this may take a while)",
input, algorithm
);
logger.info(format!("Using {} for encryption", algorithm));

logger.loading(format!(
"Encrypting {} (this may take a while)",
input
));

let encrypt_start_time = Instant::now();

let encryption_result = encrypt_bytes_stream_mode(
Expand All @@ -143,17 +160,17 @@ pub fn stream_mode(
let encrypt_duration = encrypt_start_time.elapsed();
match params.bench {
BenchMode::WriteToFilesystem => {
println!(
logger.done().success(format!(
"Encryption successful! File saved as {} [took {:.2}s]",
output,
encrypt_duration.as_secs_f32(),
);
));
}
BenchMode::BenchmarkInMemory => {
println!(
logger.done().success(format!(
"Encryption successful! [took {:.2}s]",
encrypt_duration.as_secs_f32(),
);
));
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/encrypt/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use anyhow::Context;
use anyhow::Result;
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use deoxys::DeoxysII256;
use paris::success;
use rand::{prelude::StdRng, Rng, SeedableRng};
use std::fs::File;
use std::io::Read;
Expand Down Expand Up @@ -118,7 +119,7 @@ pub fn encrypt_bytes_memory_mode(
crate::header::write_to_file(output, &header)?;
output.write_all(&encrypted_bytes)?;
let write_duration = write_start_time.elapsed();
println!("Wrote to file [took {:.2}s]", write_duration.as_secs_f32());
success!("Wrote to file [took {:.2}s]", write_duration.as_secs_f32());
}

let mut hasher = blake3::Hasher::new();
Expand All @@ -128,7 +129,7 @@ pub fn encrypt_bytes_memory_mode(
hasher.update(&encrypted_bytes);
let hash = hasher.finalize().to_hex().to_string();
let hash_duration = hash_start_time.elapsed();
println!(
success!(
"Hash of the encrypted file is: {} [took {:.2}s]",
hash,
hash_duration.as_secs_f32()
Expand Down Expand Up @@ -218,7 +219,7 @@ pub fn encrypt_bytes_stream_mode(
}
if hash == HashMode::CalculateHash {
let hash = hasher.finalize().to_hex().to_string();
println!("Hash of the encrypted file is: {}", hash,);
success!("Hash of the encrypted file is: {}", hash,);
}
Ok(())
}