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
159
160
161
162
163
extern crate sha1;
extern crate sha2;
extern crate tiny_keccak;
use std::fmt::Write;
use sha2::Digest;
use tiny_keccak::Keccak;
mod hashes;
pub use hashes::*;
mod errors;
pub use errors::*;
macro_rules! encode {
(sha1, Sha1, $input:expr, $output:expr) => ({
let mut hasher = sha1::Sha1::new();
hasher.update($input);
$output.copy_from_slice(&hasher.digest().bytes());
});
(sha2, $algorithm:ident, $input:expr, $output:expr) => ({
let mut hasher = sha2::$algorithm::default();
hasher.input($input);
$output.copy_from_slice(hasher.result().as_ref());
});
(tiny, $constructor:ident, $input:expr, $output:expr) => ({
let mut kec = Keccak::$constructor();
kec.update($input);
kec.finalize($output);
});
}
macro_rules! match_encoder {
($hash:ident for ($input:expr, $output:expr) {
$( $hashtype:ident => $lib:ident :: $method:ident, )*
}) => ({
match $hash {
$(
Hash::$hashtype => encode!($lib, $method, $input, $output),
)*
_ => return Err(Error::UnsupportedType)
}
})
}
pub fn encode(hash: Hash, input: &[u8]) -> Result<Vec<u8>, Error> {
let size = hash.size();
let mut output = Vec::new();
output.resize(2 + size as usize, 0);
output[0] = hash.code();
output[1] = size;
match_encoder!(hash for (input, &mut output[2..]) {
SHA1 => sha1::Sha1,
SHA2256 => sha2::Sha256,
SHA2512 => sha2::Sha512,
SHA3224 => tiny::new_sha3_224,
SHA3256 => tiny::new_sha3_256,
SHA3384 => tiny::new_sha3_384,
SHA3512 => tiny::new_sha3_512,
Keccak224 => tiny::new_keccak224,
Keccak256 => tiny::new_keccak256,
Keccak384 => tiny::new_keccak384,
Keccak512 => tiny::new_keccak512,
});
Ok(output)
}
pub fn decode(input: &[u8]) -> Result<Multihash, Error> {
if input.is_empty() {
return Err(Error::BadInputLength);
}
let code = input[0];
let alg = Hash::from_code(code)?;
let hash_len = alg.size() as usize;
if input.len() != hash_len + 2 {
return Err(Error::BadInputLength);
}
Ok(Multihash {
alg: alg,
digest: &input[2..],
})
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct Multihash<'a> {
pub alg: Hash,
pub digest: &'a [u8],
}
pub fn to_hex(bytes: &[u8]) -> String {
let mut hex = String::with_capacity(bytes.len() * 2);
for byte in bytes {
write!(hex, "{:02x}", byte).expect("Can't fail on writing to string");
}
hex
}