Line data Source code
# 1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : // Copyright (c) 2009-2018 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/common.h>
# 10 : #include <crypto/ripemd160.h>
# 11 : #include <crypto/sha256.h>
# 12 : #include <prevector.h>
# 13 : #include <serialize.h>
# 14 : #include <uint256.h>
# 15 : #include <version.h>
# 16 :
# 17 : #include <vector>
# 18 :
# 19 : typedef uint256 ChainCode;
# 20 :
# 21 : /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
# 22 : class CHash256 {
# 23 : private:
# 24 : CSHA256 sha;
# 25 : public:
# 26 : static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
# 27 :
# 28 4470280 : void Finalize(unsigned char hash[OUTPUT_SIZE]) {
# 29 4470280 : unsigned char buf[CSHA256::OUTPUT_SIZE];
# 30 4470280 : sha.Finalize(buf);
# 31 4470280 : sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
# 32 4470280 : }
# 33 :
# 34 204980357 : CHash256& Write(const unsigned char *data, size_t len) {
# 35 204980357 : sha.Write(data, len);
# 36 204980357 : return *this;
# 37 204980357 : }
# 38 :
# 39 34 : CHash256& Reset() {
# 40 34 : sha.Reset();
# 41 34 : return *this;
# 42 34 : }
# 43 : };
# 44 :
# 45 : /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
# 46 : class CHash160 {
# 47 : private:
# 48 : CSHA256 sha;
# 49 : public:
# 50 : static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
# 51 :
# 52 295043 : void Finalize(unsigned char hash[OUTPUT_SIZE]) {
# 53 295043 : unsigned char buf[CSHA256::OUTPUT_SIZE];
# 54 295043 : sha.Finalize(buf);
# 55 295043 : CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
# 56 295043 : }
# 57 :
# 58 295041 : CHash160& Write(const unsigned char *data, size_t len) {
# 59 295041 : sha.Write(data, len);
# 60 295041 : return *this;
# 61 295041 : }
# 62 :
# 63 0 : CHash160& Reset() {
# 64 0 : sha.Reset();
# 65 0 : return *this;
# 66 0 : }
# 67 : };
# 68 :
# 69 : /** Compute the 256-bit hash of an object. */
# 70 : template<typename T1>
# 71 : inline uint256 Hash(const T1 pbegin, const T1 pend)
# 72 15408 : {
# 73 15408 : static const unsigned char pblank[1] = {};
# 74 15408 : uint256 result;
# 75 15408 : CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
# 76 15408 : .Finalize((unsigned char*)&result);
# 77 15408 : return result;
# 78 15408 : }
# 79 :
# 80 : /** Compute the 256-bit hash of the concatenation of two objects. */
# 81 : template<typename T1, typename T2>
# 82 : inline uint256 Hash(const T1 p1begin, const T1 p1end,
# 83 712666 : const T2 p2begin, const T2 p2end) {
# 84 712666 : static const unsigned char pblank[1] = {};
# 85 712666 : uint256 result;
# 86 712666 : CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
# 87 712666 : .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
# 88 712666 : .Finalize((unsigned char*)&result);
# 89 712666 : return result;
# 90 712666 : }
# 91 :
# 92 : /** Compute the 160-bit hash an object. */
# 93 : template<typename T1>
# 94 : inline uint160 Hash160(const T1 pbegin, const T1 pend)
# 95 105184 : {
# 96 105184 : static unsigned char pblank[1] = {};
# 97 105184 : uint160 result;
# 98 105184 : CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
# 99 105184 : .Finalize((unsigned char*)&result);
# 100 105184 : return result;
# 101 105184 : }
# 102 :
# 103 : /** Compute the 160-bit hash of a vector. */
# 104 : inline uint160 Hash160(const std::vector<unsigned char>& vch)
# 105 0 : {
# 106 0 : return Hash160(vch.begin(), vch.end());
# 107 0 : }
# 108 :
# 109 : /** Compute the 160-bit hash of a vector. */
# 110 : template<unsigned int N>
# 111 : inline uint160 Hash160(const prevector<N, unsigned char>& vch)
# 112 26 : {
# 113 26 : return Hash160(vch.begin(), vch.end());
# 114 26 : }
# 115 :
# 116 : /** A writer stream (for serialization) that computes a 256-bit hash. */
# 117 : class CHashWriter
# 118 : {
# 119 : private:
# 120 : CHash256 ctx;
# 121 :
# 122 : const int nType;
# 123 : int nVersion;
# 124 : public:
# 125 :
# 126 2853808 : CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
# 127 :
# 128 18 : int GetType() const { return nType; }
# 129 1101969 : int GetVersion() const { return nVersion; }
# 130 4 : void SetVersion(int n) { nVersion = n; }
# 131 :
# 132 201736501 : void write(const char *pch, size_t size) {
# 133 201736501 : ctx.Write((const unsigned char*)pch, size);
# 134 201736501 : }
# 135 :
# 136 : // invalidates the object
# 137 1572808 : uint256 GetHash() {
# 138 1572808 : uint256 result;
# 139 1572808 : ctx.Finalize((unsigned char*)&result);
# 140 1572808 : return result;
# 141 1572808 : }
# 142 :
# 143 : /**
# 144 : * Returns the first 64 bits from the resulting hash.
# 145 : */
# 146 1281056 : inline uint64_t GetCheapHash() {
# 147 1281056 : unsigned char result[CHash256::OUTPUT_SIZE];
# 148 1281056 : ctx.Finalize(result);
# 149 1281056 : return ReadLE64(result);
# 150 1281056 : }
# 151 :
# 152 : template<typename T>
# 153 93230267 : CHashWriter& operator<<(const T& obj) {
# 154 93230267 : // Serialize to this stream
# 155 93230267 : ::Serialize(*this, obj);
# 156 93230267 : return (*this);
# 157 93230267 : }
# 158 : };
# 159 :
# 160 : /** Reads data from an underlying stream, while hashing the read data. */
# 161 : template<typename Source>
# 162 : class CHashVerifier : public CHashWriter
# 163 : {
# 164 : private:
# 165 : Source* source;
# 166 :
# 167 : public:
# 168 884 : explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
# 169 :
# 170 : void read(char* pch, size_t nSize)
# 171 3078 : {
# 172 3078 : source->read(pch, nSize);
# 173 3078 : this->write(pch, nSize);
# 174 3078 : }
# 175 :
# 176 : void ignore(size_t nSize)
# 177 0 : {
# 178 0 : char data[1024];
# 179 0 : while (nSize > 0) {
# 180 0 : size_t now = std::min<size_t>(nSize, 1024);
# 181 0 : read(data, now);
# 182 0 : nSize -= now;
# 183 0 : }
# 184 0 : }
# 185 :
# 186 : template<typename T>
# 187 : CHashVerifier<Source>& operator>>(T&& obj)
# 188 3024 : {
# 189 3024 : // Unserialize from this stream
# 190 3024 : ::Unserialize(*this, obj);
# 191 3024 : return (*this);
# 192 3024 : }
# 193 : };
# 194 :
# 195 : /** Compute the 256-bit hash of an object's serialization. */
# 196 : template<typename T>
# 197 : uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
# 198 1097741 : {
# 199 1097741 : CHashWriter ss(nType, nVersion);
# 200 1097741 : ss << obj;
# 201 1097741 : return ss.GetHash();
# 202 1097741 : }
# 203 :
# 204 : unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
# 205 :
# 206 : void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
# 207 :
# 208 : #endif // BITCOIN_HASH_H
|