Skip to content

Instantly share code, notes, and snippets.

@nbari
Created August 16, 2020 21:08
Show Gist options
  • Save nbari/876101a38550cad832bde09449f02c03 to your computer and use it in GitHub Desktop.
Save nbari/876101a38550cad832bde09449f02c03 to your computer and use it in GitHub Desktop.
how to use traits
use futures::stream::TryStreamExt;
use ring::digest::{Context, SHA256};
use std::error::Error;
use std::fmt::Write;
use std::time::Instant;
use tokio::fs::File;
use tokio::io::AsyncBufRead;
use tokio::io::BufReader;
// use tokio::prelude::*;
use tokio_util::codec::{BytesCodec, FramedRead};
#[tokio::main]
async fn main() {
/*
let now = Instant::now();
let checksum = blake("/tmp/wine.json").await.unwrap();
println!("blake: {}", checksum);
let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed);
*/
let now = Instant::now();
let checksum = sha256_digest("/tmp/wine.json").await.unwrap();
println!("sha256: {}", checksum);
let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed);
}
async fn blake(file_path: &str) -> Result<String, Box<dyn Error>> {
let file = File::open(file_path).await?;
let mut stream = FramedRead::new(file, BytesCodec::new());
let mut hasher = blake2s_simd::State::new();
while let Some(bytes) = stream.try_next().await? {
hasher.update(&bytes);
}
Ok(hasher.finalize().to_hex().to_string())
}
async fn sha256_digest(file_path: &str) -> Result<String, Box<dyn Error>> {
let file = File::open(file_path).await?;
let mut stream = FramedRead::new(file, BytesCodec::new());
let mut context = Context::new(&SHA256);
while let Some(bytes) = stream.try_next().await? {
context.update(&bytes);
}
let digest = context.finish();
Ok(write_hex_bytes(digest.as_ref()))
}
async fn sha256_digest_BufReader(file_path: &str) -> Result<String, Box<dyn Error>> {
let file = File::open(file_path).await?;
let mut reader = BufReader::new(file);
let mut context = Context::new(&SHA256);
loop {
let consummed = {
let buffer = reader.buffer();
if buffer.is_empty() {
break;
}
context.update(buffer);
buffer.len()
};
reader.consume(consummed);
}
let digest = context.finish();
Ok(write_hex_bytes(digest.as_ref()))
}
pub fn write_hex_bytes(bytes: &[u8]) -> String {
let mut s = String::new();
for byte in bytes {
write!(&mut s, "{:02x}", byte).expect("Unable to write");
}
s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment