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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels 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 STARWELS_HASH_H
  6. #define STARWELS_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 Starwels'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[CSHA256::OUTPUT_SIZE];
  23. sha.Finalize(buf);
  24. sha.Reset().Write(buf, CSHA256::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 Starwels'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[CSHA256::OUTPUT_SIZE];
  43. sha.Finalize(buf);
  44. CRIPEMD160().Write(buf, CSHA256::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. const int nType;
  116. const int nVersion;
  117. public:
  118. CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
  119. int GetType() const { return nType; }
  120. int GetVersion() const { return nVersion; }
  121. void write(const char *pch, size_t size) {
  122. ctx.Write((const unsigned char*)pch, size);
  123. }
  124. // invalidates the object
  125. uint256 GetHash() {
  126. uint256 result;
  127. ctx.Finalize((unsigned char*)&result);
  128. return result;
  129. }
  130. template<typename T>
  131. CHashWriter& operator<<(const T& obj) {
  132. // Serialize to this stream
  133. ::Serialize(*this, obj);
  134. return (*this);
  135. }
  136. };
  137. /** Reads data from an underlying stream, while hashing the read data. */
  138. template<typename Source>
  139. class CHashVerifier : public CHashWriter
  140. {
  141. private:
  142. Source* source;
  143. public:
  144. CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
  145. void read(char* pch, size_t nSize)
  146. {
  147. source->read(pch, nSize);
  148. this->write(pch, nSize);
  149. }
  150. void ignore(size_t nSize)
  151. {
  152. char data[1024];
  153. while (nSize > 0) {
  154. size_t now = std::min<size_t>(nSize, 1024);
  155. read(data, now);
  156. nSize -= now;
  157. }
  158. }
  159. template<typename T>
  160. CHashVerifier<Source>& operator>>(T& obj)
  161. {
  162. // Unserialize from this stream
  163. ::Unserialize(*this, obj);
  164. return (*this);
  165. }
  166. };
  167. /** Compute the 256-bit hash of an object's serialization. */
  168. template<typename T>
  169. uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
  170. {
  171. CHashWriter ss(nType, nVersion);
  172. ss << obj;
  173. return ss.GetHash();
  174. }
  175. unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
  176. void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
  177. /** SipHash-2-4 */
  178. class CSipHasher
  179. {
  180. private:
  181. uint64_t v[4];
  182. uint64_t tmp;
  183. int count;
  184. public:
  185. /** Construct a SipHash calculator initialized with 128-bit key (k0, k1) */
  186. CSipHasher(uint64_t k0, uint64_t k1);
  187. /** Hash a 64-bit integer worth of data
  188. * It is treated as if this was the little-endian interpretation of 8 bytes.
  189. * This function can only be used when a multiple of 8 bytes have been written so far.
  190. */
  191. CSipHasher& Write(uint64_t data);
  192. /** Hash arbitrary bytes. */
  193. CSipHasher& Write(const unsigned char* data, size_t size);
  194. /** Compute the 64-bit SipHash-2-4 of the data written so far. The object remains untouched. */
  195. uint64_t Finalize() const;
  196. };
  197. /** Optimized SipHash-2-4 implementation for uint256.
  198. *
  199. * It is identical to:
  200. * SipHasher(k0, k1)
  201. * .Write(val.GetUint64(0))
  202. * .Write(val.GetUint64(1))
  203. * .Write(val.GetUint64(2))
  204. * .Write(val.GetUint64(3))
  205. * .Finalize()
  206. */
  207. uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val);
  208. uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra);
  209. #endif // STARWELS_HASH_H