github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/crypto/hash.h (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 #pragma once 21 #include "common/byte.h" 22 #include "common/common.h" 23 #include <cryptopp/sha3.h> 24 25 namespace neb { 26 namespace crypto { 27 typedef fix_bytes<32> sha3_256_hash_t; 28 29 sha3_256_hash_t sha3_256_hash(const bytes &b); 30 31 namespace internal { 32 template <typename T, bool is_arith = std::is_arithmetic<T>::value> 33 struct sha3_256_hash_helper {}; 34 template <typename T> struct sha3_256_hash_helper<T, true> { 35 static sha3_256_hash_t get_hash(const T &t) { 36 bytes b(sizeof(t)); 37 number_to_byte(t, b, sizeof(t)); 38 return sha3_256_hash(b); 39 } 40 }; 41 42 template <> struct sha3_256_hash_helper<std::string, false> { 43 static sha3_256_hash_t get_hash(const std::string &t) { 44 bytes b = string_to_byte(t); 45 return sha3_256_hash(b); 46 } 47 }; 48 49 } // namespace internal 50 template <typename T> sha3_256_hash_t sha3_256_hash(const T &v) { 51 return internal::sha3_256_hash_helper<T>::get_hash(v); 52 }; 53 } 54 typedef crypto::sha3_256_hash_t hash_t; 55 } // namespace neb