Line data Source code
1 : /* 2 : This file is part of cpp-ethereum. 3 : 4 : cpp-ethereum is free software: you can redistribute it and/or modify 5 : it under the terms of the GNU General Public License as published by 6 : the Free Software Foundation, either version 3 of the License, or 7 : (at your option) any later version. 8 : 9 : cpp-ethereum is distributed in the hope that it will be useful, 10 : but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : GNU General Public License for more details. 13 : 14 : You should have received a copy of the GNU General Public License 15 : along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : /** @file SHA3.h 18 : * @author Gav Wood <i@gavwood.com> 19 : * @date 2014 20 : * 21 : * The FixedHash fixed-size "hash" container type. 22 : */ 23 : 24 : #pragma once 25 : 26 : #include <string> 27 : #include "FixedHash.h" 28 : #include "vector_ref.h" 29 : 30 : namespace dev { 31 : 32 : // SHA-3 convenience routines. 33 : 34 : /// Calculate SHA3-256 hash of the given input and load it into the given output. 35 : /// @returns false if o_output.size() != 32. 36 : bool sha3(bytesConstRef _input, bytesRef o_output); 37 : 38 : /// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash. 39 : inline h256 40 1567 : sha3(bytesConstRef _input) 41 : { 42 1567 : h256 ret; 43 1567 : sha3(_input, ret.ref()); 44 1567 : return ret; 45 : } 46 : 47 : /// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash. 48 : inline h256 49 : sha3(bytes const& _input) 50 : { 51 : return sha3(bytesConstRef(&_input)); 52 : } 53 : 54 : /// Calculate SHA3-256 hash of the given input (presented as a binary-filled string), returning as a 55 : /// 256-bit hash. 56 : inline h256 57 : sha3(std::string const& _input) 58 : { 59 : return sha3(bytesConstRef(_input)); 60 : } 61 : 62 : /// Calculate SHA3-256 hash of the given input (presented as a FixedHash), returns a 256-bit hash. 63 : template<unsigned N> 64 : inline h256 65 : sha3(FixedHash<N> const& _input) 66 : { 67 : return sha3(_input.ref()); 68 : } 69 : 70 : /// Calculate SHA3-256 hash of the given input, possibly interpreting it as nibbles, and return the 71 : /// hash as a string filled with binary data. 72 : inline std::string 73 : sha3(std::string const& _input, bool _isNibbles) 74 : { 75 : return asString((_isNibbles ? sha3(fromHex(_input)) : sha3(bytesConstRef(&_input))).asBytes()); 76 : } 77 : 78 : } // namespace dev