Little code improvements

This commit is contained in:
Matéo Duparc 2021-08-31 11:23:15 +02:00
parent 9ac3bd7a53
commit 882a917971
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
3 changed files with 6 additions and 9 deletions

View File

@ -96,11 +96,11 @@ pub fn parse() -> Option<CliArgs> {
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

View File

@ -17,7 +17,7 @@ impl Password {
impl From<Option<&str>> 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<P: AsRef<Path>> Write for LazyWriter<P> {
}
}
pub fn encrypt<R: Read, W: Write>(reader: &mut R, writer: &mut W, params: &EncryptionParams, mut cipher: DobyCipher, block_size: usize, already_read: Option<Vec<u8>>) -> io::Result<()> {
pub fn encrypt<R: Read, W: Write>(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<R: Read, W: Write>(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())

View File

@ -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 {
&params,
cipher,
cli_args.block_size,
Some(magic_bytes)
Some(&magic_bytes[..n])
) {
Ok(_) => success = true,
Err(e) => eprintln!("I/O error while encrypting: {}", e)