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
use std::fmt;

pub mod keccak;
pub mod sha1;

const OUTPUT_LEN: usize = 64;

pub struct Digest(pub [u8; OUTPUT_LEN]);

impl Digest {
    pub fn keccak(m: &[u8]) -> Digest {
        keccak::from(m)
    }

    pub fn sha1(m: &[u8]) -> Digest {
        sha1::from(m)
    }
}

impl Clone for Digest {
    fn clone(&self) -> Self { *self }
}

impl Copy for Digest {}

impl fmt::LowerHex for Digest {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for byte in self.0.iter() {
            try!(write!(f, "{:02x}", byte));
        }
        Ok(())
    }
}

impl fmt::Display for Digest {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::LowerHex::fmt(self, f)
    }
}

impl fmt::Debug for Digest {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::LowerHex::fmt(self, f)
    }
}

impl PartialEq for Digest {
    fn eq(&self, other: &Self) -> bool {
        // yay, timing attack
        for (a, b) in self.0.iter().zip(other.0.iter()) {
            if a != b {
                return false;
            }
        }
        true
    }
}

impl Eq for Digest {}