Files
agnostic_orderbook
ahash
aho_corasick
arrayref
arrayvec
atty
base64
bincode
blake3
block_buffer
block_padding
borsh
borsh_derive
borsh_derive_internal
borsh_schema_derive_internal
bs58
bv
bytemuck
byteorder
cfg_if
constant_time_eq
cpufeatures
crunchy
crypto_mac
curve25519_dalek
derivative
digest
either
enumflags2
enumflags2_derive
env_logger
generic_array
getrandom
hashbrown
hex
hmac
hmac_drbg
humantime
itertools
keccak
lazy_static
libc
libsecp256k1
libsecp256k1_core
log
memchr
memmap2
num_derive
num_enum
num_enum_derive
num_traits
opaque_debug
ppv_lite86
proc_macro2
quote
rand
rand_chacha
rand_core
rand_pcg
regex
regex_syntax
rustversion
serde
serde_bytes
serde_derive
sha2
sha3
solana_frozen_abi
solana_frozen_abi_macro
solana_logger
solana_program
solana_sdk_macro
spin
spl_token
subtle
syn
synstructure
termcolor
thiserror
thiserror_impl
typenum
unicode_xid
zeroize
zeroize_derive
  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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Implementations of commonly used traits like
//! [`digest::Digest`](https://crates.io/crates/digest) and
//! [`crypto_mac::Mac`](https://crates.io/crates/crypto-mac).

pub use crypto_mac;
pub use digest;

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

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

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

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

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

    #[inline]
    fn finalize_into(self, out: &mut GenericArray<u8, Self::OutputSize>) {
        out.copy_from_slice(self.finalize().as_bytes());
    }

    #[inline]
    fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>) {
        out.copy_from_slice(self.finalize().as_bytes());
        self.reset();
    }
}

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

    #[inline]
    fn finalize_xof(self) -> Self::Reader {
        Hasher::finalize_xof(&self)
    }

    #[inline]
    fn finalize_xof_reset(&mut self) -> Self::Reader {
        let reader = Hasher::finalize_xof(self);
        self.reset();
        reader
    }
}

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

impl crypto_mac::NewMac for Hasher {
    type KeySize = U32;

    #[inline]
    fn new(key: &crypto_mac::Key<Self>) -> Self {
        let key_bytes: [u8; 32] = (*key).into();
        Hasher::new_keyed(&key_bytes)
    }
}

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

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

    #[inline]
    fn reset(&mut self) {
        self.reset();
    }

    #[inline]
    fn finalize(self) -> crypto_mac::Output<Self> {
        crypto_mac::Output::new(digest::Digest::finalize(self))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[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::update(&mut hasher2, b"xxx");
        digest::Digest::reset(&mut hasher2);
        digest::Digest::update(&mut hasher2, b"foo");
        digest::Digest::update(&mut hasher2, b"bar");
        digest::Digest::update(&mut hasher2, b"baz");
        let out2 = digest::Digest::finalize(hasher2.clone());
        let mut xof2 = [0; 301];
        digest::XofReader::read(
            &mut digest::ExtendableOutput::finalize_xof(hasher2.clone()),
            &mut xof2,
        );
        assert_eq!(out1.as_bytes(), &out2[..]);
        assert_eq!(xof1[..], xof2[..]);

        // Again with the resetting variants.
        let mut hasher3: crate::Hasher = digest::Digest::new();
        digest::Digest::update(&mut hasher3, b"foobarbaz");
        let mut out3 = [0; 32];
        digest::FixedOutput::finalize_into_reset(
            &mut hasher3,
            GenericArray::from_mut_slice(&mut out3),
        );
        digest::Digest::update(&mut hasher3, b"foobarbaz");
        let mut out4 = [0; 32];
        digest::FixedOutput::finalize_into_reset(
            &mut hasher3,
            GenericArray::from_mut_slice(&mut out4),
        );
        digest::Digest::update(&mut hasher3, b"foobarbaz");
        let mut xof3 = [0; 301];
        digest::XofReader::read(
            &mut digest::ExtendableOutput::finalize_xof_reset(&mut hasher3),
            &mut xof3,
        );
        digest::Digest::update(&mut hasher3, b"foobarbaz");
        let mut xof4 = [0; 301];
        digest::XofReader::read(
            &mut digest::ExtendableOutput::finalize_xof_reset(&mut hasher3),
            &mut xof4,
        );
        assert_eq!(out1.as_bytes(), &out3[..]);
        assert_eq!(out1.as_bytes(), &out4[..]);
        assert_eq!(xof1[..], xof3[..]);
        assert_eq!(xof1[..], xof4[..]);
    }

    #[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::NewMac::new(&generic_key);
        crypto_mac::Mac::update(&mut hasher2, b"xxx");
        crypto_mac::Mac::reset(&mut hasher2);
        crypto_mac::Mac::update(&mut hasher2, b"foo");
        crypto_mac::Mac::update(&mut hasher2, b"bar");
        crypto_mac::Mac::update(&mut hasher2, b"baz");
        let out2 = crypto_mac::Mac::finalize(hasher2);
        assert_eq!(out1.as_bytes(), out2.into_bytes().as_slice());
    }
}