diff --git a/Cargo.toml b/Cargo.toml index b37a937..2886df5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptohelpers" -version = "1.6.0" +version = "1.7.0" license= "MIT" description = "Collection of helpers and simplifying functions for cryptography things" authors = ["Avril "] @@ -20,10 +20,11 @@ libc = "0.2" tokio = {version = "0.2", features=["io-util"], optional=true} serde_derive = {version = "1.0", optional = true} serde = {version = "1.0", optional = true} +futures = {version = "0.3.8", optional=true} [features] default = ["full", "async", "serialise"] -async = ["tokio"] +async = ["tokio", "futures"] # Actual things full = [ diff --git a/src/lib.rs b/src/lib.rs index 9ddeedd..03b02ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,10 +31,14 @@ use serde_derive::{ // Actual things +#[cfg(feature="sha256")] +pub use sha2; #[cfg(feature="sha256")] pub mod sha256; + #[cfg(feature="password")] pub mod password; + #[cfg(feature="aes")] pub mod aes; #[cfg(feature="checksum")] diff --git a/src/sha256.rs b/src/sha256.rs index 4e836ff..0431590 100644 --- a/src/sha256.rs +++ b/src/sha256.rs @@ -135,6 +135,38 @@ where T: AsRef<[u8]> Sha256Hash{hash} } +/// Compute a SHA256 hash from a stream of slices +#[cfg(feature="async")] +pub async fn compute_slices_stream(mut from: I) -> Sha256Hash +where I: futures::stream::Stream + std::marker::Unpin, + T: AsRef<[u8]> +{ + use futures::stream::StreamExt; + let mut hasher = Sha256::new(); + while let Some(from) = from.next().await { + hasher.update(from.as_ref()); + } + + let mut hash = [0u8; SIZE]; + bytes::copy_slice(&mut hash, &hasher.finalize()); + Sha256Hash{hash} +} + +/// Compute a SHA256 hash from a number of slices +pub fn compute_slices(from: I) -> Sha256Hash +where I: IntoIterator, + T: AsRef<[u8]> +{ + let mut hasher = Sha256::new(); + for from in from.into_iter() { + hasher.update(from.as_ref()); + } + + let mut hash = [0u8; SIZE]; + bytes::copy_slice(&mut hash, &hasher.finalize()); + Sha256Hash{hash} +} + /// Compute the SHA256 hash of the rest of this stream pub fn compute_sync(from: &mut T) -> io::Result