You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hash.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 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. #ifndef BITCOIN_HASH_H
  6. #define BITCOIN_HASH_H
  7. #include "crypto/ripemd160.h"
  8. #include "crypto/sha256.h"
  9. #include "prevector.h"
  10. #include "serialize.h"
  11. #include "uint256.h"
  12. #include "version.h"
  13. #include <vector>
  14. typedef uint256 ChainCode;
  15. /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
  16. class CHash256 {
  17. private:
  18. CSHA256 sha;
  19. public:
  20. static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
  21. void Finalize(unsigned char hash[OUTPUT_SIZE]) {
  22. unsigned char buf[sha.OUTPUT_SIZE];
  23. sha.Finalize(buf);
  24. sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
  25. }
  26. CHash256& Write(const unsigned char *data, size_t len) {
  27. sha.Write(data, len);
  28. return *this;
  29. }
  30. CHash256& Reset() {
  31. sha.Reset();
  32. return *this;
  33. }
  34. };
  35. /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
  36. class CHash160 {
  37. private:
  38. CSHA256 sha;
  39. public:
  40. static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
  41. void Finalize(unsigned char hash[OUTPUT_SIZE]) {
  42. unsigned char buf[sha.OUTPUT_SIZE];
  43. sha.Finalize(buf);
  44. CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
  45. }
  46. CHash160& Write(const unsigned char *data, size_t len) {
  47. sha.Write(data, len);
  48. return *this;
  49. }
  50. CHash160& Reset() {
  51. sha.Reset();
  52. return *this;
  53. }
  54. };
  55. /** Compute the 256-bit hash of an object. */
  56. template<typename T1>
  57. inline uint256 Hash(const T1 pbegin, const T1 pend)
  58. {
  59. static const unsigned char pblank[1] = {};
  60. uint256 result;
  61. CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
  62. .Finalize((unsigned char*)&result);
  63. return result;
  64. }
  65. /** Compute the 256-bit hash of the concatenation of two objects. */
  66. template<typename T1, typename T2>
  67. inline uint256 Hash(const T1 p1begin, const T1 p1end,
  68. const T2 p2begin, const T2 p2end) {
  69. static const unsigned char pblank[1] = {};
  70. uint256 result;
  71. CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
  72. .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
  73. .Finalize((unsigned char*)&result);
  74. return result;
  75. }
  76. /** Compute the 256-bit hash of the concatenation of three objects. */
  77. template<typename T1, typename T2, typename T3>
  78. inline uint256 Hash(const T1 p1begin, const T1 p1end,
  79. const T2 p2begin, const T2 p2end,
  80. const T3 p3begin, const T3 p3end) {
  81. static const unsigned char pblank[1] = {};
  82. uint256 result;
  83. CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
  84. .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
  85. .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
  86. .Finalize((unsigned char*)&result);
  87. return result;
  88. }
  89. /** Compute the 160-bit hash an object. */
  90. template<typename T1>
  91. inline uint160 Hash160(const T1 pbegin, const T1 pend)
  92. {
  93. static unsigned char pblank[1] = {};
  94. uint160 result;
  95. CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
  96. .Finalize((unsigned char*)&result);
  97. return result;
  98. }
  99. /** Compute the 160-bit hash of a vector. */
  100. inline uint160 Hash160(const std::vector<unsigned char>& vch)
  101. {
  102. return Hash160(vch.begin(), vch.end());
  103. }
  104. /** Compute the 160-bit hash of a vector. */
  105. template<unsigned int N>
  106. inline uint160 Hash160(const prevector<N, unsigned char>& vch)
  107. {
  108. return Hash160(vch.begin(), vch.end());
  109. }
  110. /** A writer stream (for serialization) that computes a 256-bit hash. */
  111. class CHashWriter
  112. {
  113. private:
  114. CHash256 ctx;
  115. public:
  116. int nType;
  117. int nVersion;
  118. CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
  119. CHashWriter& write(const char *pch, size_t size) {
  120. ctx.Write((const unsigned char*)pch, size);
  121. return (*this);
  122. }
  123. // invalidates the object
  124. uint256 GetHash() {
  125. uint256 result;
  126. ctx.Finalize((unsigned char*)&result);
  127. return result;
  128. }
  129. template<typename T>
  130. CHashWriter& operator<<(const T& obj) {
  131. // Serialize to this stream
  132. ::Serialize(*this, obj, nType, nVersion);
  133. return (*this);
  134. }
  135. };
  136. /** Compute the 256-bit hash of an object's serialization. */
  137. template<typename T>
  138. uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
  139. {
  140. CHashWriter ss(nType, nVersion);
  141. ss << obj;
  142. return ss.GetHash();
  143. }
  144. unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
  145. void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
  146. #endif // BITCOIN_HASH_H