ethers_hash_rs/
lib.rs

1use sha3::{Digest, Keccak256};
2
3/// Compute the Keccak-256 hash of input bytes.
4pub fn keccak256<S>(bytes: S) -> [u8; 32]
5where
6    S: AsRef<[u8]>,
7{
8    let mut hasher = Keccak256::new();
9
10    hasher.update(bytes.as_ref());
11
12    hasher.finalize().into()
13}
14
15#[cfg(feature = "rust_crypto")]
16pub mod pbkdf2 {
17    use digest::{
18        block_buffer::Eager,
19        core_api::{BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore, UpdateCore},
20        generic_array::typenum::{IsLess, Le, NonZero, U256},
21        HashMarker,
22    };
23
24    pub use ::pbkdf2::*;
25
26    pub fn pbkdf2_hmac<D>(password: &[u8], salt: &[u8], rounds: u32, res: &mut [u8])
27    where
28        D: CoreProxy,
29        D::Core: Sync
30            + HashMarker
31            + UpdateCore
32            + FixedOutputCore
33            + BufferKindUser<BufferKind = Eager>
34            + Default
35            + Clone,
36        <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
37        Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
38    {
39        pbkdf2::pbkdf2::<hmac::Hmac<D>>(password, salt, rounds, res);
40    }
41
42    pub fn pbkdf2_hmac_array<D, const N: usize>(
43        password: &[u8],
44        salt: &[u8],
45        rounds: u32,
46    ) -> [u8; N]
47    where
48        D: CoreProxy,
49        D::Core: Sync
50            + HashMarker
51            + UpdateCore
52            + FixedOutputCore
53            + BufferKindUser<BufferKind = Eager>
54            + Default
55            + Clone,
56        <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
57        Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
58    {
59        let mut buf = [0u8; N];
60        pbkdf2_hmac::<D>(password, salt, rounds, &mut buf);
61        buf
62    }
63}