#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
use core::hash::Hasher;
use byteorder::{ByteOrder, LittleEndian};
use digest::Digest;
#[inline(always)]
fn blake2<const N: usize>(data: &[u8]) -> [u8; N] {
blake2b_simd::Params::new()
.hash_length(N)
.hash(data)
.as_bytes()
.try_into()
.expect("slice is always the necessary length")
}
pub fn blake2_512_into(data: &[u8], dest: &mut [u8; 64]) {
*dest = blake2(data);
}
pub fn blake2_512(data: &[u8]) -> [u8; 64] {
blake2(data)
}
pub fn blake2_256(data: &[u8]) -> [u8; 32] {
blake2(data)
}
pub fn blake2_128(data: &[u8]) -> [u8; 16] {
blake2(data)
}
pub fn blake2_64(data: &[u8]) -> [u8; 8] {
blake2(data)
}
pub fn twox_64_into(data: &[u8], dest: &mut [u8; 8]) {
let r0 = twox_hash::XxHash::with_seed(0).chain_update(data).finish();
LittleEndian::write_u64(&mut dest[0..8], r0);
}
pub fn twox_64(data: &[u8]) -> [u8; 8] {
let mut r: [u8; 8] = [0; 8];
twox_64_into(data, &mut r);
r
}
pub fn twox_128_into(data: &[u8], dest: &mut [u8; 16]) {
let r0 = twox_hash::XxHash::with_seed(0).chain_update(data).finish();
let r1 = twox_hash::XxHash::with_seed(1).chain_update(data).finish();
LittleEndian::write_u64(&mut dest[0..8], r0);
LittleEndian::write_u64(&mut dest[8..16], r1);
}
pub fn twox_128(data: &[u8]) -> [u8; 16] {
let mut r: [u8; 16] = [0; 16];
twox_128_into(data, &mut r);
r
}
pub fn twox_256_into(data: &[u8], dest: &mut [u8; 32]) {
let r0 = twox_hash::XxHash::with_seed(0).chain_update(data).finish();
let r1 = twox_hash::XxHash::with_seed(1).chain_update(data).finish();
let r2 = twox_hash::XxHash::with_seed(2).chain_update(data).finish();
let r3 = twox_hash::XxHash::with_seed(3).chain_update(data).finish();
LittleEndian::write_u64(&mut dest[0..8], r0);
LittleEndian::write_u64(&mut dest[8..16], r1);
LittleEndian::write_u64(&mut dest[16..24], r2);
LittleEndian::write_u64(&mut dest[24..32], r3);
}
pub fn twox_256(data: &[u8]) -> [u8; 32] {
let mut r: [u8; 32] = [0; 32];
twox_256_into(data, &mut r);
r
}
pub fn keccak_256(data: &[u8]) -> [u8; 32] {
sha3::Keccak256::digest(data).into()
}
pub fn keccak_512(data: &[u8]) -> [u8; 64] {
sha3::Keccak512::digest(data).into()
}
pub fn sha2_256(data: &[u8]) -> [u8; 32] {
sha2::Sha256::digest(data).into()
}