SHA3.h 3.3 KB

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