Changeset View
Changeset View
Standalone View
Standalone View
src/hash.h
Show All 20 Lines | |||||
/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */ | /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */ | ||||
class CHash256 { | class CHash256 { | ||||
private: | private: | ||||
CSHA256 sha; | CSHA256 sha; | ||||
public: | 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(Span<uint8_t> output) { | ||||
assert(output.size() == 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(output.data()); | ||||
} | } | ||||
CHash256 &Write(Span<const uint8_t> input) { | CHash256 &Write(Span<const uint8_t> input) { | ||||
sha.Write(input.data(), input.size()); | 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(Span<uint8_t> output) { | ||||
assert(output.size() == 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(output.data()); | ||||
} | } | ||||
CHash160 &Write(Span<const uint8_t> input) { | CHash160 &Write(Span<const uint8_t> input) { | ||||
sha.Write(input.data(), input.size()); | 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(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(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(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 20 Lines | public: | ||||
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(result); | ||||
return result; | return result; | ||||
} | } | ||||
/** | /** | ||||
* Returns the first 64 bits from the resulting hash. | * Returns the first 64 bits from the resulting hash. | ||||
*/ | */ | ||||
inline uint64_t GetCheapHash() { | inline uint64_t GetCheapHash() { | ||||
uint8_t result[CHash256::OUTPUT_SIZE]; | uint8_t result[CHash256::OUTPUT_SIZE]; | ||||
▲ Show 20 Lines • Show All 59 Lines • Show Last 20 Lines |