ic_representation_independent_hash/
lib.rs

1//! # Representation Independent Hash
2
3#![deny(missing_docs, missing_debug_implementations, rustdoc::all, clippy::all)]
4
5/// Type alias for a SHA-256 hash.
6pub type Sha256Digest = [u8; 32];
7
8mod representation_independent_hash;
9pub use representation_independent_hash::*;
10
11use sha2::{Digest, Sha256};
12
13/// Calculates the SHA-256 hash of the given slice.
14pub fn hash(data: &[u8]) -> Sha256Digest {
15    let mut hasher = Sha256::new();
16    hasher.update(data);
17    hasher.finalize().into()
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn hash_text() {
26        let text = "Hello World!";
27        let expected_hash: Sha256Digest = [
28            127, 131, 177, 101, 127, 241, 252, 83, 185, 45, 193, 129, 72, 161, 214, 93, 252, 45,
29            75, 31, 163, 214, 119, 40, 74, 221, 210, 0, 18, 109, 144, 105,
30        ];
31
32        let result = hash(text.as_bytes());
33
34        assert_eq!(result, expected_hash);
35    }
36}