Changeset View
Changeset View
Standalone View
Standalone View
src/hash.h
Show All 26 Lines | public: | ||||
static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE; | static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE; | ||||
void Finalize(uint8_t hash[OUTPUT_SIZE]) { | void Finalize(uint8_t hash[OUTPUT_SIZE]) { | ||||
uint8_t buf[CSHA256::OUTPUT_SIZE]; | uint8_t buf[CSHA256::OUTPUT_SIZE]; | ||||
sha.Finalize(buf); | sha.Finalize(buf); | ||||
sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash); | sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash); | ||||
} | } | ||||
CHash256 &Write(const uint8_t *data, size_t len) { | CHash256 &Write(Span<const uint8_t> input) { | ||||
sha.Write(data, len); | sha.Write(input.data(), input.size()); | ||||
return *this; | return *this; | ||||
} | } | ||||
CHash256 &Reset() { | CHash256 &Reset() { | ||||
sha.Reset(); | sha.Reset(); | ||||
return *this; | return *this; | ||||
} | } | ||||
}; | }; | ||||
/** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */ | /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */ | ||||
class CHash160 { | class CHash160 { | ||||
private: | private: | ||||
CSHA256 sha; | CSHA256 sha; | ||||
public: | public: | ||||
static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE; | static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE; | ||||
void Finalize(uint8_t hash[OUTPUT_SIZE]) { | void Finalize(uint8_t hash[OUTPUT_SIZE]) { | ||||
uint8_t buf[CSHA256::OUTPUT_SIZE]; | uint8_t buf[CSHA256::OUTPUT_SIZE]; | ||||
sha.Finalize(buf); | sha.Finalize(buf); | ||||
CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash); | CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash); | ||||
} | } | ||||
CHash160 &Write(const uint8_t *data, size_t len) { | CHash160 &Write(Span<const uint8_t> input) { | ||||
sha.Write(data, len); | sha.Write(input.data(), input.size()); | ||||
return *this; | return *this; | ||||
} | } | ||||
CHash160 &Reset() { | CHash160 &Reset() { | ||||
sha.Reset(); | sha.Reset(); | ||||
return *this; | return *this; | ||||
} | } | ||||
}; | }; | ||||
/** Compute the 256-bit hash of an object. */ | /** Compute the 256-bit hash of an object. */ | ||||
template <typename T1> inline uint256 Hash(const T1 pbegin, const T1 pend) { | template <typename T1> inline uint256 Hash(const T1 pbegin, const T1 pend) { | ||||
static const uint8_t pblank[1] = {}; | static const uint8_t pblank[1] = {}; | ||||
uint256 result; | uint256 result; | ||||
CHash256() | CHash256() | ||||
.Write(pbegin == pend ? pblank : (const uint8_t *)&pbegin[0], | .Write({pbegin == pend ? pblank : (const uint8_t *)&pbegin[0], | ||||
(pend - pbegin) * sizeof(pbegin[0])) | (pend - pbegin) * sizeof(pbegin[0])}) | ||||
.Finalize((uint8_t *)&result); | .Finalize((uint8_t *)&result); | ||||
return result; | return result; | ||||
} | } | ||||
/** Compute the 256-bit hash of the concatenation of two objects. */ | /** Compute the 256-bit hash of the concatenation of two objects. */ | ||||
template <typename T1, typename T2> | template <typename T1, typename T2> | ||||
inline uint256 Hash(const T1 p1begin, const T1 p1end, const T2 p2begin, | inline uint256 Hash(const T1 p1begin, const T1 p1end, const T2 p2begin, | ||||
const T2 p2end) { | const T2 p2end) { | ||||
static const uint8_t pblank[1] = {}; | static const uint8_t pblank[1] = {}; | ||||
uint256 result; | uint256 result; | ||||
CHash256() | CHash256() | ||||
.Write(p1begin == p1end ? pblank : (const uint8_t *)&p1begin[0], | .Write({p1begin == p1end ? pblank : (const uint8_t *)&p1begin[0], | ||||
(p1end - p1begin) * sizeof(p1begin[0])) | (p1end - p1begin) * sizeof(p1begin[0])}) | ||||
.Write(p2begin == p2end ? pblank : (const uint8_t *)&p2begin[0], | .Write({p2begin == p2end ? pblank : (const uint8_t *)&p2begin[0], | ||||
(p2end - p2begin) * sizeof(p2begin[0])) | (p2end - p2begin) * sizeof(p2begin[0])}) | ||||
.Finalize((uint8_t *)&result); | .Finalize((uint8_t *)&result); | ||||
return result; | return result; | ||||
} | } | ||||
/** Compute the 160-bit hash an object. */ | /** Compute the 160-bit hash an object. */ | ||||
template <typename T1> inline uint160 Hash160(const T1 pbegin, const T1 pend) { | template <typename T1> inline uint160 Hash160(const T1 pbegin, const T1 pend) { | ||||
static uint8_t pblank[1] = {}; | static uint8_t pblank[1] = {}; | ||||
uint160 result; | uint160 result; | ||||
CHash160() | CHash160() | ||||
.Write(pbegin == pend ? pblank : (const uint8_t *)&pbegin[0], | .Write({pbegin == pend ? pblank : (const uint8_t *)&pbegin[0], | ||||
(pend - pbegin) * sizeof(pbegin[0])) | (pend - pbegin) * sizeof(pbegin[0])}) | ||||
.Finalize((uint8_t *)&result); | .Finalize((uint8_t *)&result); | ||||
return result; | return result; | ||||
} | } | ||||
/** Compute the 160-bit hash of a vector. */ | /** Compute the 160-bit hash of a vector. */ | ||||
inline uint160 Hash160(const std::vector<uint8_t> &vch) { | inline uint160 Hash160(const std::vector<uint8_t> &vch) { | ||||
return Hash160(vch.begin(), vch.end()); | return Hash160(vch.begin(), vch.end()); | ||||
} | } | ||||
Show All 15 Lines | |||||
public: | public: | ||||
CHashWriter(int nTypeIn, int nVersionIn) | CHashWriter(int nTypeIn, int nVersionIn) | ||||
: nType(nTypeIn), nVersion(nVersionIn) {} | : nType(nTypeIn), nVersion(nVersionIn) {} | ||||
int GetType() const { return nType; } | int GetType() const { return nType; } | ||||
int GetVersion() const { return nVersion; } | int GetVersion() const { return nVersion; } | ||||
void write(const char *pch, size_t size) { | void write(const char *pch, size_t size) { | ||||
ctx.Write((const uint8_t *)pch, size); | ctx.Write({(const uint8_t *)pch, size}); | ||||
} | } | ||||
// invalidates the object | // invalidates the object | ||||
uint256 GetHash() { | uint256 GetHash() { | ||||
uint256 result; | uint256 result; | ||||
ctx.Finalize((uint8_t *)&result); | ctx.Finalize((uint8_t *)&result); | ||||
return result; | return result; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |