ethers-hash-rs 0.1.0

Ethereum wallet implementation and utility tools
Documentation
use sha3::{Digest, Keccak256};

/// Compute the Keccak-256 hash of input bytes.
pub fn keccak256<S>(bytes: S) -> [u8; 32]
where
    S: AsRef<[u8]>,
{
    let mut hasher = Keccak256::new();

    hasher.update(bytes.as_ref());

    hasher.finalize().into()
}

#[cfg(feature = "rust_crypto")]
pub mod pbkdf2 {
    use digest::{
        block_buffer::Eager,
        core_api::{BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore, UpdateCore},
        generic_array::typenum::{IsLess, Le, NonZero, U256},
        HashMarker,
    };

    pub use ::pbkdf2::*;

    pub fn pbkdf2_hmac<D>(password: &[u8], salt: &[u8], rounds: u32, res: &mut [u8])
    where
        D: CoreProxy,
        D::Core: Sync
            + HashMarker
            + UpdateCore
            + FixedOutputCore
            + BufferKindUser<BufferKind = Eager>
            + Default
            + Clone,
        <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
        Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
    {
        pbkdf2::pbkdf2::<hmac::Hmac<D>>(password, salt, rounds, res);
    }

    pub fn pbkdf2_hmac_array<D, const N: usize>(
        password: &[u8],
        salt: &[u8],
        rounds: u32,
    ) -> [u8; N]
    where
        D: CoreProxy,
        D::Core: Sync
            + HashMarker
            + UpdateCore
            + FixedOutputCore
            + BufferKindUser<BufferKind = Eager>
            + Default
            + Clone,
        <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
        Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
    {
        let mut buf = [0u8; N];
        pbkdf2_hmac::<D>(password, salt, rounds, &mut buf);
        buf
    }
}