PRCYCoin  2.0.0.7rc1
P2P Digital Currency
hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013-2014 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "hash.h"
6 #include "crypto/common.h"
7 #include "crypto/hmac_sha512.h"
8 #include "crypto/scrypt.h"
9 
10 inline uint32_t ROTL32(uint32_t x, int8_t r)
11 {
12  return (x << r) | (x >> (32 - r));
13 }
14 
15 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash)
16 {
17  // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
18  uint32_t h1 = nHashSeed;
19  if (vDataToHash.size() > 0) {
20  const uint32_t c1 = 0xcc9e2d51;
21  const uint32_t c2 = 0x1b873593;
22 
23  const int nblocks = vDataToHash.size() / 4;
24 
25  //----------
26  // body
27  const uint8_t* blocks = &vDataToHash[0] + nblocks * 4;
28 
29  for (int i = -nblocks; i; i++) {
30  uint32_t k1 = ReadLE32(blocks + i*4);
31 
32  k1 *= c1;
33  k1 = ROTL32(k1, 15);
34  k1 *= c2;
35 
36  h1 ^= k1;
37  h1 = ROTL32(h1, 13);
38  h1 = h1 * 5 + 0xe6546b64;
39  }
40 
41  //----------
42  // tail
43  const uint8_t* tail = (const uint8_t*)(&vDataToHash[0] + nblocks * 4);
44 
45  uint32_t k1 = 0;
46 
47  switch (vDataToHash.size() & 3) {
48  case 3:
49  k1 ^= tail[2] << 16;
50  case 2:
51  k1 ^= tail[1] << 8;
52  case 1:
53  k1 ^= tail[0];
54  k1 *= c1;
55  k1 = ROTL32(k1, 15);
56  k1 *= c2;
57  h1 ^= k1;
58  };
59  }
60 
61  //----------
62  // finalization
63  h1 ^= vDataToHash.size();
64  h1 ^= h1 >> 16;
65  h1 *= 0x85ebca6b;
66  h1 ^= h1 >> 13;
67  h1 *= 0xc2b2ae35;
68  h1 ^= h1 >> 16;
69 
70  return h1;
71 }
72 
73 void BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
74 {
75  unsigned char num[4];
76  num[0] = (nChild >> 24) & 0xFF;
77  num[1] = (nChild >> 16) & 0xFF;
78  num[2] = (nChild >> 8) & 0xFF;
79  num[3] = (nChild >> 0) & 0xFF;
80  CHMAC_SHA512(chainCode, 32).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
81 }
82 
83 void scrypt_hash(const char* pass, unsigned int pLen, const char* salt, unsigned int sLen, char* output, unsigned int N, unsigned int r, unsigned int p, unsigned int dkLen)
84 {
85  scrypt(pass, pLen, salt, sLen, output, N, r, p, dkLen);
86 }
scrypt
void scrypt(const char *pass, unsigned int pLen, const char *salt, unsigned int sLen, char *output, unsigned int N, unsigned int r, unsigned int p, unsigned int dkLen)
Definition: scrypt.cpp:362
scrypt.h
scrypt_hash
void scrypt_hash(const char *pass, unsigned int pLen, const char *salt, unsigned int sLen, char *output, unsigned int N, unsigned int r, unsigned int p, unsigned int dkLen)
Definition: hash.cpp:83
MurmurHash3
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector< unsigned char > &vDataToHash)
Definition: hash.cpp:15
hmac_sha512.h
CHMAC_SHA512::Write
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha512.h:24
r
void const uint64_t uint64_t * r
Definition: field_5x52_asm_impl.h:10
ROTL32
uint32_t ROTL32(uint32_t x, int8_t r)
Definition: hash.cpp:10
CHMAC_SHA512::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hmac_sha512.cpp:29
BIP32Hash
void BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:73
common.h
hash.h
CHMAC_SHA512
A hasher class for HMAC-SHA-512.
Definition: hmac_sha512.h:14