1
0
Fork 0
This commit is contained in:
Mo 2023-04-19 22:39:20 +02:00
commit 660391fb99
4 changed files with 185 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
test.txt

111
Cargo.lock generated Normal file
View file

@ -0,0 +1,111 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cpufeatures"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "280a9f2d8b3a38871a3c8a46fb80db65e5e5ed97da80c4d08bf27fb63e35e181"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "hashing"
version = "0.1.0"
dependencies = [
"digest",
"hex",
"md-5",
"sha2",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "libc"
version = "0.2.141"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
[[package]]
name = "md-5"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6365506850d44bff6e2fbcb5176cf63650e48bd45ef2fe2665ae1570e0f4b9ca"
dependencies = [
"digest",
]
[[package]]
name = "sha2"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "typenum"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"

12
Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "hashing"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
digest = "0.10.6"
hex = "0.4.3"
md-5 = "0.10.5"
sha2 = "0.10.6"

60
src/main.rs Normal file
View file

@ -0,0 +1,60 @@
use digest::Digest;
use md5::Md5;
use sha2::Sha256;
use std::{
fs::File,
io::Read,
sync::{mpsc::sync_channel, Arc},
thread,
};
fn main() {
let mut file = File::open("test.txt").unwrap();
let mut buffer = vec![0; 4096];
let mut sha256_hasher = Sha256::new();
let mut md5_hasher = Md5::new();
let channel_capacity = 1;
let (sha256_tx, sha256_rx) = sync_channel::<Arc<Vec<u8>>>(channel_capacity);
let (md5_tx, md5_rx) = sync_channel::<Arc<Vec<u8>>>(channel_capacity);
let sha256_handle = thread::spawn(move || {
while let Ok(buf) = sha256_rx.recv() {
sha256_hasher.update(&*buf);
}
sha256_hasher.finalize()
});
let md5_handle = thread::spawn(move || {
while let Ok(buf) = md5_rx.recv() {
md5_hasher.update(&*buf);
}
md5_hasher.finalize()
});
loop {
let n = file.read(&mut buffer).unwrap();
if n == 0 {
break;
}
let buffer_arc = Arc::new(buffer[..n].to_vec());
sha256_tx.send(Arc::clone(&buffer_arc)).unwrap();
md5_tx.send(buffer_arc).unwrap();
}
// Drop senders for the threads to terminate.
drop(sha256_tx);
drop(md5_tx);
let md5_result = md5_handle.join().unwrap();
let sha256_result = sha256_handle.join().unwrap();
println!("md5: {:?}", hex::encode(md5_result));
println!("sha256: {:?}", hex::encode(sha256_result));
}