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.
This commit is contained in:
Tim Flynn 2016-08-15 10:46:46 -04:00 committed by Holger Schinzel
parent a31e7600e9
commit 9a8a1ace9e

View File

@ -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();