Unverified Commit 13eb43e2 authored by Hero Bird's avatar Hero Bird Committed by GitHub

[core] use official Rust crypto crates (#443)

parent 2e584d23
Pipeline #96059 passed with stages
in 14 minutes and 39 seconds
......@@ -30,9 +30,9 @@ generic-array = "0.14.1"
paste = "0.1"
# Hashes for the off-chain environment.
blake2-rfc = { version = "0.2", optional = true }
sha2 = { version = "0.8", optional = true }
tiny-keccak = { version = "2.0", optional = true }
sha3 = { version = "0.8", optional = true }
blake2 = { version = "0.8", optional = true }
# Only used in the off-chain environment.
#
......@@ -63,9 +63,9 @@ std = [
"rand/std",
"num-traits/std",
# Enables hashing crates for off-chain environment.
"blake2-rfc",
"sha2",
"tiny-keccak",
"sha3",
"blake2",
]
ink-generate-abi = [
"ink_abi",
......
......@@ -14,34 +14,39 @@
//! Implementations of supported cryptographic hash functions.
/// Helper routine implementing variable size BLAKE2b hash computation.
fn blake2b_var(size: usize, input: &[u8], output: &mut [u8]) {
use blake2::digest::VariableOutput as _;
use std::io::Write as _;
let mut blake2 = blake2::VarBlake2b::new_keyed(&[], size);
blake2
.write_all(input)
.expect("failed to compute BLAKE2b hash of input");
output.copy_from_slice(blake2.vec_result().as_slice());
}
/// Conduct the BLAKE2 256-bit hash and place the result into `output`.
pub fn blake2_256(input: &[u8], output: &mut [u8; 32]) {
output.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], input).as_bytes());
pub fn blake2b_256(input: &[u8], output: &mut [u8; 32]) {
blake2b_var(32, input, output)
}
/// Conduct the BLAKE2 128-bit hash and place the result into `output`.
pub fn blake2_128(input: &[u8], output: &mut [u8; 16]) {
output.copy_from_slice(blake2_rfc::blake2b::blake2b(16, &[], input).as_bytes());
pub fn blake2b_128(input: &[u8], output: &mut [u8; 16]) {
blake2b_var(16, input, output)
}
/// Conduct the KECCAK 256-bit hash and place the result into `output`.
pub fn keccak_256(input: &[u8], output: &mut [u8; 32]) {
use ::tiny_keccak::{
Hasher,
Keccak,
};
let mut keccak = Keccak::v256();
keccak.update(input);
keccak.finalize(output)
use ::sha2::Digest as _;
let mut hasher = ::sha3::Keccak256::new();
hasher.input(input);
output.copy_from_slice(hasher.result().as_slice());
}
/// Conduct the SHA2 256-bit hash and place the result into `output`.
pub fn sha2_256(input: &[u8], output: &mut [u8; 32]) {
use ::sha2::{
Digest,
Sha256,
};
let mut hasher = Sha256::new();
use ::sha2::Digest as _;
let mut hasher = ::sha2::Sha256::new();
hasher.input(input);
output.copy_from_slice(&hasher.result());
output.copy_from_slice(hasher.result().as_slice());
}
......@@ -118,11 +118,11 @@ impl Env for EnvInstance {
}
fn hash_blake2_256(input: &[u8], output: &mut [u8; 32]) {
hashing::blake2_256(input, output)
hashing::blake2b_256(input, output)
}
fn hash_blake2_128(input: &[u8], output: &mut [u8; 16]) {
hashing::blake2_128(input, output)
hashing::blake2b_128(input, output)
}
fn hash_sha2_256(input: &[u8], output: &mut [u8; 32]) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment