/src/hash.h

https://gitlab.com/Ltaimao/bitcoin · C Header · 164 lines · 123 code · 26 blank · 15 comment · 7 complexity · bc244f4e0ce58f339ac4f891f92abce9 MD5 · raw file

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