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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 The Bitcoin developers
  3. // Distributed under the MIT/X11 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 "serialize.h"
  8. #include "uint256.h"
  9. #include "version.h"
  10. #include <vector>
  11. #include <openssl/ripemd.h>
  12. #include <openssl/sha.h>
  13. template<typename T1>
  14. inline uint256 Hash(const T1 pbegin, const T1 pend)
  15. {
  16. static unsigned char pblank[1];
  17. uint256 hash1;
  18. SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
  19. uint256 hash2;
  20. SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
  21. return hash2;
  22. }
  23. class CHashWriter
  24. {
  25. private:
  26. SHA256_CTX ctx;
  27. public:
  28. int nType;
  29. int nVersion;
  30. void Init() {
  31. SHA256_Init(&ctx);
  32. }
  33. CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {
  34. Init();
  35. }
  36. CHashWriter& write(const char *pch, size_t size) {
  37. SHA256_Update(&ctx, pch, size);
  38. return (*this);
  39. }
  40. // invalidates the object
  41. uint256 GetHash() {
  42. uint256 hash1;
  43. SHA256_Final((unsigned char*)&hash1, &ctx);
  44. uint256 hash2;
  45. SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
  46. return hash2;
  47. }
  48. template<typename T>
  49. CHashWriter& operator<<(const T& obj) {
  50. // Serialize to this stream
  51. ::Serialize(*this, obj, nType, nVersion);
  52. return (*this);
  53. }
  54. };
  55. template<typename T1, typename T2>
  56. inline uint256 Hash(const T1 p1begin, const T1 p1end,
  57. const T2 p2begin, const T2 p2end)
  58. {
  59. static unsigned char pblank[1];
  60. uint256 hash1;
  61. SHA256_CTX ctx;
  62. SHA256_Init(&ctx);
  63. SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
  64. SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
  65. SHA256_Final((unsigned char*)&hash1, &ctx);
  66. uint256 hash2;
  67. SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
  68. return hash2;
  69. }
  70. template<typename T1, typename T2, typename T3>
  71. inline uint256 Hash(const T1 p1begin, const T1 p1end,
  72. const T2 p2begin, const T2 p2end,
  73. const T3 p3begin, const T3 p3end)
  74. {
  75. static unsigned char pblank[1];
  76. uint256 hash1;
  77. SHA256_CTX ctx;
  78. SHA256_Init(&ctx);
  79. SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
  80. SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
  81. SHA256_Update(&ctx, (p3begin == p3end ? pblank : (unsigned char*)&p3begin[0]), (p3end - p3begin) * sizeof(p3begin[0]));
  82. SHA256_Final((unsigned char*)&hash1, &ctx);
  83. uint256 hash2;
  84. SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
  85. return hash2;
  86. }
  87. template<typename T>
  88. uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
  89. {
  90. CHashWriter ss(nType, nVersion);
  91. ss << obj;
  92. return ss.GetHash();
  93. }
  94. template<typename T1>
  95. inline uint160 Hash160(const T1 pbegin, const T1 pend)
  96. {
  97. static unsigned char pblank[1];
  98. uint256 hash1;
  99. SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
  100. uint160 hash2;
  101. RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
  102. return hash2;
  103. }
  104. inline uint160 Hash160(const std::vector<unsigned char>& vch)
  105. {
  106. return Hash160(vch.begin(), vch.end());
  107. }
  108. unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
  109. typedef struct
  110. {
  111. SHA512_CTX ctxInner;
  112. SHA512_CTX ctxOuter;
  113. } HMAC_SHA512_CTX;
  114. int HMAC_SHA512_Init(HMAC_SHA512_CTX *pctx, const void *pkey, size_t len);
  115. int HMAC_SHA512_Update(HMAC_SHA512_CTX *pctx, const void *pdata, size_t len);
  116. int HMAC_SHA512_Final(unsigned char *pmd, HMAC_SHA512_CTX *pctx);
  117. #endif