Skip to content

Commit

Permalink
fix: hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
nhussein11 committed Feb 16, 2023
1 parent de2a950 commit 2739673
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sha2 = "0.10.2"
sha256 = "1.0.3"
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use sha2::Digest;
use std::cell::RefCell;
use std::rc::Rc;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash as StdHash, Hasher};


mod tests;
pub type Data = Vec<u8>;
pub type Hash = Vec<u8>;
mod tests; pub type Data = Vec<u8>;
pub type Hash = String;

#[derive(Debug)]
pub struct MerkleTree {
Expand Down Expand Up @@ -132,13 +131,15 @@ fn pair_off_tree(input: &[Data]) -> Vec<Data> {
}
}
}
/// Hashes the given data using sha256
/// Hashes the given data using Hash from std
fn hash_data(data: &Data) -> Hash {
sha2::Sha256::digest(data).to_vec()
let mut hasher = DefaultHasher::new();
data.hash(&mut hasher);
hasher.finish().to_string()
}
/// Concatenates the given hashes and returns the hash of the concatenated data
fn concatenate_and_hash(left: Hash, right: Hash) -> Hash {
let parent_hash = [left, right].concat();
let parent_hash = format!("{}{}", left, right).into_bytes();
hash_data(&parent_hash)
}
/// Update child node to point to parent
Expand Down

0 comments on commit 2739673

Please sign in to comment.