1use std::ops::{Deref, DerefMut};
2use crate::*;
3
4macro_rules! new_sha3 {
5 ( $sid:ident, $tid:ident, $hname:ident, $tname:ident, $len:expr, $str:expr, $hwhash:expr ) => {
6 new_hash!($hname, $tname, $len, $str);
7
8 pub struct $sid(lib_sha3::$sid);
9 impl Deref for $sid {
10 type Target = lib_sha3::$sid;
11 fn deref(&self) -> &Self::Target {
12 &self.0
13 }
14 }
15 impl DerefMut for $sid {
16 fn deref_mut(&mut self) -> &mut Self::Target {
17 &mut self.0
18 }
19 }
20
21 impl Hasher for $sid {
22 type HasherOutput = $hname;
23
24 fn output_type() -> &'static HashType {
25 &$tname as &HashType
26 }
27
28 fn new() -> Self {
29 $sid(lib_sha3::Digest::new())
30 }
31
32 fn input(&mut self, buf: &[u8]) -> Result<(), std::io::Error> {
33 Ok(lib_sha3::digest::Input::input(&mut self.0, buf))
34 }
35
36 fn result(self) -> Box<Self::HasherOutput> {
37 let hash = lib_sha3::digest::FixedOutput::fixed_result(self.0);
38 let mut array: [u8; $len] = [0; $len]; assert!(hash.as_slice().len() == array.len());
40
41 array.copy_from_slice(&hash);
42 Box::new($hname(array))
43 }
44 }
45
46 #[test]
47 fn $tid() {
48 let mut hasher: $sid = $sid::new();
49 hasher.input(b"Hello World").unwrap();
50 let hash: Box<$hname> = hasher.result();
51
52 let htype = $tname;
53 let newhash = htype.new_with_content(hash.as_slice()).unwrap();
54
55 assert!(format!("{:?}", newhash) == format!("Hash<{}, {}>", $str, $hwhash));
56 }
57 };
58}
59
60new_sha3!(
61 Sha3_224,
62 test_sha3_224,
63 Hash_Sha3_224,
64 HashType_Sha3_224,
65 28,
66 "sha3_224",
67 "8e800079a0b311788bf29353f400eff969b650a3597c91efd9aa5b38"
68);
69new_sha3!(
70 Sha3_256,
71 test_sha3_256,
72 Hash_Sha3_256,
73 HashType_Sha3_256,
74 32,
75 "sha3_256",
76 "e167f68d6563d75bb25f3aa49c29ef612d41352dc00606de7cbd630bb2665f51"
77);
78new_sha3!(
79 Sha3_384,
80 test_sha3_384,
81 Hash_Sha3_384,
82 HashType_Sha3_384,
83 48,
84 "sha3_384",
85 "a78ec2851e991638ce505d4a44efa606dd4056d3ab274ec6fdbac00cde16478263ef7213bad5a7db7044f58d637afdeb"
86);
87new_sha3!(
88 Sha3_512,
89 test_sha3_512,
90 Hash_Sha3_512,
91 HashType_Sha3_512,
92 64,
93 "sha3_512",
94 "3d58a719c6866b0214f96b0a67b37e51a91e233ce0be126a08f35fdf4c043c6126f40139bfbc338d44eb2a03de9f7bb8eff0ac260b3629811e389a5fbee8a894"
95);