use std::fmt;
use sha2::Digest;
use crate::{__impl_index, __impl_u8_array, priv_util::ToHexString};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Hash160([u8; 20]);
impl Hash160 {
pub fn new(value: [u8; 20]) -> Self {
Self(value)
}
}
__impl_u8_array!(Hash160);
__impl_index!(Hash160, 0, u8);
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Hash256([u8; 32]);
impl Hash256 {
pub fn new(value: [u8; 32]) -> Self {
Self(value)
}
}
__impl_u8_array!(Hash256);
__impl_index!(Hash256, 0, u8);
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Hash512(Vec<u8>);
impl Hash512 {
pub fn new(value: [u8; 64]) -> Self {
Self(value.to_vec())
}
}
__impl_u8_array!(Hash512);
__impl_index!(Hash512, 0, u8);
pub fn sha256(input: impl AsRef<[u8]>) -> Hash256 {
let mut hasher = sha2::Sha256::new();
hasher.update(input);
Hash256(hasher.finalize().into())
}
pub fn double_sha256(input: impl AsRef<[u8]>) -> Hash256 {
let mut hasher = sha2::Sha256::new();
hasher.update(input);
let hash1 = hasher.finalize_reset();
hasher.update(hash1);
Hash256(hasher.finalize().into())
}
pub fn sha512(input: impl AsRef<[u8]>) -> Hash512 {
let mut hasher = sha2::Sha512::new();
hasher.update(input);
Hash512(hasher.finalize().to_vec())
}
pub fn double_sha512(input: impl AsRef<[u8]>) -> Hash512 {
let mut hasher = sha2::Sha512::new();
hasher.update(input);
let hash1 = hasher.finalize_reset();
hasher.update(hash1);
Hash512(hasher.finalize().to_vec())
}
pub fn ripemd160(input: impl AsRef<[u8]>) -> Hash160 {
let mut hasher = ripemd::Ripemd160::new();
hasher.update(input);
Hash160(hasher.finalize().into())
}
pub fn ripemd160_sha512(input: impl AsRef<[u8]>) -> Hash160 {
let mut hasher = sha2::Sha512::new();
hasher.update(input);
let hash1 = hasher.finalize();
let mut hasher = ripemd::Ripemd160::new();
hasher.update(hash1);
Hash160(hasher.finalize().into())
}
pub fn sha3_256(input: impl AsRef<[u8]>) -> Hash256 {
let mut hasher = sha3::Sha3_256::new();
hasher.update(input);
Hash256(hasher.finalize().into())
}