1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#[macro_export]
macro_rules! sha256 {
($data:expr) => {{
use sha2::{Digest, Sha256};
let mut sha = Sha256::new();
sha.update($data);
sha.finalize().into()
}};
}
#[cfg(test)]
mod tests {
#[test]
fn test_compute_hash() {
let data = b"Hello, world!";
let hash: [u8; 32] = sha256!(data);
assert_eq!(
hash,
[
49, 95, 91, 219, 118, 208, 120, 196, 59, 138, 192, 6, 78, 74, 1, 100, 97, 43, 31,
206, 119, 200, 105, 52, 91, 252, 148, 199, 88, 148, 237, 211
]
);
}
}