diff options
author | Gavin Andresen <gavinandresen@gmail.com> | 2012-07-13 08:35:58 -0400 |
---|---|---|
committer | Gavin Andresen <gavinandresen@gmail.com> | 2012-07-13 08:35:58 -0400 |
commit | dbb0a12366ab9c95df62d643049c0500ae4a22ea (patch) | |
tree | c5f77b6d89e271f536cb59b77c644adabba8b0d2 | |
parent | 916b11fba5758fc7b5aa98a7bb78cfb22687c687 (diff) | |
parent | 6ece1d747eed764f727ef60961ebdac66ab249c7 (diff) |
Merge branch 'chashwriter' of https://github.com/sipa/bitcoin
-rw-r--r-- | src/util.h | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/src/util.h b/src/util.h index 7b2c678916..7fe204c0df 100644 --- a/src/util.h +++ b/src/util.h @@ -391,6 +391,46 @@ inline uint256 Hash(const T1 pbegin, const T1 pend) return hash2; } +class CHashWriter +{ +private: + SHA256_CTX ctx; + +public: + int nType; + int nVersion; + + void Init() { + SHA256_Init(&ctx); + } + + CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) { + Init(); + } + + CHashWriter& write(const char *pch, size_t size) { + SHA256_Update(&ctx, pch, size); + return (*this); + } + + // invalidates the object + uint256 GetHash() { + uint256 hash1; + SHA256_Final((unsigned char*)&hash1, &ctx); + uint256 hash2; + SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2); + return hash2; + } + + template<typename T> + CHashWriter& operator<<(const T& obj) { + // Serialize to this stream + ::Serialize(*this, obj, nType, nVersion); + return (*this); + } +}; + + template<typename T1, typename T2> inline uint256 Hash(const T1 p1begin, const T1 p1end, const T2 p2begin, const T2 p2end) @@ -428,13 +468,9 @@ inline uint256 Hash(const T1 p1begin, const T1 p1end, template<typename T> uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) { - // Most of the time is spent allocating and deallocating CDataStream's - // buffer. If this ever needs to be optimized further, make a CStaticStream - // class with its buffer on the stack. - CDataStream ss(nType, nVersion); - ss.reserve(10000); + CHashWriter ss(nType, nVersion); ss << obj; - return Hash(ss.begin(), ss.end()); + return ss.GetHash(); } inline uint160 Hash160(const std::vector<unsigned char>& vch) |