Skip to content

Commit

Permalink
fix #18
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Aug 22, 2023
1 parent 0d89f03 commit dfa209c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 75 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sha256"
version = "1.3.0"
version = "1.4.0"
authors = ["baoyachi <[email protected]>"]
edition = "2018"
description = "sha256 crypto digest"
Expand All @@ -17,11 +17,13 @@ hex = "0.4.2"
sha2 = { version = "0.10.6", default-features = false }
openssl = { version = "0.10.54", optional = true, default-features = false }
async-trait = "0.1.68"
tokio = { version = "1.28.2", features = ["io-util", "fs"] }
tokio = { version = "1.28.2", optional = true, features = ["io-util", "fs"] }
bytes = "1.4.0"

[features]
default = ["async"]
native_openssl = ["openssl"]
async = ["tokio"]

[dev-dependencies]
tokio = { version = "1.28.2", features = ["full"] }
Expand Down
164 changes: 91 additions & 73 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
#[cfg(feature = "native_openssl")]
use crate::openssl_sha256::OpenSslSha256;
use bytes::BytesMut;

#[cfg(feature = "async")]
pub use async_digest::*;
use sha2::digest::Output;
use sha2::{Digest, Sha256};
use std::fmt::Debug;
use std::fs;
use std::io;
use std::io::{BufReader, Read};
use std::path::Path;
use tokio::io::AsyncReadExt;

/// sha256 digest string
///
Expand Down Expand Up @@ -69,42 +70,6 @@ pub fn try_digest<D: TrySha256Digest>(input: D) -> Result<String, D::Error> {
input.digest()
}

/// sha256 digest file
///
/// # Examples
///
/// ```rust
/// use sha256::{try_async_digest};
/// use std::path::Path;
/// let input = Path::new("./foo.file");
/// tokio_test::block_on(async{
/// let val = try_async_digest(input).await.unwrap();
/// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1")
/// });
///
/// ```
pub async fn try_async_digest<D: TrySha256Digest>(input: D) -> Result<String, D::Error> {
input.async_digest().await
}

/// sha256 digest file
///
/// # Examples
///
/// ```rust
/// use sha256::{try_async_openssl_digest};
/// use std::path::Path;
/// let input = Path::new("./foo.file");
/// tokio_test::block_on(async{
/// let val = try_async_openssl_digest(input).await.unwrap();
/// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1")
/// });
/// ```
#[cfg(feature = "native_openssl")]
pub async fn try_async_openssl_digest<D: TrySha256Digest>(input: D) -> Result<String, D::Error> {
input.async_openssl_digest().await
}

/// sha256 digest bytes
///
/// # Examples
Expand Down Expand Up @@ -148,6 +113,7 @@ pub trait TrySha256Digest {

fn digest(self) -> Result<String, Self::Error>;

#[cfg(feature = "async")]
async fn async_digest(self) -> Result<String, Self::Error>;

#[cfg(feature = "native_openssl")]
Expand Down Expand Up @@ -210,14 +176,15 @@ where
calc(reader, sha)
}

#[cfg(feature = "async")]
async fn async_digest(self) -> Result<String, Self::Error> {
let f = tokio::fs::File::open(self).await?;
let reader = tokio::io::BufReader::new(f);
let sha = Sha256::new();
async_calc(reader, sha).await
}

#[cfg(feature = "native_openssl")]
#[cfg(all(feature = "async", feature = "native_openssl"))]
async fn async_openssl_digest(self) -> Result<String, Self::Error> {
let f = tokio::fs::File::open(self).await?;
let reader = tokio::io::BufReader::new(f);
Expand All @@ -234,11 +201,6 @@ trait CalculatorInput {
fn read_inner(&mut self, buf: &mut [u8]) -> std::io::Result<usize>;
}

#[async_trait::async_trait]
trait AsyncCalculatorInput {
async fn read_inner(&mut self, buf: &mut BytesMut) -> std::io::Result<usize>;
}

impl<T> CalculatorInput for T
where
T: Read,
Expand All @@ -248,17 +210,7 @@ where
}
}

#[async_trait::async_trait]
impl<R> AsyncCalculatorInput for tokio::io::BufReader<R>
where
R: tokio::io::AsyncRead + Unpin + Send,
{
async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result<usize> {
self.read_buf(buf).await
}
}

trait CalculatorSelector {
pub trait CalculatorSelector {
type FinishType: AsRef<[u8]>;
fn update_inner(&mut self, data: &[u8]);
fn finish_inner(self) -> Self::FinishType;
Expand Down Expand Up @@ -293,22 +245,84 @@ where
Ok(hex::encode(hash))
}

async fn async_calc<I, S>(mut input: I, mut selector: S) -> io::Result<String>
where
I: AsyncCalculatorInput,
S: CalculatorSelector,
{
let mut buf = BytesMut::with_capacity(1024);
loop {
buf.clear();
let len = input.read_inner(&mut buf).await?;
if len == 0 {
break;
#[cfg(feature = "async")]
pub mod async_digest {
use crate::{CalculatorSelector, TrySha256Digest};
use bytes::BytesMut;
use std::io;

/// sha256 digest file
///
/// # Examples
///
/// ```rust
/// use sha256::{try_async_digest};
/// use std::path::Path;
/// let input = Path::new("./foo.file");
/// tokio_test::block_on(async{
/// let val = try_async_digest(input).await.unwrap();
/// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1")
/// });
///
/// ```
pub async fn try_async_digest<D: TrySha256Digest>(input: D) -> Result<String, D::Error> {
input.async_digest().await
}

/// sha256 digest file
///
/// # Examples
///
/// ```rust
/// use sha256::{try_async_openssl_digest};
/// use std::path::Path;
/// let input = Path::new("./foo.file");
/// tokio_test::block_on(async{
/// let val = try_async_openssl_digest(input).await.unwrap();
/// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1")
/// });
/// ```
#[cfg(feature = "native_openssl")]
pub async fn try_async_openssl_digest<D: TrySha256Digest>(
input: D,
) -> Result<String, D::Error> {
input.async_openssl_digest().await
}

#[async_trait::async_trait]
pub trait AsyncCalculatorInput {
async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result<usize>;
}

pub async fn async_calc<I, S>(mut input: I, mut selector: S) -> io::Result<String>
where
I: AsyncCalculatorInput,
S: CalculatorSelector,
{
let mut buf = BytesMut::with_capacity(1024);
loop {
buf.clear();
let len = input.read_inner(&mut buf).await?;
if len == 0 {
break;
}
selector.update_inner(&buf[0..len]);
}
let hash = selector.finish_inner();
Ok(hex::encode(hash))
}

#[async_trait::async_trait]
impl<R> AsyncCalculatorInput for tokio::io::BufReader<R>
where
R: tokio::io::AsyncRead + Unpin + Send,
{
async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result<usize> {
use tokio::io::AsyncReadExt;

self.read_buf(buf).await
}
selector.update_inner(&buf[0..len]);
}
let hash = selector.finish_inner();
Ok(hex::encode(hash))
}

#[cfg(feature = "native_openssl")]
Expand All @@ -333,7 +347,6 @@ mod openssl_sha256 {
#[cfg(test)]
mod tests {
use super::*;
use tokio::io::AsyncWriteExt;

#[cfg(feature = "native_openssl")]
#[test]
Expand All @@ -360,7 +373,7 @@ mod tests {
);
}

#[cfg(feature = "native_openssl")]
#[cfg(all(feature = "async", feature = "native_openssl"))]
#[tokio::test]
async fn test_async_openssl() {
let input = Path::new("./foo.file");
Expand All @@ -373,6 +386,7 @@ mod tests {
);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn test_async() {
let input = Path::new("./foo.file");
Expand All @@ -385,7 +399,7 @@ mod tests {
);
}

#[cfg(feature = "native_openssl")]
#[cfg(all(feature = "async", feature = "native_openssl"))]
#[tokio::test]
async fn test_try_async_openssl_digest() {
let hash = try_async_openssl_digest("./foo.file").await.unwrap();
Expand All @@ -395,6 +409,7 @@ mod tests {
);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn test_try_async_digest() {
let hash = try_async_digest("./foo.file").await.unwrap();
Expand All @@ -404,6 +419,7 @@ mod tests {
);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn test_async_parity() {
let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::<Vec<_>>();
Expand All @@ -417,6 +433,7 @@ mod tests {
let reader = tokio::io::BufReader::new(client);
let sha = Sha256::new();

use tokio::io::AsyncWriteExt;
tokio::join! {
async_calc(reader, sha),
async move {
Expand All @@ -437,7 +454,7 @@ mod tests {
assert_eq!(async_res, sync_res);
}

#[cfg(feature = "native_openssl")]
#[cfg(all(feature = "async", feature = "native_openssl"))]
#[tokio::test]
async fn test_async_parity_openssl() {
let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::<Vec<_>>();
Expand All @@ -451,6 +468,7 @@ mod tests {
let reader = tokio::io::BufReader::new(client);
let sha = OpenSslSha256::new();

use tokio::io::AsyncWriteExt;
tokio::join! {
async_calc(reader, sha),
async move {
Expand Down

0 comments on commit dfa209c

Please sign in to comment.