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
use std::cmp::Ordering;
use crate::crypto;
use crate::crypto::mem;
use crate::packet;
use crate::Packet;
#[derive(Clone, Debug)]
pub struct MDC {
pub(crate) common: packet::Common,
computed_digest: [u8; 20],
digest: [u8; 20],
}
assert_send_and_sync!(MDC);
impl PartialEq for MDC {
fn eq(&self, other: &MDC) -> bool {
self.common == other.common
&& self.digest == other.digest
}
}
impl Eq for MDC {}
impl std::hash::Hash for MDC {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(&self.common, state);
std::hash::Hash::hash(&self.digest, state);
}
}
impl MDC {
pub fn new(digest: [u8; 20], computed_digest: [u8; 20]) -> Self {
MDC {
common: Default::default(),
computed_digest,
digest,
}
}
pub fn digest(&self) -> &[u8] {
&self.digest[..]
}
pub fn computed_digest(&self) -> &[u8] {
&self.computed_digest[..]
}
pub fn valid(&self) -> bool {
if self.digest == [ 0; 20 ] {
false
} else {
mem::secure_cmp(&self.computed_digest, &self.digest) == Ordering::Equal
}
}
}
impl From<MDC> for Packet {
fn from(s: MDC) -> Self {
#[allow(deprecated)]
Packet::MDC(s)
}
}
impl From<[u8; 20]> for MDC {
fn from(digest: [u8; 20]) -> Self {
MDC {
common: Default::default(),
computed_digest: Default::default(),
digest,
}
}
}
impl From<Box<dyn crypto::hash::Digest>> for MDC {
fn from(mut hash: Box<dyn crypto::hash::Digest>) -> Self {
let mut value : [u8; 20] = Default::default();
let _ = hash.digest(&mut value[..]);
value.into()
}
}