tet_core/
hashing.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2017-2021 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//! Hashing functions.
19//!
20//! This module is gated by `full-crypto` feature. If you intend to use any of the functions
21//! defined here within your runtime, you should most likely rather use [tet_io::hashing] instead,
22//! unless you know what you're doing. Using `tet_io` will be more performant, since instead of
23//! computing the hash in WASM it delegates that computation to the host client.
24
25use blake2_rfc;
26use sha2::{Digest, Sha256};
27use tiny_keccak::{Hasher, Keccak};
28use twox_hash;
29
30/// Do a Blake2 512-bit hash and place result in `dest`.
31pub fn blake2_512_into(data: &[u8], dest: &mut [u8; 64]) {
32	dest.copy_from_slice(blake2_rfc::blake2b::blake2b(64, &[], data).as_bytes());
33}
34
35/// Do a Blake2 512-bit hash and return result.
36pub fn blake2_512(data: &[u8]) -> [u8; 64] {
37	let mut r = [0; 64];
38	blake2_512_into(data, &mut r);
39	r
40}
41
42/// Do a Blake2 256-bit hash and place result in `dest`.
43pub fn blake2_256_into(data: &[u8], dest: &mut [u8; 32]) {
44	dest.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], data).as_bytes());
45}
46
47/// Do a Blake2 256-bit hash and return result.
48pub fn blake2_256(data: &[u8]) -> [u8; 32] {
49	let mut r = [0; 32];
50	blake2_256_into(data, &mut r);
51	r
52}
53
54/// Do a Blake2 128-bit hash and place result in `dest`.
55pub fn blake2_128_into(data: &[u8], dest: &mut [u8; 16]) {
56	dest.copy_from_slice(blake2_rfc::blake2b::blake2b(16, &[], data).as_bytes());
57}
58
59/// Do a Blake2 128-bit hash and return result.
60pub fn blake2_128(data: &[u8]) -> [u8; 16] {
61	let mut r = [0; 16];
62	blake2_128_into(data, &mut r);
63	r
64}
65
66/// Do a Blake2 64-bit hash and place result in `dest`.
67pub fn blake2_64_into(data: &[u8], dest: &mut [u8; 8]) {
68	dest.copy_from_slice(blake2_rfc::blake2b::blake2b(8, &[], data).as_bytes());
69}
70
71/// Do a Blake2 64-bit hash and return result.
72pub fn blake2_64(data: &[u8]) -> [u8; 8] {
73	let mut r = [0; 8];
74	blake2_64_into(data, &mut r);
75	r
76}
77
78/// Do a XX 64-bit hash and place result in `dest`.
79pub fn twox_64_into(data: &[u8], dest: &mut [u8; 8]) {
80	use core::hash::Hasher;
81	let mut h0 = twox_hash::XxHash::with_seed(0);
82	h0.write(data);
83	let r0 = h0.finish();
84	use byteorder::{ByteOrder, LittleEndian};
85	LittleEndian::write_u64(&mut dest[0..8], r0);
86}
87
88/// Do a XX 64-bit hash and return result.
89pub fn twox_64(data: &[u8]) -> [u8; 8] {
90	let mut r: [u8; 8] = [0; 8];
91	twox_64_into(data, &mut r);
92	r
93}
94
95/// Do a XX 128-bit hash and place result in `dest`.
96pub fn twox_128_into(data: &[u8], dest: &mut [u8; 16]) {
97	use core::hash::Hasher;
98	let mut h0 = twox_hash::XxHash::with_seed(0);
99	let mut h1 = twox_hash::XxHash::with_seed(1);
100	h0.write(data);
101	h1.write(data);
102	let r0 = h0.finish();
103	let r1 = h1.finish();
104	use byteorder::{ByteOrder, LittleEndian};
105	LittleEndian::write_u64(&mut dest[0..8], r0);
106	LittleEndian::write_u64(&mut dest[8..16], r1);
107}
108
109/// Do a XX 128-bit hash and return result.
110pub fn twox_128(data: &[u8]) -> [u8; 16] {
111	let mut r: [u8; 16] = [0; 16];
112	twox_128_into(data, &mut r);
113	r
114}
115
116/// Do a XX 256-bit hash and place result in `dest`.
117pub fn twox_256_into(data: &[u8], dest: &mut [u8; 32]) {
118	use ::core::hash::Hasher;
119	use byteorder::{ByteOrder, LittleEndian};
120	let mut h0 = twox_hash::XxHash::with_seed(0);
121	let mut h1 = twox_hash::XxHash::with_seed(1);
122	let mut h2 = twox_hash::XxHash::with_seed(2);
123	let mut h3 = twox_hash::XxHash::with_seed(3);
124	h0.write(data);
125	h1.write(data);
126	h2.write(data);
127	h3.write(data);
128	let r0 = h0.finish();
129	let r1 = h1.finish();
130	let r2 = h2.finish();
131	let r3 = h3.finish();
132	LittleEndian::write_u64(&mut dest[0..8], r0);
133	LittleEndian::write_u64(&mut dest[8..16], r1);
134	LittleEndian::write_u64(&mut dest[16..24], r2);
135	LittleEndian::write_u64(&mut dest[24..32], r3);
136}
137
138/// Do a XX 256-bit hash and return result.
139pub fn twox_256(data: &[u8]) -> [u8; 32] {
140	let mut r: [u8; 32] = [0; 32];
141	twox_256_into(data, &mut r);
142	r
143}
144
145/// Do a keccak 256-bit hash and return result.
146pub fn keccak_256(data: &[u8]) -> [u8; 32] {
147	let mut keccak = Keccak::v256();
148	keccak.update(data);
149	let mut output = [0u8; 32];
150	keccak.finalize(&mut output);
151	output
152}
153
154/// Do a keccak 512-bit hash and return result.
155pub fn keccak_512(data: &[u8]) -> [u8; 64] {
156	let mut keccak = Keccak::v512();
157	keccak.update(data);
158	let mut output = [0u8; 64];
159	keccak.finalize(&mut output);
160	output
161}
162
163/// Do a sha2 256-bit hash and return result.
164pub fn sha2_256(data: &[u8]) -> [u8; 32] {
165	let mut hasher = Sha256::new();
166	hasher.update(data);
167	let mut output = [0u8; 32];
168	output.copy_from_slice(&hasher.finalize());
169	output
170}