#![allow(
clippy::unreadable_literal,
clippy::upper_case_acronyms,
dead_code,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
overflowing_literals,
unused_variables,
unused_assignments
)]
use lazy_static::lazy_static;
use crate::bcossdk::bcosclientconfig::BcosCryptoKind;
use keccak_hash::{keccak, H256};
use wedpr_l_crypto_hash_keccak256::WedprKeccak256;
use wedpr_l_crypto_hash_sm3::WedprSm3;
use wedpr_l_utils::traits::Hash;
#[derive(Debug, Clone, PartialEq)]
pub enum HashType {
WEDPR_KECCAK,
WEDRP_SM3,
KECCAK,
Unknow, }
lazy_static! {
static ref WEDPR_SM3: WedprSm3 = WedprSm3::default();
static ref WEDPR_KECCAK256: WedprKeccak256 = WedprKeccak256::default();
}
#[derive(Debug, Clone, PartialEq)]
pub struct CommonHash {
hashtype: HashType,
}
impl CommonHash {
pub fn hash(data: &Vec<u8>, hashtype: &HashType) -> Vec<u8> {
printlnex!("Using HashType {:?}", hashtype);
match hashtype {
HashType::WEDPR_KECCAK => {
let msg_hash = WEDPR_KECCAK256.hash(data.as_slice());
msg_hash
}
HashType::WEDRP_SM3 => {
let msg_hash = WEDPR_SM3.hash(data.as_slice());
msg_hash
}
HashType::KECCAK => {
let keccakhash = keccak(&data);
Vec::from(keccakhash.as_bytes())
}
HashType::Unknow => {
vec![]
}
}
}
pub fn hash_to_h256(data: &Vec<u8>, hashtype: &HashType) -> H256 {
let hash = CommonHash::hash(data, hashtype);
let h256 = H256::from_slice(&hash.as_slice());
h256
}
pub fn crypto_to_hashtype(crypto: &BcosCryptoKind) -> HashType {
match crypto {
BcosCryptoKind::ECDSA => {
return HashType::WEDPR_KECCAK;
}
BcosCryptoKind::GM => {
return HashType::WEDRP_SM3;
}
}
}
}