|
1 | 1 | use multihash::*; |
2 | 2 |
|
3 | | -/// Helper function to convert a hex-encoded byte array back into a bytearray |
4 | | -fn hex_to_bytes(s: &str) -> Vec<u8> { |
5 | | - let mut c = 0; |
6 | | - let mut v = Vec::new(); |
7 | | - while c < s.len() { |
8 | | - v.push(u8::from_str_radix(&s[c..c + 2], 16).unwrap()); |
9 | | - c += 2; |
10 | | - } |
11 | | - v |
12 | | -} |
13 | | - |
14 | 3 | macro_rules! assert_encode { |
15 | 4 | {$( $alg:ty, $data:expr, $expect:expr; )*} => { |
16 | 5 | $( |
17 | | - let hex = hex_to_bytes($expect); |
| 6 | + let bytes = hex::decode($expect).unwrap(); |
18 | 7 | assert_eq!( |
19 | 8 | <$alg>::digest($data).into_bytes(), |
20 | | - hex, |
| 9 | + bytes, |
21 | 10 | "{:?} encodes correctly", stringify!($alg) |
22 | 11 | ); |
23 | 12 |
|
24 | 13 | let mut hasher = <$alg>::default(); |
25 | 14 | &mut hasher.input($data); |
26 | 15 | assert_eq!( |
27 | 16 | hasher.result().into_bytes(), |
28 | | - hex, |
| 17 | + bytes, |
29 | 18 | "{:?} encodes correctly", stringify!($alg) |
30 | 19 | ); |
31 | 20 | )* |
@@ -55,13 +44,14 @@ fn multihash_encode() { |
55 | 44 | Blake2s256, b"hello world", "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b"; |
56 | 45 | Blake2b256, b"hello world", "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610"; |
57 | 46 | Blake2s128, b"hello world", "d0e4021037deae0226c30da2ab424a7b8ee14e83"; |
| 47 | + Blake3, b"hello world", "1e20d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24"; |
58 | 48 | } |
59 | 49 | } |
60 | 50 |
|
61 | 51 | macro_rules! assert_decode { |
62 | 52 | {$( $alg:ty, $hash:expr; )*} => { |
63 | 53 | $( |
64 | | - let hash = hex_to_bytes($hash); |
| 54 | + let hash = hex::decode($hash).unwrap(); |
65 | 55 | assert_eq!( |
66 | 56 | MultihashRef::from_slice(&hash).unwrap().algorithm(), |
67 | 57 | <$alg>::CODE, |
@@ -91,6 +81,7 @@ fn assert_decode() { |
91 | 81 | Blake2s256, "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b"; |
92 | 82 | Blake2b256, "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610"; |
93 | 83 | Blake2s128, "d0e4021037deae0226c30da2ab424a7b8ee14e83"; |
| 84 | + Blake3, "1e20d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24"; |
94 | 85 | } |
95 | 86 | } |
96 | 87 |
|
@@ -128,15 +119,15 @@ fn assert_roundtrip() { |
128 | 119 |
|
129 | 120 | /// Testing the public interface of `Multihash` and `MultihashRef` |
130 | 121 | fn test_methods(hash: impl MultihashDigest<Code>, prefix: &str, digest: &str) { |
131 | | - let expected_bytes = hex_to_bytes(&format!("{}{}", prefix, digest)); |
| 122 | + let expected_bytes = hex::decode(&format!("{}{}", prefix, digest)).unwrap(); |
132 | 123 | let multihash = hash.digest(b"hello world"); |
133 | 124 | assert_eq!( |
134 | 125 | Multihash::from_bytes(expected_bytes.clone()).unwrap(), |
135 | 126 | multihash |
136 | 127 | ); |
137 | 128 | assert_eq!(multihash.as_bytes(), &expected_bytes[..]); |
138 | 129 | assert_eq!(multihash.algorithm(), hash.code()); |
139 | | - assert_eq!(multihash.digest(), &hex_to_bytes(digest)[..]); |
| 130 | + assert_eq!(multihash.digest(), hex::decode(digest).unwrap().as_slice()); |
140 | 131 |
|
141 | 132 | let multihash_ref = multihash.as_ref(); |
142 | 133 | assert_eq!(multihash, multihash_ref); |
@@ -226,6 +217,11 @@ fn multihash_methods() { |
226 | 217 | "d0e40210", |
227 | 218 | "37deae0226c30da2ab424a7b8ee14e83", |
228 | 219 | ); |
| 220 | + test_methods( |
| 221 | + Blake3::default(), |
| 222 | + "1e20", |
| 223 | + "d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24", |
| 224 | + ); |
229 | 225 | } |
230 | 226 |
|
231 | 227 | #[test] |
|
0 commit comments