Ariel
6 years ago
committed by
Jack Grigg
No known key found for this signature in database
GPG Key ID: 665DBCD284F7DAFF
1 changed files with
43 additions and
0 deletions
-
src/hash.h
|
|
@ -12,6 +12,8 @@ |
|
|
|
#include "uint256.h" |
|
|
|
#include "version.h" |
|
|
|
|
|
|
|
#include "sodium.h" |
|
|
|
|
|
|
|
#include <vector> |
|
|
|
|
|
|
|
typedef uint256 ChainCode; |
|
|
@ -150,6 +152,47 @@ public: |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** A writer stream (for serialization) that computes a 256-bit BLAKE2b hash. */ |
|
|
|
class CBLAKE2bWriter |
|
|
|
{ |
|
|
|
private: |
|
|
|
crypto_generichash_blake2b_state state; |
|
|
|
|
|
|
|
public: |
|
|
|
int nType; |
|
|
|
int nVersion; |
|
|
|
|
|
|
|
CBLAKE2bWriter(int nTypeIn, int nVersionIn, const unsigned char* personal) : nType(nTypeIn), nVersion(nVersionIn) { |
|
|
|
assert(crypto_generichash_blake2b_init_salt_personal( |
|
|
|
&state, |
|
|
|
NULL, 0, // No key.
|
|
|
|
32, |
|
|
|
NULL, // No salt.
|
|
|
|
personal) == 0); |
|
|
|
} |
|
|
|
|
|
|
|
CBLAKE2bWriter& write(const char *pch, size_t size) { |
|
|
|
crypto_generichash_blake2b_update(&state, (const unsigned char*)pch, size); |
|
|
|
return (*this); |
|
|
|
} |
|
|
|
|
|
|
|
// invalidates the object
|
|
|
|
uint256 GetHash() { |
|
|
|
uint256 result; |
|
|
|
crypto_generichash_blake2b_final(&state, (unsigned char*)&result, 32); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
template<typename T> |
|
|
|
CBLAKE2bWriter& operator<<(const T& obj) { |
|
|
|
// Serialize to this stream
|
|
|
|
::Serialize(*this, obj, nType, nVersion); |
|
|
|
return (*this); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** Compute the 256-bit hash of an object's serialization. */ |
|
|
|
template<typename T> |
|
|
|
uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) |
|
|
|