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
117
118
119
120
121
122
//! Implementations for commonly used traits like `digest::Digest` and
//! `crypto_mac::Mac`.

use crate::{Hasher, OutputReader};
use digest::generic_array::{
    typenum::{U32, U64},
    GenericArray,
};

impl digest::BlockInput for Hasher {
    type BlockSize = U64;
}

impl digest::Input for Hasher {
    fn input<B: AsRef<[u8]>>(&mut self, data: B) {
        self.update(data.as_ref());
    }
}

impl digest::Reset for Hasher {
    fn reset(&mut self) {
        self.reset(); // the inherent method
    }
}

impl digest::FixedOutput for Hasher {
    type OutputSize = U32;

    fn fixed_result(self) -> GenericArray<u8, Self::OutputSize> {
        GenericArray::clone_from_slice(self.finalize().as_bytes())
    }
}

impl digest::ExtendableOutput for Hasher {
    type Reader = OutputReader;

    fn xof_result(self) -> Self::Reader {
        self.finalize_xof()
    }
}

impl digest::XofReader for OutputReader {
    fn read(&mut self, buffer: &mut [u8]) {
        self.fill(buffer);
    }
}

impl crypto_mac::Mac for Hasher {
    type OutputSize = U32;
    type KeySize = U32;

    fn new(key: &GenericArray<u8, Self::KeySize>) -> Self {
        let key_bytes: [u8; 32] = (*key).into();
        Hasher::new_keyed(&key_bytes)
    }

    fn input(&mut self, data: &[u8]) {
        self.update(data);
    }

    fn reset(&mut self) {
        self.reset();
    }

    fn result(self) -> crypto_mac::MacResult<Self::OutputSize> {
        crypto_mac::MacResult::new((*self.finalize().as_bytes()).into())
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn test_digest_traits() {
        // Inherent methods.
        let mut hasher1 = crate::Hasher::new();
        hasher1.update(b"foo");
        hasher1.update(b"bar");
        hasher1.update(b"baz");
        let out1 = hasher1.finalize();
        let mut xof1 = [0; 301];
        hasher1.finalize_xof().fill(&mut xof1);
        assert_eq!(out1.as_bytes(), &xof1[..32]);

        // Trait implementations.
        let mut hasher2: crate::Hasher = digest::Digest::new();
        digest::Digest::input(&mut hasher2, b"xxx");
        digest::Digest::reset(&mut hasher2);
        digest::Digest::input(&mut hasher2, b"foo");
        digest::Digest::input(&mut hasher2, b"bar");
        digest::Digest::input(&mut hasher2, b"baz");
        let out2 = digest::Digest::result(hasher2.clone());
        let mut xof2 = [0; 301];
        digest::XofReader::read(
            &mut digest::ExtendableOutput::xof_result(hasher2),
            &mut xof2,
        );
        assert_eq!(out1.as_bytes(), &out2[..]);
        assert_eq!(xof1[..], xof2[..]);
    }

    #[test]
    fn test_mac_trait() {
        // Inherent methods.
        let key = b"some super secret key bytes fooo";
        let mut hasher1 = crate::Hasher::new_keyed(key);
        hasher1.update(b"foo");
        hasher1.update(b"bar");
        hasher1.update(b"baz");
        let out1 = hasher1.finalize();

        // Trait implementation.
        let generic_key = (*key).into();
        let mut hasher2: crate::Hasher = crypto_mac::Mac::new(&generic_key);
        crypto_mac::Mac::input(&mut hasher2, b"xxx");
        crypto_mac::Mac::reset(&mut hasher2);
        crypto_mac::Mac::input(&mut hasher2, b"foo");
        crypto_mac::Mac::input(&mut hasher2, b"bar");
        crypto_mac::Mac::input(&mut hasher2, b"baz");
        let out2 = crypto_mac::Mac::result(hasher2);
        assert_eq!(out1.as_bytes(), out2.code().as_slice());
    }
}