From 06c7cafe7fd3875d4af497d100de527681233241 Mon Sep 17 00:00:00 2001 From: RockerBot Date: Sat, 22 Oct 2022 01:35:09 +0530 Subject: [PATCH] chunked file input followed same documentation --- src/algorithms.rs | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/algorithms.rs b/src/algorithms.rs index 52b95e9..b7eeee0 100644 --- a/src/algorithms.rs +++ b/src/algorithms.rs @@ -1,16 +1,42 @@ -use md5; -use sha2::{Digest, Sha256}; +use md5::Context; +use sha2::{Digest as shaDigest, Sha256}; +use std::io::{ prelude::*, BufReader}; pub fn calc_sha256(filepath: &str) -> String { - let file_as_bytes = std::fs::read(filepath).unwrap(); - let hash = Sha256::digest(file_as_bytes); + // breaks file into lines and cumulatively calculates the hash + let file = std::fs::File::open(filepath).unwrap(); + let reader = BufReader::new(file); + + let mut hasher = Sha256::new(); + for line in reader.lines(){ + match line { + Ok(a) => { + hasher.update(a.as_bytes()); + hasher.update(b"\n"); + }, + _ => break, + }; + } + let hash = hasher.finalize(); format!("{:x}", hash) } pub fn calc_md5(filepath: &str) -> String { - let file_as_bytes = std::fs::read(filepath).unwrap(); - let hash = md5::compute(file_as_bytes); + let file = std::fs::File::open(filepath).unwrap(); + let reader = BufReader::new(file); + + let mut hasher = Context::new(); + for line in reader.lines(){ + match line { + Ok(a) => { + hasher.consume(a.as_bytes()); + hasher.consume(b"\n"); + }, + _ => break, + }; + } + let hash = hasher.compute(); format!("{:x}", hash) }