sha256: added compute_slices()

tokio-1.0
Avril 4 years ago
parent 05bbb95f66
commit 4e1d21b7d7
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,6 +1,6 @@
[package] [package]
name = "cryptohelpers" name = "cryptohelpers"
version = "1.6.0" version = "1.7.0"
license= "MIT" license= "MIT"
description = "Collection of helpers and simplifying functions for cryptography things" description = "Collection of helpers and simplifying functions for cryptography things"
authors = ["Avril <flanchan@cumallover.me>"] authors = ["Avril <flanchan@cumallover.me>"]
@ -20,10 +20,11 @@ libc = "0.2"
tokio = {version = "0.2", features=["io-util"], optional=true} tokio = {version = "0.2", features=["io-util"], optional=true}
serde_derive = {version = "1.0", optional = true} serde_derive = {version = "1.0", optional = true}
serde = {version = "1.0", optional = true} serde = {version = "1.0", optional = true}
futures = {version = "0.3.8", optional=true}
[features] [features]
default = ["full", "async", "serialise"] default = ["full", "async", "serialise"]
async = ["tokio"] async = ["tokio", "futures"]
# Actual things # Actual things
full = [ full = [

@ -31,10 +31,14 @@ use serde_derive::{
// Actual things // Actual things
#[cfg(feature="sha256")]
pub use sha2;
#[cfg(feature="sha256")] #[cfg(feature="sha256")]
pub mod sha256; pub mod sha256;
#[cfg(feature="password")] #[cfg(feature="password")]
pub mod password; pub mod password;
#[cfg(feature="aes")] #[cfg(feature="aes")]
pub mod aes; pub mod aes;
#[cfg(feature="checksum")] #[cfg(feature="checksum")]

@ -135,6 +135,38 @@ where T: AsRef<[u8]>
Sha256Hash{hash} Sha256Hash{hash}
} }
/// Compute a SHA256 hash from a stream of slices
#[cfg(feature="async")]
pub async fn compute_slices_stream<T, I>(mut from: I) -> Sha256Hash
where I: futures::stream::Stream<Item=T> + 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<T, I>(from: I) -> Sha256Hash
where I: IntoIterator<Item=T>,
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 /// Compute the SHA256 hash of the rest of this stream
pub fn compute_sync<T>(from: &mut T) -> io::Result<Sha256Hash> pub fn compute_sync<T>(from: &mut T) -> io::Result<Sha256Hash>

Loading…
Cancel
Save