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:
Weiliang Li
2019-11-28 02:32:35 +09:00
committed by Gavin Wood
parent f8bf17dc49
commit dcaabbaacf
4 changed files with 57 additions and 2 deletions
+11 -1
View File
@@ -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
}