From 882a917971bfa4e6850553a7b86b7e8891c6f745 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 31 Aug 2021 11:23:15 +0200 Subject: [PATCH] Little code improvements --- src/cli.rs | 4 ++-- src/lib.rs | 6 +++--- src/main.rs | 5 +---- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 7143da3..c466589 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -96,11 +96,11 @@ pub fn parse() -> Option { let cipher = app .value_of("cipher") - .and_then(|s| Some(if s.to_lowercase() == "aes" { + .map(|s| if s.to_lowercase() == "aes" { CipherAlgorithm::AesCtr } else { CipherAlgorithm::XChaCha20 - }) + } ) .unwrap_or_else(|| if aes_ni::get() { CipherAlgorithm::AesCtr diff --git a/src/lib.rs b/src/lib.rs index 25a0c1e..c24aee6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ impl Password { impl From> for Password { fn from(s: Option<&str>) -> Self { - Self(s.map(|s| String::from(s))) + Self(s.map(String::from)) } } @@ -66,7 +66,7 @@ impl> Write for LazyWriter

{ } } -pub fn encrypt(reader: &mut R, writer: &mut W, params: &EncryptionParams, mut cipher: DobyCipher, block_size: usize, already_read: Option>) -> io::Result<()> { +pub fn encrypt(reader: &mut R, writer: &mut W, params: &EncryptionParams, mut cipher: DobyCipher, block_size: usize, already_read: Option<&[u8]>) -> io::Result<()> { writer.write_all(MAGIC_BYTES)?; params.write(writer)?; let mut buff = vec![0; block_size]; @@ -97,7 +97,7 @@ pub fn decrypt(reader: &mut R, writer: &mut W, mut cipher: Do if n == 0 { break; } else { - writer.write(&buff[..n])?; + writer.write_all(&buff[..n])?; } } Ok(cipher.verify_hmac()) diff --git a/src/main.rs b/src/main.rs index 17d33af..683d7c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,9 +16,6 @@ fn run() -> bool { let mut magic_bytes = vec![0; MAGIC_BYTES.len()]; match reader.read(&mut magic_bytes) { Ok(n) => { - if n < magic_bytes.len() { - magic_bytes.truncate(n); - } if magic_bytes == MAGIC_BYTES && !cli_args.force_encrypt { //we probably want to decrypt match EncryptionParams::read(&mut reader) { Ok(params) => { @@ -55,7 +52,7 @@ fn run() -> bool { ¶ms, cipher, cli_args.block_size, - Some(magic_bytes) + Some(&magic_bytes[..n]) ) { Ok(_) => success = true, Err(e) => eprintln!("I/O error while encrypting: {}", e)