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
use openssl::crypto::hash;
use ::rr::dnssec::Algorithm;
use ::error::*;
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum DigestType {
SHA1,
SHA256,
SHA384,
SHA512,
}
impl DigestType {
pub fn from_u8(value: u8) -> DecodeResult<Self> {
match value {
1 => Ok(DigestType::SHA1),
2 => Ok(DigestType::SHA256),
4 => Ok(DigestType::SHA384),
_ => Err(DecodeError::UnknownAlgorithmTypeValue(value)),
}
}
pub fn to_hash(&self) -> hash::Type {
match *self {
DigestType::SHA1 => hash::Type::SHA1,
DigestType::SHA256 => hash::Type::SHA256,
DigestType::SHA384 => hash::Type::SHA384,
DigestType::SHA512 => hash::Type::SHA512,
}
}
pub fn hash(&self, data: &[u8]) -> Vec<u8> {
hash::hash(self.to_hash(), data)
}
}
impl From<Algorithm> for DigestType {
fn from(a: Algorithm) -> DigestType {
match a {
Algorithm::RSASHA1 | Algorithm::RSASHA1NSEC3SHA1 => DigestType::SHA1,
Algorithm::RSASHA256 => DigestType::SHA256,
Algorithm::RSASHA512 => DigestType::SHA512,
}
}
}
impl From<DigestType> for u8 {
fn from(a: DigestType) -> u8 {
match a {
DigestType::SHA1 => 1,
DigestType::SHA256 => 2,
DigestType::SHA384 => 4,
_ => panic!("No code for this type: {:?}", a)
}
}
}