sp_core/hasher.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Substrate Blake2b Hasher implementation
19
20pub mod blake2 {
21 use crate::hash::H256;
22 use hash256_std_hasher::Hash256StdHasher;
23 use hash_db::Hasher;
24
25 /// Concrete implementation of Hasher using Blake2b 256-bit hashes
26 #[derive(Debug)]
27 pub struct Blake2Hasher;
28
29 impl Hasher for Blake2Hasher {
30 type Out = H256;
31 type StdHasher = Hash256StdHasher;
32 const LENGTH: usize = 32;
33
34 fn hash(x: &[u8]) -> Self::Out {
35 sp_crypto_hashing::blake2_256(x).into()
36 }
37 }
38}
39
40pub mod keccak {
41 use crate::hash::H256;
42 use hash256_std_hasher::Hash256StdHasher;
43 use hash_db::Hasher;
44
45 /// Concrete implementation of Hasher using Keccak 256-bit hashes
46 #[derive(Debug)]
47 pub struct KeccakHasher;
48
49 impl Hasher for KeccakHasher {
50 type Out = H256;
51 type StdHasher = Hash256StdHasher;
52 const LENGTH: usize = 32;
53
54 fn hash(x: &[u8]) -> Self::Out {
55 sp_crypto_hashing::keccak_256(x).into()
56 }
57 }
58}