hash/
lib.rs

1use std::fmt;
2mod sha1;
3mod sha3;
4
5pub struct Digest(Vec<u8>);
6
7impl Digest {
8    pub fn sha1(m: &[u8]) -> Digest {
9        let a = sha1::from(m);
10        let mut digest: Vec<u8> = Vec::new();
11        digest.extend(a.iter().cloned());
12        Digest(digest)
13    }
14
15    pub fn sha3(m: &[u8]) -> Digest {
16        let a = sha3::from(m);
17        let mut digest: Vec<u8> = Vec::new();
18        digest.extend(a.iter().cloned());
19        Digest(digest)
20    }
21}
22
23impl Clone for Digest {
24    fn clone(&self) -> Self {
25        Digest(self.0.clone())
26    }
27}
28
29impl fmt::LowerHex for Digest {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        for byte in self.0.iter() {
32            try!(write!(f, "{:02x}", byte));
33        }
34        Ok(())
35    }
36}
37
38impl fmt::Display for Digest {
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        fmt::LowerHex::fmt(self, f)
41    }
42}
43
44impl fmt::Debug for Digest {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        fmt::LowerHex::fmt(self, f)
47    }
48}
49
50impl PartialEq for Digest {
51    fn eq(&self, other: &Self) -> bool {
52        // yay, timing attack
53        for (a, b) in self.0.iter().zip(other.0.iter()) {
54            if a != b {
55                return false;
56            }
57        }
58        true
59    }
60}
61
62impl Eq for Digest {}