mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-19 21:41:02 +00:00
Add sha2-256 hash function (#4218)
* Add sha2-256 hash function Widely used hash function, supported by bitcoin and ethereum * Add runtime io support * add test * add test * Update hashing.rs * Update hashing.rs
This commit is contained in:
@@ -10,7 +10,7 @@ use rstd::{vec::Vec, vec};
|
|||||||
|
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
use runtime_io::{
|
use runtime_io::{
|
||||||
storage, hashing::{blake2_128, blake2_256, twox_128, twox_256},
|
storage, hashing::{blake2_128, blake2_256, sha2_256, twox_128, twox_256},
|
||||||
crypto::{ed25519_verify, sr25519_verify},
|
crypto::{ed25519_verify, sr25519_verify},
|
||||||
};
|
};
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
@@ -60,6 +60,10 @@ primitives::wasm_export_functions! {
|
|||||||
blake2_128(&input).to_vec()
|
blake2_128(&input).to_vec()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_sha2_256(input: Vec<u8>) -> Vec<u8> {
|
||||||
|
sha2_256(&input).to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
fn test_twox_256(input: Vec<u8>) -> Vec<u8> {
|
fn test_twox_256(input: Vec<u8>) -> Vec<u8> {
|
||||||
twox_256(&input).to_vec()
|
twox_256(&input).to_vec()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -230,6 +230,42 @@ fn blake2_128_should_work(wasm_method: WasmExecutionMethod) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
||||||
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
||||||
|
fn sha2_256_should_work(wasm_method: WasmExecutionMethod) {
|
||||||
|
let mut ext = TestExternalities::default();
|
||||||
|
let mut ext = ext.ext();
|
||||||
|
let test_code = WASM_BINARY;
|
||||||
|
assert_eq!(
|
||||||
|
call_in_wasm(
|
||||||
|
"test_sha2_256",
|
||||||
|
&[0],
|
||||||
|
wasm_method,
|
||||||
|
&mut ext,
|
||||||
|
&test_code[..],
|
||||||
|
8,
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
hex!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
||||||
|
.to_vec()
|
||||||
|
.encode(),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
call_in_wasm(
|
||||||
|
"test_sha2_256",
|
||||||
|
&b"Hello world!".to_vec().encode(),
|
||||||
|
wasm_method,
|
||||||
|
&mut ext,
|
||||||
|
&test_code[..],
|
||||||
|
8,
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
hex!("c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a")
|
||||||
|
.to_vec()
|
||||||
|
.encode(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test_case(WasmExecutionMethod::Interpreted)]
|
#[test_case(WasmExecutionMethod::Interpreted)]
|
||||||
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
||||||
fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
|
fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
//! Hashing functions.
|
//! Hashing functions.
|
||||||
|
|
||||||
use blake2_rfc;
|
use blake2_rfc;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
use tiny_keccak::{Hasher, Keccak};
|
use tiny_keccak::{Hasher, Keccak};
|
||||||
use twox_hash;
|
use twox_hash;
|
||||||
|
|
||||||
@@ -123,7 +124,7 @@ pub fn twox_256(data: &[u8]) -> [u8; 32] {
|
|||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Do a keccak 256 hash and return result.
|
/// Do a keccak 256-bit hash and return result.
|
||||||
pub fn keccak_256(data: &[u8]) -> [u8; 32] {
|
pub fn keccak_256(data: &[u8]) -> [u8; 32] {
|
||||||
let mut keccak = Keccak::v256();
|
let mut keccak = Keccak::v256();
|
||||||
keccak.update(data);
|
keccak.update(data);
|
||||||
@@ -131,3 +132,12 @@ pub fn keccak_256(data: &[u8]) -> [u8; 32] {
|
|||||||
keccak.finalize(&mut output);
|
keccak.finalize(&mut output);
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Do a sha2 256-bit hash and return result.
|
||||||
|
pub fn sha2_256(data: &[u8]) -> [u8; 32] {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.input(data);
|
||||||
|
let mut output = [0u8; 32];
|
||||||
|
output.copy_from_slice(&hasher.result());
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|||||||
@@ -384,6 +384,11 @@ pub trait Hashing {
|
|||||||
primitives::hashing::keccak_256(data)
|
primitives::hashing::keccak_256(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Conduct a 256-bit Sha2 hash.
|
||||||
|
fn sha2_256(data: &[u8]) -> [u8; 32] {
|
||||||
|
primitives::hashing::sha2_256(data)
|
||||||
|
}
|
||||||
|
|
||||||
/// Conduct a 128-bit Blake2 hash.
|
/// Conduct a 128-bit Blake2 hash.
|
||||||
fn blake2_128(data: &[u8]) -> [u8; 16] {
|
fn blake2_128(data: &[u8]) -> [u8; 16] {
|
||||||
primitives::hashing::blake2_128(data)
|
primitives::hashing::blake2_128(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user