Expand description
§RustCrypto: async tokio::io-compatibility wrappers for digest
Async counterpart to digest-io, providing tokio::io-compatible
wrappers for traits defined in the digest crate.
§Examples
Simultaneously reading and hashing file data asynchronously:
ⓘ
use digest_io_async::HashReader;
use sha2::Sha256;
use tokio::{fs::File, io};
#[tokio::main(flavor = "current_thread")]
async fn main() -> io::Result<()> {
// Create new hashing reader
let f = File::open("Cargo.toml").await?;
let mut reader = HashReader::<Sha256, File>::new(f);
// Copy all data out of the file without buffering it up front
let mut sink = Vec::new();
io::copy(&mut reader, &mut sink).await?;
// Get the resulting hash over the read data
let hash = reader.finalize();
println!("Hash: {hash:?}");
Ok(())
}Simultaneously hashing data and writing it to file asynchronously:
ⓘ
use digest_io_async::HashWriter;
use sha2::{Digest, Sha256};
use tokio::{fs::File, io::AsyncWriteExt};
#[tokio::main(flavor = "current_thread")]
async fn main() -> std::io::Result<()> {
// Create new hashing writer
let f = File::create("out.txt").await?;
let mut writer = HashWriter::<Sha256, File>::new(f);
// Write data to the file
let data = b"Hello world!";
writer.write_all(data).await?;
// Get the resulting hash over written data
let hash = writer.finalize();
println!("{hash:?}");
assert_eq!(hash, Sha256::digest(data));
tokio::fs::remove_file("out.txt").await?;
Ok(())
}§License
Licensed under either of:
at your option.
§Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Structs§
- Hash
Reader - Abstraction over an
AsyncReadreader which hashes the data being read. - Hash
Writer - Abstraction over an
AsyncWritewriter which hashes the data being written.