LCOV - code coverage report
Current view: top level - src/jamidht/eth/libdevcore - SHA3.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 4 15 26.7 %
Date: 2021-07-24 07:34:39 Functions: 1 5 20.0 %

          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         652 : sha3(bytesConstRef _input)
      41             : {
      42         652 :     h256 ret;
      43         652 :     sha3(_input, ret.ref());
      44         652 :     return ret;
      45             : }
      46             : inline SecureFixedHash<32>
      47           0 : sha3Secure(bytesConstRef _input)
      48             : {
      49           0 :     SecureFixedHash<32> ret;
      50           0 :     sha3(_input, ret.writable().ref());
      51           0 :     return ret;
      52             : }
      53             : 
      54             : /// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash.
      55             : inline h256
      56           0 : sha3(bytes const& _input)
      57             : {
      58           0 :     return sha3(bytesConstRef(&_input));
      59             : }
      60             : inline SecureFixedHash<32>
      61             : sha3Secure(bytes const& _input)
      62             : {
      63             :     return sha3Secure(bytesConstRef(&_input));
      64             : }
      65             : 
      66             : /// Calculate SHA3-256 hash of the given input (presented as a binary-filled string), returning as a
      67             : /// 256-bit hash.
      68             : inline h256
      69             : sha3(std::string const& _input)
      70             : {
      71             :     return sha3(bytesConstRef(_input));
      72             : }
      73             : inline SecureFixedHash<32>
      74             : sha3Secure(std::string const& _input)
      75             : {
      76             :     return sha3Secure(bytesConstRef(_input));
      77             : }
      78             : 
      79             : /// Calculate SHA3-256 hash of the given input (presented as a FixedHash), returns a 256-bit hash.
      80             : template<unsigned N>
      81             : inline h256
      82             : sha3(FixedHash<N> const& _input)
      83             : {
      84             :     return sha3(_input.ref());
      85             : }
      86             : template<unsigned N>
      87             : inline SecureFixedHash<32>
      88             : sha3Secure(FixedHash<N> const& _input)
      89             : {
      90             :     return sha3Secure(_input.ref());
      91             : }
      92             : 
      93             : /// Fully secure variants are equivalent for sha3 and sha3Secure.
      94             : inline SecureFixedHash<32>
      95             : sha3(bytesSec const& _input)
      96             : {
      97             :     return sha3Secure(_input.ref());
      98             : }
      99             : inline SecureFixedHash<32>
     100             : sha3Secure(bytesSec const& _input)
     101             : {
     102             :     return sha3Secure(_input.ref());
     103             : }
     104             : template<unsigned N>
     105             : inline SecureFixedHash<32>
     106           0 : sha3(SecureFixedHash<N> const& _input)
     107             : {
     108           0 :     return sha3Secure(_input.ref());
     109             : }
     110             : template<unsigned N>
     111             : inline SecureFixedHash<32>
     112             : sha3Secure(SecureFixedHash<N> const& _input)
     113             : {
     114             :     return sha3Secure(_input.ref());
     115             : }
     116             : 
     117             : /// Calculate SHA3-256 hash of the given input, possibly interpreting it as nibbles, and return the
     118             : /// hash as a string filled with binary data.
     119             : inline std::string
     120             : sha3(std::string const& _input, bool _isNibbles)
     121             : {
     122             :     return asString((_isNibbles ? sha3(fromHex(_input)) : sha3(bytesConstRef(&_input))).asBytes());
     123             : }
     124             : 
     125             : /// Calculate SHA3-256 MAC
     126             : inline void
     127           0 : sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output)
     128             : {
     129           0 :     sha3(_secret.toBytes() + _plain.toBytes()).ref().populate(_output);
     130           0 : }
     131             : 
     132             : extern h256 EmptySHA3;
     133             : 
     134             : } // namespace dev

Generated by: LCOV version 1.14