diff --git a/substrate/client/executor/runtime-test/src/lib.rs b/substrate/client/executor/runtime-test/src/lib.rs index 16c6c353d9..63f89fca57 100644 --- a/substrate/client/executor/runtime-test/src/lib.rs +++ b/substrate/client/executor/runtime-test/src/lib.rs @@ -10,7 +10,7 @@ use rstd::{vec::Vec, vec}; #[cfg(not(feature = "std"))] 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}, }; #[cfg(not(feature = "std"))] @@ -60,6 +60,10 @@ primitives::wasm_export_functions! { blake2_128(&input).to_vec() } + fn test_sha2_256(input: Vec) -> Vec { + sha2_256(&input).to_vec() + } + fn test_twox_256(input: Vec) -> Vec { twox_256(&input).to_vec() } diff --git a/substrate/client/executor/src/integration_tests/mod.rs b/substrate/client/executor/src/integration_tests/mod.rs index 32424c6065..fce39bb400 100644 --- a/substrate/client/executor/src/integration_tests/mod.rs +++ b/substrate/client/executor/src/integration_tests/mod.rs @@ -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)] #[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))] fn twox_256_should_work(wasm_method: WasmExecutionMethod) { diff --git a/substrate/primitives/core/src/hashing.rs b/substrate/primitives/core/src/hashing.rs index c3f7ddf9f5..8fce710230 100644 --- a/substrate/primitives/core/src/hashing.rs +++ b/substrate/primitives/core/src/hashing.rs @@ -17,6 +17,7 @@ //! Hashing functions. use blake2_rfc; +use sha2::{Digest, Sha256}; use tiny_keccak::{Hasher, Keccak}; use twox_hash; @@ -123,7 +124,7 @@ pub fn twox_256(data: &[u8]) -> [u8; 32] { 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] { let mut keccak = Keccak::v256(); keccak.update(data); @@ -131,3 +132,12 @@ pub fn keccak_256(data: &[u8]) -> [u8; 32] { keccak.finalize(&mut 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 +} diff --git a/substrate/primitives/sr-io/src/lib.rs b/substrate/primitives/sr-io/src/lib.rs index 4ceabb4511..589b6a1dd1 100644 --- a/substrate/primitives/sr-io/src/lib.rs +++ b/substrate/primitives/sr-io/src/lib.rs @@ -384,6 +384,11 @@ pub trait Hashing { 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. fn blake2_128(data: &[u8]) -> [u8; 16] { primitives::hashing::blake2_128(data)