From 95447dce6ff99a9ffc52bd0bba4eea6ea239d0ce Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 29 Jul 2021 14:09:17 +0100 Subject: [PATCH] Added aes en/decryption slice->vec functions (sync+async) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix aes sync functions marked as async Fortune for cryptohelpers's current commit: Future blessing − 末吉 --- Cargo.toml | 2 +- src/aes.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4d66928..ecba3bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptohelpers" -version = "1.7.3" +version = "1.8.0" license= "MIT" description = "Collection of helpers and simplifying functions for cryptography things" authors = ["Avril "] diff --git a/src/aes.rs b/src/aes.rs index cb9538d..b4fbec5 100644 --- a/src/aes.rs +++ b/src/aes.rs @@ -187,7 +187,7 @@ where F: AsyncRead + Unpin + ?Sized, } /// Encrypt a stream into another using a key -pub async fn encrypt_stream_sync(key: &AesKey, from: &mut F, to: &mut T) -> Result +pub fn encrypt_stream_sync(key: &AesKey, from: &mut F, to: &mut T) -> Result where F: io::Read + ?Sized, T: io::Write + ?Sized { @@ -234,7 +234,7 @@ where F: AsyncRead + Unpin + ?Sized, } /// Decrypt a stream into another using a key -pub async fn decrypt_stream_sync(key: &AesKey, from: &mut F, to: &mut T) -> Result +pub fn decrypt_stream_sync(key: &AesKey, from: &mut F, to: &mut T) -> Result where F: io::Read + ?Sized, T: io::Write + ?Sized { @@ -256,6 +256,48 @@ where F: io::Read + ?Sized, Ok(done + bytes_encrypted) } +/// Decrypt a slice to a `Vec` async +#[cfg(feature="async")] +pub async fn decrypt_slice(key: &AesKey, from: impl AsRef<[u8]>) -> Result, Error> +{ + let from = from.as_ref(); + let mut to = Vec::with_capacity(from.len() + 128); + + decrypt_stream(key, &mut &from[..], &mut to).await?; + Ok(to) +} + +/// Decrypt a slice to a `Vec` sync +pub fn decrypt_slice_sync(key: &AesKey, from: impl AsRef<[u8]>) -> Result, Error> +{ + let from = from.as_ref(); + let mut to = Vec::with_capacity(from.len() + 128); + + decrypt_stream_sync(key, &mut &from[..], &mut to)?; + Ok(to) +} + +/// Encrypt a slice to a `Vec` async +#[cfg(feature="async")] +pub async fn encrypt_slice(key: &AesKey, from: impl AsRef<[u8]>) -> Result, Error> +{ + let from = from.as_ref(); + let mut to = Vec::with_capacity(from.len() + 128); + + encrypt_stream(key, &mut &from[..], &mut to).await?; + Ok(to) +} + +/// Encrypt a slice to a `Vec` sync +pub fn encrypt_slice_sync(key: &AesKey, from: impl AsRef<[u8]>) -> Result, Error> +{ + let from = from.as_ref(); + let mut to = Vec::with_capacity(from.len() + 128); + + encrypt_stream_sync(key, &mut &from[..], &mut to)?; + Ok(to) +} + pub use crate::error::aes::Error; #[cfg(test)]