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.cpp 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2013-2015 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "hash.h"
  5. #include "crypto/common.h"
  6. #include "crypto/hmac_sha512.h"
  7. #include "pubkey.h"
  8. inline uint32_t ROTL32(uint32_t x, int8_t r)
  9. {
  10. return (x << r) | (x >> (32 - r));
  11. }
  12. unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash)
  13. {
  14. // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
  15. uint32_t h1 = nHashSeed;
  16. if (vDataToHash.size() > 0)
  17. {
  18. const uint32_t c1 = 0xcc9e2d51;
  19. const uint32_t c2 = 0x1b873593;
  20. const int nblocks = vDataToHash.size() / 4;
  21. //----------
  22. // body
  23. const uint8_t* blocks = &vDataToHash[0] + nblocks * 4;
  24. for (int i = -nblocks; i; i++) {
  25. uint32_t k1 = ReadLE32(blocks + i*4);
  26. k1 *= c1;
  27. k1 = ROTL32(k1, 15);
  28. k1 *= c2;
  29. h1 ^= k1;
  30. h1 = ROTL32(h1, 13);
  31. h1 = h1 * 5 + 0xe6546b64;
  32. }
  33. //----------
  34. // tail
  35. const uint8_t* tail = (const uint8_t*)(&vDataToHash[0] + nblocks * 4);
  36. uint32_t k1 = 0;
  37. switch (vDataToHash.size() & 3) {
  38. case 3:
  39. k1 ^= tail[2] << 16;
  40. case 2:
  41. k1 ^= tail[1] << 8;
  42. case 1:
  43. k1 ^= tail[0];
  44. k1 *= c1;
  45. k1 = ROTL32(k1, 15);
  46. k1 *= c2;
  47. h1 ^= k1;
  48. };
  49. }
  50. //----------
  51. // finalization
  52. h1 ^= vDataToHash.size();
  53. h1 ^= h1 >> 16;
  54. h1 *= 0x85ebca6b;
  55. h1 ^= h1 >> 13;
  56. h1 *= 0xc2b2ae35;
  57. h1 ^= h1 >> 16;
  58. return h1;
  59. }
  60. void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
  61. {
  62. unsigned char num[4];
  63. num[0] = (nChild >> 24) & 0xFF;
  64. num[1] = (nChild >> 16) & 0xFF;
  65. num[2] = (nChild >> 8) & 0xFF;
  66. num[3] = (nChild >> 0) & 0xFF;
  67. CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
  68. }