sp_crypto_hashing_proc_macro/lib.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//! Macros to calculate constant hash bytes result.
19//!
20//! Macros from this crate does apply a specific hash function on input.
21//! Input can be literal byte array as `b"content"` or array of bytes
22//! as `[1, 2, 3]`.
23//! Rust identifier can also be use, in this case we use their utf8 string
24//! byte representation, for instance if the ident is `MyStruct`, then
25//! `b"MyStruct"` will be hashed.
26//! If multiple arguments comma separated are passed, they are concatenated
27//! then hashed.
28//!
29//! Examples:
30//!
31//! ```rust
32//! assert_eq!(
33//! sp_crypto_hashing_proc_macro::blake2b_256!(b"test"),
34//! sp_crypto_hashing::blake2_256(b"test"),
35//! );
36//! assert_eq!(
37//! sp_crypto_hashing_proc_macro::blake2b_256!([1u8]),
38//! sp_crypto_hashing::blake2_256(&[1u8]),
39//! );
40//! assert_eq!(
41//! sp_crypto_hashing_proc_macro::blake2b_256!([1, 2, 3]),
42//! sp_crypto_hashing::blake2_256(&[1, 2, 3]),
43//! );
44//! assert_eq!(
45//! sp_crypto_hashing_proc_macro::blake2b_256!(identifier),
46//! sp_crypto_hashing::blake2_256(b"identifier"),
47//! );
48//! assert_eq!(
49//! sp_crypto_hashing_proc_macro::blake2b_256!(identifier, b"/string"),
50//! sp_crypto_hashing::blake2_256(b"identifier/string"),
51//! );
52//! ```
53
54mod impls;
55
56use impls::MultipleInputBytes;
57use proc_macro::TokenStream;
58
59/// Process a Blake2 64-bit hash of bytes parameter outputs a `[u8; 8]`.
60/// Multiple inputs are concatenated before hashing.
61/// Input can be identifier (name of identifier as bytes is used), byte string or
62/// array of bytes.
63#[proc_macro]
64pub fn blake2b_64(input: TokenStream) -> TokenStream {
65 impls::blake2b_64(syn::parse_macro_input!(input as MultipleInputBytes).concatenated())
66}
67
68/// Apply a Blake2 256-bit hash of bytes parameter, outputs a `[u8; 32]`.
69/// Multiple inputs are concatenated before hashing.
70/// Input can be identifier (name of identifier as bytes is used), byte string or
71/// array of bytes.
72#[proc_macro]
73pub fn blake2b_256(input: TokenStream) -> TokenStream {
74 impls::blake2b_256(syn::parse_macro_input!(input as MultipleInputBytes).concatenated())
75}
76
77/// Apply a Blake2 512-bit hash of bytes parameter, outputs a `[u8; 64]`.
78/// Multiple inputs are concatenated before hashing.
79/// Input can be identifier (name of identifier as bytes is used), byte string or
80/// array of bytes.
81#[proc_macro]
82pub fn blake2b_512(input: TokenStream) -> TokenStream {
83 impls::blake2b_512(syn::parse_macro_input!(input as MultipleInputBytes).concatenated())
84}
85
86/// Apply a XX 64-bit hash on its bytes parameter, outputs a `[u8; 8]`.
87/// Multiple inputs are concatenated before hashing.
88/// Input can be identifier (name of identifier as bytes is used), byte string or
89/// array of bytes.
90#[proc_macro]
91pub fn twox_64(input: TokenStream) -> TokenStream {
92 impls::twox_64(syn::parse_macro_input!(input as MultipleInputBytes).concatenated())
93}
94
95/// Apply a XX 128-bit hash on its bytes parameter, outputs a `[u8; 16]`.
96/// Multiple inputs are concatenated before hashing.
97/// Input can be identifier (name of identifier as bytes is used), byte string or
98/// array of bytes.
99#[proc_macro]
100pub fn twox_128(input: TokenStream) -> TokenStream {
101 impls::twox_128(syn::parse_macro_input!(input as MultipleInputBytes).concatenated())
102}
103
104/// Apply a keccak 256-bit hash on its bytes parameter, outputs a `[u8; 32]`.
105/// Multiple inputs are concatenated before hashing.
106/// Input can be identifier (name of identifier as bytes is used), byte string or
107/// array of bytes.
108#[proc_macro]
109pub fn keccak_256(input: TokenStream) -> TokenStream {
110 impls::keccak_256(syn::parse_macro_input!(input as MultipleInputBytes).concatenated())
111}
112
113/// Apply a keccak 512-bit hash on its bytes parameter, outputs a `[u8; 64]`.
114/// Multiple inputs are concatenated before hashing.
115/// Input can be identifier (name of identifier as bytes is used), byte string or
116/// array of bytes.
117#[proc_macro]
118pub fn keccak_512(input: TokenStream) -> TokenStream {
119 impls::keccak_512(syn::parse_macro_input!(input as MultipleInputBytes).concatenated())
120}
121
122/// Apply a sha2 256-bit hash on its bytes parameter, outputs a `[u8; 32]`.
123/// Multiple inputs are concatenated before hashing.
124/// Input can be identifier (name of identifier as bytes is used), byte string or
125/// array of bytes.
126#[proc_macro]
127pub fn sha2_256(input: TokenStream) -> TokenStream {
128 impls::sha2_256(syn::parse_macro_input!(input as MultipleInputBytes).concatenated())
129}