LCOV - code coverage report
Current view: top level - src - hash.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 28 30 93.3 %
Date: 2015-10-12 22:39:14 Functions: 14 21 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2013 The Bitcoin Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #ifndef BITCOIN_HASH_H
       7             : #define BITCOIN_HASH_H
       8             : 
       9             : #include "crypto/ripemd160.h"
      10             : #include "crypto/sha256.h"
      11             : #include "serialize.h"
      12             : #include "uint256.h"
      13             : #include "version.h"
      14             : 
      15             : #include <vector>
      16             : 
      17             : typedef uint256 ChainCode;
      18             : 
      19             : /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
      20     1089036 : class CHash256 {
      21             : private:
      22             :     CSHA256 sha;
      23             : public:
      24             :     static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
      25             : 
      26     1089034 :     void Finalize(unsigned char hash[OUTPUT_SIZE]) {
      27             :         unsigned char buf[sha.OUTPUT_SIZE];
      28     1089034 :         sha.Finalize(buf);
      29     1089037 :         sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
      30     1089038 :     }
      31             : 
      32             :     CHash256& Write(const unsigned char *data, size_t len) {
      33     9053631 :         sha.Write(data, len);
      34             :         return *this;
      35             :     }
      36             : 
      37             :     CHash256& Reset() {
      38             :         sha.Reset();
      39             :         return *this;
      40             :     }
      41             : };
      42             : 
      43             : /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
      44       36513 : class CHash160 {
      45             : private:
      46             :     CSHA256 sha;
      47             : public:
      48             :     static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
      49             : 
      50       36512 :     void Finalize(unsigned char hash[OUTPUT_SIZE]) {
      51             :         unsigned char buf[sha.OUTPUT_SIZE];
      52       36512 :         sha.Finalize(buf);
      53       36514 :         CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
      54       36511 :     }
      55             : 
      56             :     CHash160& Write(const unsigned char *data, size_t len) {
      57       36512 :         sha.Write(data, len);
      58             :         return *this;
      59             :     }
      60             : 
      61             :     CHash160& Reset() {
      62             :         sha.Reset();
      63             :         return *this;
      64             :     }
      65             : };
      66             : 
      67             : /** Compute the 256-bit hash of an object. */
      68             : template<typename T1>
      69       52100 : inline uint256 Hash(const T1 pbegin, const T1 pend)
      70             : {
      71             :     static const unsigned char pblank[1] = {};
      72             :     uint256 result;
      73      153869 :     CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
      74             :               .Finalize((unsigned char*)&result);
      75       52100 :     return result;
      76             : }
      77             : 
      78             : /** Compute the 256-bit hash of the concatenation of two objects. */
      79             : template<typename T1, typename T2>
      80      253087 : inline uint256 Hash(const T1 p1begin, const T1 p1end,
      81             :                     const T2 p2begin, const T2 p2end) {
      82             :     static const unsigned char pblank[1] = {};
      83             :     uint256 result;
      84      506174 :     CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
      85             :               .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
      86             :               .Finalize((unsigned char*)&result);
      87      253087 :     return result;
      88             : }
      89             : 
      90             : /** Compute the 256-bit hash of the concatenation of three objects. */
      91             : template<typename T1, typename T2, typename T3>
      92             : inline uint256 Hash(const T1 p1begin, const T1 p1end,
      93             :                     const T2 p2begin, const T2 p2end,
      94             :                     const T3 p3begin, const T3 p3end) {
      95             :     static const unsigned char pblank[1] = {};
      96             :     uint256 result;
      97             :     CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
      98             :               .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
      99             :               .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
     100             :               .Finalize((unsigned char*)&result);
     101             :     return result;
     102             : }
     103             : 
     104             : /** Compute the 160-bit hash an object. */
     105             : template<typename T1>
     106       33592 : inline uint160 Hash160(const T1 pbegin, const T1 pend)
     107             : {
     108             :     static unsigned char pblank[1] = {};
     109             :     uint160 result;
     110       67272 :     CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
     111             :               .Finalize((unsigned char*)&result);
     112       33592 :     return result;
     113             : }
     114             : 
     115             : /** Compute the 160-bit hash of a vector. */
     116             : inline uint160 Hash160(const std::vector<unsigned char>& vch)
     117             : {
     118          27 :     return Hash160(vch.begin(), vch.end());
     119             : }
     120             : 
     121             : /** A writer stream (for serialization) that computes a 256-bit hash. */
     122             : class CHashWriter
     123             : {
     124             : private:
     125             :     CHash256 ctx;
     126             : 
     127             : public:
     128             :     int nType;
     129             :     int nVersion;
     130             : 
     131     1564300 :     CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
     132             : 
     133             :     CHashWriter& write(const char *pch, size_t size) {
     134     8491969 :         ctx.Write((const unsigned char*)pch, size);
     135             :         return (*this);
     136             :     }
     137             : 
     138             :     // invalidates the object
     139             :     uint256 GetHash() {
     140             :         uint256 result;
     141      782148 :         ctx.Finalize((unsigned char*)&result);
     142             :         return result;
     143             :     }
     144             : 
     145             :     template<typename T>
     146           0 :     CHashWriter& operator<<(const T& obj) {
     147             :         // Serialize to this stream
     148     1263843 :         ::Serialize(*this, obj, nType, nVersion);
     149           0 :         return (*this);
     150             :     }
     151             : };
     152             : 
     153             : /** Compute the 256-bit hash of an object's serialization. */
     154             : template<typename T>
     155      532689 : uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
     156             : {
     157             :     CHashWriter ss(nType, nVersion);
     158             :     ss << obj;
     159      532689 :     return ss.GetHash();
     160             : }
     161             : 
     162             : unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
     163             : 
     164             : void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
     165             : 
     166             : #endif // BITCOIN_HASH_H

Generated by: LCOV version 1.11