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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use std::convert::TryFrom;

use blake2b_simd::{Params as Blake2bParams, State as Blake2b};
use blake2s_simd::{Params as Blake2sParams, State as Blake2s};

use crate::digests::{wrap, Multihash, MultihashDigest, Multihasher};
use crate::errors::DecodeError;

#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_code {
    ($(
        #[$doc:meta]
        $name:ident => $code:expr,
    )*) => {
        /// The code of Multihash.
        #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub enum Code {
            $(
                #[$doc]
                $name,
            )*
        }

        impl Code {
            /// Hash some input and return the raw binary digest.
            pub fn digest(&self, data: &[u8]) -> Multihash {
                match self {
                    $(Self::$name => $name::digest(data),)*
                }
            }
        }

        impl From<Code> for Box<dyn MultihashDigest<Code>> {
            fn from(code: Code) -> Self {
                match code {
                    $(Code::$name => Box::new($name::default()),)*
                }
            }
        }

        impl From<Code> for u64 {
            /// Return the code as integer value.
            fn from(code: Code) -> Self {
                match code {
                    $(Code::$name => $code,)*
                }
            }
        }

        impl TryFrom<u64> for Code {
            type Error = DecodeError;

            /// Return the `Code` based on the integer value. Error if no matching code exists.
            fn try_from(raw: u64) -> Result<Self, Self::Error> {
                match raw {
                    $($code => Ok(Self::$name),)*
                    _ => Err(DecodeError::UnknownCode),
                }
            }
        }
    };
}

#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! derive_digest {
    ($(
        #[$doc:meta]
        @sha $type:ty as $name:ident;
            @code_doc $code_doc:literal,
    )*) => {
        $(
            #[$doc]
            #[derive(Clone, Debug, Default)]
            pub struct $name($type);
            impl $name {
                #[doc = $code_doc]
                pub const CODE: Code = Code::$name;
                /// Hash some input and return the Multihash digest.
                #[inline]
                pub fn digest(data: &[u8]) -> Multihash {
                    let digest = <$type as digest::Digest>::digest(&data);
                    wrap(Self::CODE, &digest)
                }
            }
            impl Multihasher<Code> for $name {
                const CODE: Code = Code::$name;
                #[inline]
                fn digest(data: &[u8]) -> Multihash {
                    Self::digest(data)
                }
            }
            impl MultihashDigest<Code> for $name {
                #[inline]
                fn code(&self) -> Code {
                    Self::CODE
                }
                #[inline]
                fn digest(&self, data: &[u8]) -> Multihash {
                    Self::digest(data)
                }
                #[inline]
                fn input(&mut self, data: &[u8]) {
                    <$type as digest::Digest>::update(&mut self.0, data)
                }
                #[inline]
                fn result(self) -> Multihash {
                    wrap(Self::CODE, <$type as digest::Digest>::finalize(self.0).as_slice())
                }
                #[inline]
                fn result_reset(&mut self) -> Multihash {
                    wrap(Self::CODE, <$type as digest::Digest>::finalize_reset(&mut self.0).as_slice())
                }
                #[inline]
                fn reset(&mut self) {
                    <$type as digest::Digest>::reset(&mut self.0)
                }
            }
            impl ::std::io::Write for $name {
                #[inline]
                fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
                    <$name as MultihashDigest<Code>>::input(self, buf);
                    Ok(buf.len())
                }
                #[inline]
                fn flush(&mut self) -> ::std::io::Result<()> {
                    Ok(())
                }
            }
        )*
    };
    ($(
        #[$doc:meta]
        @blake $type:ty | $params:ty as $name:ident $len:expr;
            @code_doc $code_doc:literal,
    )*) => {
        $(
            #[$doc]
            #[derive(Clone, Debug)]
            pub struct $name($type);
            impl $name {
                #[doc = $code_doc]
                pub const CODE: Code = Code::$name;
                /// Hash some input and return the Multihash digest.
                pub fn digest(data: &[u8]) -> Multihash {
                    let digest = <$params>::new().hash_length($len).hash(data);
                    wrap(Self::CODE, &digest.as_bytes())
                }
            }
            impl Default for $name {
                fn default() -> Self {
                    $name(<$params>::new().hash_length($len).to_state())
                }
            }
            impl Multihasher<Code> for $name {
                const CODE: Code = Code::$name;
                #[inline]
                fn digest(data: &[u8]) -> Multihash {
                    Self::digest(data)
                }
            }
            impl MultihashDigest<Code> for $name {
                #[inline]
                fn code(&self) -> Code {
                    Self::CODE
                }
                #[inline]
                fn digest(&self, data: &[u8]) -> Multihash {
                    Self::digest(data)
                }
                #[inline]
                fn input(&mut self, data: &[u8]) {
                    self.0.update(data);
                }
                #[inline]
                fn result(self) -> Multihash {
                    let digest = self.0.finalize();
                    wrap(Self::CODE, digest.as_bytes())
                }
                #[inline]
                fn result_reset(&mut self) -> Multihash {
                    let digest = self.0.finalize();
                    let hash = wrap(Self::CODE, digest.as_bytes());
                    self.reset();
                    hash
                }
                #[inline]
                fn reset(&mut self) {
                    self.0 = Self::default().0;
                }
            }
            impl ::std::io::Write for $name {
                #[inline]
                fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
                    <$name as MultihashDigest<Code>>::input(self, buf);
                    Ok(buf.len())
                }
                #[inline]
                fn flush(&mut self) -> ::std::io::Result<()> {
                    self.0.finalize();
                    Ok(())
                }
            }
        )*
    };
    ($(
        #[$doc:meta]
        @blake3 $type:ty as $name:ident;
            @code_doc $code_doc:literal,
    )*) => {
        $(
            #[$doc]
            #[derive(Clone, Debug, Default)]
            pub struct $name($type);
            impl $name {
                #[doc = $code_doc]
                pub const CODE: Code = Code::$name;
                /// Hash some input and return the Multihash digest.
                pub fn digest(data: &[u8]) -> Multihash {
                    let digest = blake3::hash(data);
                    wrap(Self::CODE, digest.as_bytes())
                }
            }
            impl Multihasher<Code> for $name {
                const CODE: Code = Code::$name;
                #[inline]
                fn digest(data: &[u8]) -> Multihash {
                    Self::digest(data)
                }
            }
            impl MultihashDigest<Code> for $name {
                #[inline]
                fn code(&self) -> Code {
                    Self::CODE
                }
                #[inline]
                fn digest(&self, data: &[u8]) -> Multihash {
                    Self::digest(data)
                }
                #[inline]
                fn input(&mut self, data: &[u8]) {
                    self.0.update(data);
                }
                #[inline]
                fn result(self) -> Multihash {
                    let digest = self.0.finalize();
                    wrap(Self::CODE, digest.as_bytes())
                }
                #[inline]
                fn result_reset(&mut self) -> Multihash {
                    let digest = self.0.finalize();
                    let hash = wrap(Self::CODE, digest.as_bytes());
                    self.0.reset();
                    hash
                }
                #[inline]
                fn reset(&mut self) {
                    self.0.reset();
                }
            }
            impl ::std::io::Write for $name {
                #[inline]
                fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
                    <$name as MultihashDigest<Code>>::input(self, buf);
                    Ok(buf.len())
                }
                #[inline]
                fn flush(&mut self) -> ::std::io::Result<()> {
                    self.0.finalize();
                    Ok(())
                }
            }
        )*
    }
}

#[cfg(not(feature = "use_blake3"))]
impl_code! {
    /// Identity (Raw binary)
    Identity => 0x00,
    /// SHA-1 (20-byte hash size)
    Sha1 => 0x11,
    /// SHA-256 (32-byte hash size)
    Sha2_256 => 0x12,
    /// SHA-512 (64-byte hash size)
    Sha2_512 => 0x13,
    /// SHA3-224 (28-byte hash size)
    Sha3_224 => 0x17,
    /// SHA3-256 (32-byte hash size)
    Sha3_256 => 0x16,
    /// SHA3-384 (48-byte hash size)
    Sha3_384 => 0x15,
    /// SHA3-512 (64-byte hash size)
    Sha3_512 => 0x14,
    /// Keccak-224 (28-byte hash size)
    Keccak224 => 0x1a,
    /// Keccak-256 (32-byte hash size)
    Keccak256 => 0x1b,
    /// Keccak-384 (48-byte hash size)
    Keccak384 => 0x1c,
    /// Keccak-512 (64-byte hash size)
    Keccak512 => 0x1d,
    /// BLAKE2b-256 (32-byte hash size)
    Blake2b256 => 0xb220,
    /// BLAKE2b-512 (64-byte hash size)
    Blake2b512 => 0xb240,
    /// BLAKE2s-128 (16-byte hash size)
    Blake2s128 => 0xb250,
    /// BLAKE2s-256 (32-byte hash size)
    Blake2s256 => 0xb260,
}
#[cfg(feature = "use_blake3")]
impl_code! {
    /// Identity (Raw binary)
    Identity => 0x00,
    /// SHA-1 (20-byte hash size)
    Sha1 => 0x11,
    /// SHA-256 (32-byte hash size)
    Sha2_256 => 0x12,
    /// SHA-512 (64-byte hash size)
    Sha2_512 => 0x13,
    /// SHA3-224 (28-byte hash size)
    Sha3_224 => 0x17,
    /// SHA3-256 (32-byte hash size)
    Sha3_256 => 0x16,
    /// SHA3-384 (48-byte hash size)
    Sha3_384 => 0x15,
    /// SHA3-512 (64-byte hash size)
    Sha3_512 => 0x14,
    /// Keccak-224 (28-byte hash size)
    Keccak224 => 0x1a,
    /// Keccak-256 (32-byte hash size)
    Keccak256 => 0x1b,
    /// Keccak-384 (48-byte hash size)
    Keccak384 => 0x1c,
    /// Keccak-512 (64-byte hash size)
    Keccak512 => 0x1d,
    /// BLAKE2b-256 (32-byte hash size)
    Blake2b256 => 0xb220,
    /// BLAKE2b-512 (64-byte hash size)
    Blake2b512 => 0xb240,
    /// BLAKE2s-128 (16-byte hash size)
    Blake2s128 => 0xb250,
    /// BLAKE2s-256 (32-byte hash size)
    Blake2s256 => 0xb260,
    /// BLAKE3 (32-byte hash size)
    Blake3 => 0x1e,
}

/// The Identity hasher.
#[derive(Clone, Debug, Default)]
pub struct Identity(Vec<u8>);
impl Identity {
    /// The code of the Identity hasher, 0x00.
    pub const CODE: Code = Code::Identity;
    /// Hash some input and return the raw binary digest.
    pub fn digest(data: &[u8]) -> Multihash {
        if (data.len() as u64) >= u64::from(std::u32::MAX) {
            panic!("Input data for identity hash is too large, it needs to be less than 2^32.")
        }
        wrap(Self::CODE, data)
    }
}
impl Multihasher<Code> for Identity {
    const CODE: Code = Code::Identity;
    #[inline]
    fn digest(data: &[u8]) -> Multihash {
        Self::digest(data)
    }
}
impl MultihashDigest<Code> for Identity {
    #[inline]
    fn code(&self) -> Code {
        Self::CODE
    }
    #[inline]
    fn digest(&self, data: &[u8]) -> Multihash {
        Self::digest(data)
    }
    #[inline]
    fn input(&mut self, data: &[u8]) {
        if ((self.0.len() as u64) + (data.len() as u64)) >= u64::from(std::u32::MAX) {
            panic!("Input data for identity hash is too large, it needs to be less than 2^32.")
        }
        self.0.extend_from_slice(data)
    }
    #[inline]
    fn result(self) -> Multihash {
        wrap(Self::CODE, &self.0)
    }
    #[inline]
    fn result_reset(&mut self) -> Multihash {
        let hash = wrap(Self::CODE, &self.0);
        self.reset();
        hash
    }
    #[inline]
    fn reset(&mut self) {
        self.0.clear()
    }
}
impl ::std::io::Write for Identity {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
        <Identity as MultihashDigest<Code>>::input(self, buf);
        Ok(buf.len())
    }
    #[inline]
    fn flush(&mut self) -> ::std::io::Result<()> {
        Ok(())
    }
}

derive_digest! {
    /// The SHA-1 hasher.
    @sha ::sha1::Sha1 as Sha1;
        @code_doc "The code of the SHA-1 hasher, 0x11.",
    /// The SHA2-256 hasher.
    @sha ::sha2::Sha256 as Sha2_256;
        @code_doc "The code of the SHA2-256 hasher, 0x12.",
    /// The SHA2-512 hasher.
    @sha ::sha2::Sha512 as Sha2_512;
        @code_doc "The code of the SHA2-512 hasher, 0x13.",
    /// The SHA3-224 hasher.
    @sha ::sha3::Sha3_224 as Sha3_224;
        @code_doc "The code of the SHA3-224 hasher, 0x17.",
    /// The SHA3-256 hasher.
    @sha ::sha3::Sha3_256 as Sha3_256;
        @code_doc "The code of the SHA3-256 hasher, 0x16.",
    /// The SHA3-384 hasher.
    @sha ::sha3::Sha3_384 as Sha3_384;
        @code_doc "The code of the SHA3-384 hasher, 0x15.",
    /// The SHA3-512 hasher.
    @sha ::sha3::Sha3_512 as Sha3_512;
        @code_doc "The code of the SHA3-512 hasher, 0x14.",
    /// The Keccak-224 hasher.
    @sha ::sha3::Keccak224 as Keccak224;
        @code_doc "The code of the Keccak-224 hasher, 0x1a.",
    /// The Keccak-256 hasher.
    @sha ::sha3::Keccak256 as Keccak256;
        @code_doc "The code of the Keccak-256 hasher, 0x1b.",
    /// The Keccak-384 hasher.
    @sha ::sha3::Keccak384 as Keccak384;
        @code_doc "The code of the Keccak-384 hasher, 0x1c.",
    /// The Keccak-512 hasher.
    @sha ::sha3::Keccak512 as Keccak512;
        @code_doc "The code of the Keccak-512 hasher, 0x1d.",
}
derive_digest! {
    /// The Blake2b-256 hasher.
    @blake Blake2b | Blake2bParams as Blake2b256 32;
        @code_doc "The code of the Blake2-256 hasher, 0xb220.",
    /// The Blake2b-512 hasher.
    @blake Blake2b | Blake2bParams as Blake2b512 64;
        @code_doc "The code of the Blake2-512 hasher, 0xb240.",
    /// The Blake2s-128 hasher.
    @blake Blake2s | Blake2sParams as Blake2s128 16;
        @code_doc "The code of the Blake2-128 hasher, 0xb250.",
    /// The Blake2s-256 hasher.
    @blake Blake2s | Blake2sParams as Blake2s256 32;
        @code_doc "The code of the Blake2-256 hasher, 0xb260.",
}
#[cfg(feature = "use_blake3")]
derive_digest! {
    /// The Blake3 hasher.
    @blake3 blake3::Hasher as Blake3;
        @code_doc "The code of the Blake3 hasher, 0x1e.",
}