Commit
+26 -14 +/-2 browse
1 | diff --git a/src/common/crypto.rs b/src/common/crypto.rs |
2 | index 1aaa007..5e206d2 100644 |
3 | --- a/src/common/crypto.rs |
4 | +++ b/src/common/crypto.rs |
5 | @@ -234,6 +234,29 @@ pub enum HashAlgorithm { |
6 | Sha256 = R_HASH_SHA256, |
7 | } |
8 | |
9 | + impl HashAlgorithm { |
10 | + pub fn hash(&self, data: &[u8]) -> HashOutput { |
11 | + match self { |
12 | + Self::Sha1 => HashOutput::Sha1(Sha1::digest(data)), |
13 | + Self::Sha256 => HashOutput::Sha256(Sha256::digest(data)), |
14 | + } |
15 | + } |
16 | + } |
17 | + |
18 | + pub enum HashOutput { |
19 | + Sha1(Output<Sha1>), |
20 | + Sha256(Output<Sha256>), |
21 | + } |
22 | + |
23 | + impl AsRef<[u8]> for HashOutput { |
24 | + fn as_ref(&self) -> &[u8] { |
25 | + match self { |
26 | + Self::Sha1(output) => output.as_ref(), |
27 | + Self::Sha256(output) => output.as_ref(), |
28 | + } |
29 | + } |
30 | + } |
31 | + |
32 | impl TryFrom<&ObjectIdentifier> for HashAlgorithm { |
33 | type Error = Error; |
34 | |
35 | diff --git a/src/dkim/verify.rs b/src/dkim/verify.rs |
36 | index ed53aa7..26099f9 100644 |
37 | --- a/src/dkim/verify.rs |
38 | +++ b/src/dkim/verify.rs |
39 | @@ -10,9 +10,6 @@ |
40 | |
41 | use std::time::SystemTime; |
42 | |
43 | - use sha1::{Digest, Sha1}; |
44 | - use sha2::Sha256; |
45 | - |
46 | use crate::{ |
47 | common::{ |
48 | base32::Base32Writer, |
49 | @@ -132,18 +129,10 @@ impl Resolver { |
50 | |
51 | if found { |
52 | let mut query_domain = match &signature.atpsh { |
53 | - Some(HashAlgorithm::Sha256) => { |
54 | - let mut writer = Base32Writer::with_capacity(40); |
55 | - let mut hash = Sha256::new(); |
56 | - hash.update(signature.d.as_bytes()); |
57 | - writer.write(hash.finalize().as_ref()); |
58 | - writer.finalize() |
59 | - } |
60 | - Some(HashAlgorithm::Sha1) => { |
61 | + Some(algorithm) => { |
62 | let mut writer = Base32Writer::with_capacity(40); |
63 | - let mut hash = Sha1::new(); |
64 | - hash.update(signature.d.as_bytes()); |
65 | - writer.write(hash.finalize().as_ref()); |
66 | + let output = algorithm.hash(signature.d.as_bytes()); |
67 | + writer.write(output.as_ref()); |
68 | writer.finalize() |
69 | } |
70 | None => signature.d.to_string(), |