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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::{error, fmt, ops::Deref, str::FromStr};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Md5Checksum([u8; 16]);
impl Deref for Md5Checksum {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for Md5Checksum {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.iter() {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}
impl From<[u8; 16]> for Md5Checksum {
fn from(checksum: [u8; 16]) -> Self {
Self(checksum)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
InvalidLength(usize),
InvalidHexDigit(char),
}
impl error::Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidLength(len) => write!(f, "expected length to be 32, got {}", len),
Self::InvalidHexDigit(c) => write!(f, "invalid hex digit: {}", c),
}
}
}
impl FromStr for Md5Checksum {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() != 32 {
return Err(ParseError::InvalidLength(s.len()));
}
let mut checksum = [0; 16];
for (digits, value) in s.as_bytes().chunks(2).zip(checksum.iter_mut()) {
let l = parse_digit(digits[0])?;
let r = parse_digit(digits[1])?;
*value = l << 4 | r;
}
Ok(Self(checksum))
}
}
impl From<Md5Checksum> for [u8; 16] {
fn from(md5_checksum: Md5Checksum) -> Self {
md5_checksum.0
}
}
fn parse_digit(b: u8) -> Result<u8, ParseError> {
match b {
b'a'..=b'f' => Ok(b - b'a' + 10),
b'0'..=b'9' => Ok(b - b'0'),
_ => Err(ParseError::InvalidHexDigit(char::from(b))),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
let md5_checksum = Md5Checksum::from([
0xd7, 0xeb, 0xa3, 0x11, 0x42, 0x1b, 0xbc, 0x9d, 0x3a, 0xda, 0x44, 0x70, 0x9d, 0xd6,
0x15, 0x34,
]);
assert_eq!(md5_checksum.to_string(), "d7eba311421bbc9d3ada44709dd61534");
let md5_checksum = Md5Checksum::from([
0xb0, 0x0c, 0x61, 0xdf, 0xed, 0x4a, 0x92, 0xfd, 0xfb, 0x24, 0x4d, 0x35, 0x79, 0x05,
0x56, 0xeb,
]);
assert_eq!(md5_checksum.to_string(), "b00c61dfed4a92fdfb244d35790556eb");
}
#[test]
fn test_from_u8_array_for_md5_checksum() {
let checksum = [
0xd7, 0xeb, 0xa3, 0x11, 0x42, 0x1b, 0xbc, 0x9d, 0x3a, 0xda, 0x44, 0x70, 0x9d, 0xd6,
0x15, 0x34,
];
assert_eq!(*Md5Checksum::from(checksum), checksum);
}
#[test]
fn test_parse() {
assert_eq!(
"d7eba311421bbc9d3ada44709dd61534".parse(),
Ok(Md5Checksum::from([
0xd7, 0xeb, 0xa3, 0x11, 0x42, 0x1b, 0xbc, 0x9d, 0x3a, 0xda, 0x44, 0x70, 0x9d, 0xd6,
0x15, 0x34,
]))
);
assert_eq!("".parse::<Md5Checksum>(), Err(ParseError::InvalidLength(0)));
assert_eq!(
"d7".parse::<Md5Checksum>(),
Err(ParseError::InvalidLength(2))
);
assert_eq!(
"838f8d9acc45bd36e3213c47c3222e644f44c959fa370bbfa6df46b171c02f0c"
.parse::<Md5Checksum>(),
Err(ParseError::InvalidLength(64))
);
assert_eq!(
"D7EBA311421BBC9D3ADA44709DD61534".parse::<Md5Checksum>(),
Err(ParseError::InvalidHexDigit('D'))
);
assert!(matches!(
"n7eba311421bbc9d3ada44709dd61534".parse::<Md5Checksum>(),
Err(ParseError::InvalidHexDigit('n'))
));
}
#[test]
fn test_from_md5_checksum_for_u8_array() {
let checksum = [
0xd7, 0xeb, 0xa3, 0x11, 0x42, 0x1b, 0xbc, 0x9d, 0x3a, 0xda, 0x44, 0x70, 0x9d, 0xd6,
0x15, 0x34,
];
let md5_checksum = Md5Checksum::from(checksum);
assert_eq!(<[u8; 16]>::from(md5_checksum), checksum);
}
}