From 9a8a1ace9e062a91bac7fa262276eaf7aa39c26e Mon Sep 17 00:00:00 2001 From: Tim Flynn Date: Mon, 15 Aug 2016 10:46:46 -0400 Subject: [PATCH] Merge #943: Fixed non-deterministic CSporkMessage hash function 5bb8dca Fixed non-deterministic CSporkMessage hash function CSporkMessage::GetHash() was including random data in the hash due to 4 bytes of structure padding between the 32 bit nSporkID and the following 64 bit nValue members. This has been fixed by using CHashWriter which serializes the structure data properly before hashing rather than relying on compiler defined behavior such as structure alignment. The underlying hash function is CHash256 which is bitcoin's double SHA-256 hash. HashX11 is not necessary here because the hash is only used to identify distinct spork messages. --- src/spork.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/spork.h b/src/spork.h index e4814b44c..f29c69280 100644 --- a/src/spork.h +++ b/src/spork.h @@ -62,7 +62,15 @@ public: CSporkMessage(int nSporkID, int64_t nValue, int64_t nTimeSigned) : nSporkID(nSporkID), nValue(nValue), nTimeSigned(nTimeSigned) {} CSporkMessage() : nSporkID(0), nValue(0), nTimeSigned(0) {} - uint256 GetHash() { return HashX11(BEGIN(nSporkID), END(nTimeSigned)); } + uint256 GetHash() + { + CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); + ss << nSporkID; + ss << nValue; + ss << nTimeSigned; + return ss.GetHash(); + } + bool Sign(std::string strSignKey); bool CheckSignature(); void Relay();