Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
@@ -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)
}