1 | #![allow (clippy::duplicate_mod)] |
2 | |
3 | use super::ring_like::digest; |
4 | use crate::crypto; |
5 | use crate::msgs::enums::HashAlgorithm; |
6 | |
7 | use alloc::boxed::Box; |
8 | |
9 | pub(crate) static SHA256: Hash = Hash(&digest::SHA256, HashAlgorithm::SHA256); |
10 | pub(crate) static SHA384: Hash = Hash(&digest::SHA384, HashAlgorithm::SHA384); |
11 | |
12 | pub(crate) struct Hash(&'static digest::Algorithm, HashAlgorithm); |
13 | |
14 | impl crypto::hash::Hash for Hash { |
15 | fn start(&self) -> Box<dyn crypto::hash::Context> { |
16 | Box::new(Context(digest::Context::new(self.0))) |
17 | } |
18 | |
19 | fn hash(&self, bytes: &[u8]) -> crypto::hash::Output { |
20 | let mut ctx: Context = digest::Context::new(self.0); |
21 | ctx.update(data:bytes); |
22 | convert(val:ctx.finish()) |
23 | } |
24 | |
25 | fn output_len(&self) -> usize { |
26 | self.0.output_len() |
27 | } |
28 | |
29 | fn algorithm(&self) -> HashAlgorithm { |
30 | self.1 |
31 | } |
32 | } |
33 | |
34 | struct Context(digest::Context); |
35 | |
36 | impl crypto::hash::Context for Context { |
37 | fn fork_finish(&self) -> crypto::hash::Output { |
38 | convert(self.0.clone().finish()) |
39 | } |
40 | |
41 | fn fork(&self) -> Box<dyn crypto::hash::Context> { |
42 | Box::new(Self(self.0.clone())) |
43 | } |
44 | |
45 | fn finish(self: Box<Self>) -> crypto::hash::Output { |
46 | convert(self.0.finish()) |
47 | } |
48 | |
49 | fn update(&mut self, data: &[u8]) { |
50 | self.0.update(data); |
51 | } |
52 | } |
53 | |
54 | fn convert(val: digest::Digest) -> crypto::hash::Output { |
55 | crypto::hash::Output::new(bytes:val.as_ref()) |
56 | } |
57 | |