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
/// A digest that either passes through calls to an actual digest or returns
/// pre-existing hash bytes..
///
/// For signing implementations that require a pre-filled digest instance to
/// sign instead of plain digest bytes, construct an instance of `DummyDigest`
/// with the bytes of the hash. The instance will return these bytes when
/// finalized. It will panic if an attempt is made to update its state.
///
/// If it was initialized empty, it will pass all calls through to the actual
/// digest function, useful for signers that do further internal hashing when
/// signing.
pub(crate) struct DummyDigest<D> {
    underlying: Option<D>,
    preexisting: Vec<u8>,
}

impl<D> DummyDigest<D> {
    pub(crate) fn new_precomputed(bytes: &[u8]) -> Self {
        Self {
            underlying: None,
            preexisting: bytes.to_vec(),
        }
    }
}

impl<D> Default for DummyDigest<D>
where
    D: Default,
{
    fn default() -> Self {
        Self {
            underlying: Some(D::default()),
            preexisting: Vec::new(),
        }
    }
}

impl<D> Clone for DummyDigest<D>
where
    D: Clone,
{
    fn clone(&self) -> Self {
        Self {
            underlying: self.underlying.clone(),
            preexisting: self.preexisting.clone(),
        }
    }
}

impl<D> digest::OutputSizeUser for DummyDigest<D>
where
    D: digest::OutputSizeUser,
{
    type OutputSize = <D as digest::OutputSizeUser>::OutputSize;
}

impl<D> digest::core_api::BlockSizeUser for DummyDigest<D>
where
    D: digest::core_api::BlockSizeUser,
{
    type BlockSize = <D as digest::core_api::BlockSizeUser>::BlockSize;
}

impl<D> digest::FixedOutput for DummyDigest<D>
where
    D: digest::FixedOutput,
{
    fn finalize_into(self, out: &mut digest::Output<Self>) {
        if let Some(digest) = self.underlying {
            digest.finalize_into(out);
        } else {
            out.as_mut_slice().copy_from_slice(&self.preexisting);
        }
    }
}

impl<D> digest::FixedOutputReset for DummyDigest<D>
where
    D: digest::FixedOutputReset,
{
    fn finalize_into_reset(&mut self, out: &mut digest::Output<Self>) {
        if let Some(digest) = &mut self.underlying {
            digest.finalize_into_reset(out);
        } else {
            out.as_mut_slice().copy_from_slice(&self.preexisting);
        }
    }
}

impl<D> digest::Reset for DummyDigest<D>
where
    D: digest::Reset,
{
    fn reset(&mut self) {
        if let Some(ref mut digest) = self.underlying {
            digest.reset();
        } else {
            panic!("mutating dummy digest with precomputed hash");
        }
    }
}

impl<D> digest::Update for DummyDigest<D>
where
    D: digest::Update,
{
    fn update(&mut self, data: &[u8]) {
        if let Some(ref mut digest) = self.underlying {
            digest.update(data);
        } else {
            panic!("mutating dummy digest with precomputed hash");
        }
    }
}

impl<D> digest::HashMarker for DummyDigest<D> where D: digest::HashMarker {}