src/hash_and_binary_functions.rs
// hash some data
pub async fn hash_data(data: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(data);
let result = hasher.finalize();
result.iter().map(|b| format!("{:02x}", b)).collect::<String>()
}
// synchronous hash data
pub fn sync_hash_data(data: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(data);
let result = hasher.finalize();
result.iter().map(|b| format!("{:02x}", b)).collect::<String>()
}
// validate some hash
pub async fn validate_hash(data: &[u8], expected_hash: &str) -> bool {
let mut hasher = Sha256::new();
hasher.update(data);
let result = hasher.finalize();
let calculated_hash = hex::encode(result);
calculated_hash == expected_hash
}