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
use crate::reverse_digest::ReversibleDigest;
use digest::consts::U20;
use digest::{
    consts::{U32, U64},
    BlockInput, Digest, FixedOutputDirty, Reset, Update,
};

use ripemd160::Ripemd160;
use sha2::Sha256;

#[derive(Clone)]
pub struct Hash160 {
    engine: Sha256,
    reverse: bool,
}

impl ReversibleDigest for Hash160 {
    fn reverse(&self) -> Self {
        let mut reversed = self.clone();
        reversed.reverse = true;
        reversed
    }
}

impl Hash160 {
    pub fn new(reverse: bool) -> Self {
        let engine: Sha256 = Sha256::default();
        Self { engine, reverse }
    }
}

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

impl BlockInput for Hash160 {
    type BlockSize = U64;
}

impl Update for Hash160 {
    fn update(&mut self, input: impl AsRef<[u8]>) {
        digest::Digest::update(&mut self.engine, input.as_ref())
    }
}

impl FixedOutputDirty for Hash160 {
    type OutputSize = U20;

    fn finalize_into_dirty(&mut self, out: &mut digest::Output<Self>) {
        let first_hash = self.clone().engine.finalize();
        let finished_hash = &mut *Ripemd160::digest(&*first_hash);
        if self.reverse {
            finished_hash.reverse()
        }

        out.copy_from_slice(finished_hash);
    }
}

impl Reset for Hash160 {
    fn reset(&mut self) {
        self.engine = Sha256::default();
    }
}

digest::impl_write!(Hash160);