add blake2b writer
This commit is contained in:
parent
d55e5e778b
commit
132dc81f7d
43
src/hash.h
43
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)
|
||||
|
|
Loading…
Reference in New Issue