diff --git a/src/crypto/sha256.cpp b/src/crypto/sha256.cpp index cab37e0322..e6a61c1691 100644 --- a/src/crypto/sha256.cpp +++ b/src/crypto/sha256.cpp @@ -670,6 +670,23 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len) } return *this; } +// +//void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE]) +//{ +// static const unsigned char pad[64] = {0x80}; +// unsigned char sizedesc[8]; +// WriteBE64(sizedesc, bytes << 3); +// Write(pad, 1 + ((119 - (bytes % 64)) % 64)); +// Write(sizedesc, 8); +// WriteBE32(hash, s[0]); +// WriteBE32(hash + 4, s[1]); +// WriteBE32(hash + 8, s[2]); +// WriteBE32(hash + 12, s[3]); +// WriteBE32(hash + 16, s[4]); +// WriteBE32(hash + 20, s[5]); +// WriteBE32(hash + 24, s[6]); +// WriteBE32(hash + 28, s[7]); +//} void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE]) { @@ -678,6 +695,11 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE]) WriteBE64(sizedesc, bytes << 3); Write(pad, 1 + ((119 - (bytes % 64)) % 64)); Write(sizedesc, 8); + Midstate(hash, NULL, NULL); +} + +void CSHA256::Midstate(unsigned char hash[OUTPUT_SIZE], uint64_t* len, unsigned char *buffer) +{ WriteBE32(hash, s[0]); WriteBE32(hash + 4, s[1]); WriteBE32(hash + 8, s[2]); @@ -686,6 +708,12 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE]) WriteBE32(hash + 20, s[5]); WriteBE32(hash + 24, s[6]); WriteBE32(hash + 28, s[7]); + if (len) { + *len = bytes << 3; + } + if (buffer) { + memcpy(buffer, buf, bytes % 64); + } } CSHA256& CSHA256::Reset() diff --git a/src/crypto/sha256.h b/src/crypto/sha256.h index 028ee14345..7cdcde1956 100644 --- a/src/crypto/sha256.h +++ b/src/crypto/sha256.h @@ -23,6 +23,9 @@ public: CSHA256(); CSHA256& Write(const unsigned char* data, size_t len); void Finalize(unsigned char hash[OUTPUT_SIZE]); + //TODO: Midstate is a hack'ish speedup that probably should make way for something + //akin to the SHA256D64 speedups + void Midstate(unsigned char hash[OUTPUT_SIZE], uint64_t* len, unsigned char *buffer); CSHA256& Reset(); };