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
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use blake2::Blake2b;
use digest::{
    generic_array::typenum::{U20, U32},
    FixedOutput, FixedOutputReset, HashMarker, Output, OutputSizeUser, Reset, Update,
};

// blake2 has [`Blake2s256`] instance but not for 160 bits.
/// Blake2b instance with a 256-bit output.
#[derive(Clone)]
pub struct Blake2b256(Blake2b<U32>);

impl Blake2b256 {
    /// Creates a new [`Blake2b256`] instance.
    pub fn new() -> Self {
        Self(Blake2b::default())
    }
}

impl Default for Blake2b256 {
    fn default() -> Self {
        Self::new()
    }
}

impl OutputSizeUser for Blake2b256 {
    type OutputSize = U32;
}

impl Reset for Blake2b256 {
    fn reset(&mut self) {
        self.0.reset();
    }
}

impl FixedOutput for Blake2b256 {
    fn finalize_into(self, out: &mut Output<Self>) {
        self.0.finalize_into(out);
    }
}

impl FixedOutputReset for Blake2b256 {
    fn finalize_into_reset(&mut self, out: &mut Output<Self>) {
        self.0.finalize_into_reset(out);
    }
}

impl Update for Blake2b256 {
    fn update(&mut self, data: &[u8]) {
        self.0.update(data);
    }
}

impl HashMarker for Blake2b256 {}

/// Blake2b instance with a 160-bit output.
#[derive(Clone)]
pub struct Blake2b160(Blake2b<U20>);

impl Blake2b160 {
    /// Creates a new [`Blake2b160`] instance.
    pub fn new() -> Self {
        Self(Blake2b::default())
    }
}

impl Default for Blake2b160 {
    fn default() -> Self {
        Self::new()
    }
}

impl OutputSizeUser for Blake2b160 {
    type OutputSize = U20;
}

impl Reset for Blake2b160 {
    fn reset(&mut self) {
        self.0.reset();
    }
}

impl FixedOutput for Blake2b160 {
    fn finalize_into(self, out: &mut Output<Self>) {
        self.0.finalize_into(out);
    }
}

impl FixedOutputReset for Blake2b160 {
    fn finalize_into_reset(&mut self, out: &mut Output<Self>) {
        self.0.finalize_into_reset(out);
    }
}

impl Update for Blake2b160 {
    fn update(&mut self, data: &[u8]) {
        self.0.update(data);
    }
}

impl HashMarker for Blake2b160 {}