Commit
Author: Dirkjan Ochtman [dirkjan@ochtman.nl]
Hash: d8b40549873ef885dbfba77490f19c7cd7891235
Timestamp: Wed, 14 Dec 2022 12:35:13 +0000 (1 year ago)

+10 -20 +/-2 browse
Use Writer trait for Base32Writer
1diff --git a/src/common/base32.rs b/src/common/base32.rs
2index 8207e4f..c9e9d88 100644
3--- a/src/common/base32.rs
4+++ b/src/common/base32.rs
5 @@ -8,7 +8,7 @@
6 * except according to those terms.
7 */
8
9- use std::io;
10+ use super::headers::Writer;
11
12 pub(crate) static BASE32_ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
13
14 @@ -68,30 +68,19 @@ impl Base32Writer {
15 }
16 }
17
18- impl io::Write for Base32Writer {
19- fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
20- let start_pos = self.pos;
21-
22- for &byte in bytes {
23+ impl Writer for Base32Writer {
24+ fn write(&mut self, buf: &[u8]) {
25+ for &byte in buf {
26 self.push_byte(byte, false);
27 }
28-
29- Ok(self.pos - start_pos)
30- }
31-
32- fn flush(&mut self) -> io::Result<()> {
33- Ok(())
34 }
35 }
36
37 #[cfg(test)]
38 mod tests {
39-
40- use std::io::Write;
41-
42 use sha1::{Digest, Sha1};
43
44- use crate::common::base32::Base32Writer;
45+ use crate::common::{base32::Base32Writer, headers::Writer};
46
47 #[test]
48 fn base32_hash() {
49 @@ -102,7 +91,7 @@ mod tests {
50 let mut writer = Base32Writer::with_capacity(10);
51 let mut hash = Sha1::new();
52 hash.update(test.as_bytes());
53- writer.write_all(&hash.finalize()[..]).ok();
54+ writer.write(&hash.finalize()[..]);
55 assert_eq!(writer.finalize(), expected_result);
56 }
57 }
58 diff --git a/src/dkim/verify.rs b/src/dkim/verify.rs
59index 626e9e1..ed53aa7 100644
60--- a/src/dkim/verify.rs
61+++ b/src/dkim/verify.rs
62 @@ -8,7 +8,7 @@
63 * except according to those terms.
64 */
65
66- use std::{io::Write, time::SystemTime};
67+ use std::time::SystemTime;
68
69 use sha1::{Digest, Sha1};
70 use sha2::Sha256;
71 @@ -16,6 +16,7 @@ use sha2::Sha256;
72 use crate::{
73 common::{
74 base32::Base32Writer,
75+ headers::Writer,
76 verify::{DomainKey, VerifySignature},
77 },
78 is_within_pct, AuthenticatedMessage, DkimOutput, DkimResult, Error, Resolver,
79 @@ -135,14 +136,14 @@ impl Resolver {
80 let mut writer = Base32Writer::with_capacity(40);
81 let mut hash = Sha256::new();
82 hash.update(signature.d.as_bytes());
83- writer.write_all(&hash.finalize()[..]).ok();
84+ writer.write(hash.finalize().as_ref());
85 writer.finalize()
86 }
87 Some(HashAlgorithm::Sha1) => {
88 let mut writer = Base32Writer::with_capacity(40);
89 let mut hash = Sha1::new();
90 hash.update(signature.d.as_bytes());
91- writer.write_all(&hash.finalize()[..]).ok();
92+ writer.write(hash.finalize().as_ref());
93 writer.finalize()
94 }
95 None => signature.d.to_string(),