Skip to content

Commit 7f65e76

Browse files
committed
Add support for SHA-256 midstate access
1 parent 7a01d4f commit 7f65e76

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/crypto/sha256.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,23 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
670670
}
671671
return *this;
672672
}
673+
//
674+
//void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
675+
//{
676+
// static const unsigned char pad[64] = {0x80};
677+
// unsigned char sizedesc[8];
678+
// WriteBE64(sizedesc, bytes << 3);
679+
// Write(pad, 1 + ((119 - (bytes % 64)) % 64));
680+
// Write(sizedesc, 8);
681+
// WriteBE32(hash, s[0]);
682+
// WriteBE32(hash + 4, s[1]);
683+
// WriteBE32(hash + 8, s[2]);
684+
// WriteBE32(hash + 12, s[3]);
685+
// WriteBE32(hash + 16, s[4]);
686+
// WriteBE32(hash + 20, s[5]);
687+
// WriteBE32(hash + 24, s[6]);
688+
// WriteBE32(hash + 28, s[7]);
689+
//}
673690

674691
void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
675692
{
@@ -678,6 +695,11 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
678695
WriteBE64(sizedesc, bytes << 3);
679696
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
680697
Write(sizedesc, 8);
698+
Midstate(hash, NULL, NULL);
699+
}
700+
701+
void CSHA256::Midstate(unsigned char hash[OUTPUT_SIZE], uint64_t* len, unsigned char *buffer)
702+
{
681703
WriteBE32(hash, s[0]);
682704
WriteBE32(hash + 4, s[1]);
683705
WriteBE32(hash + 8, s[2]);
@@ -686,6 +708,12 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
686708
WriteBE32(hash + 20, s[5]);
687709
WriteBE32(hash + 24, s[6]);
688710
WriteBE32(hash + 28, s[7]);
711+
if (len) {
712+
*len = bytes << 3;
713+
}
714+
if (buffer) {
715+
memcpy(buffer, buf, bytes % 64);
716+
}
689717
}
690718

691719
CSHA256& CSHA256::Reset()

src/crypto/sha256.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class CSHA256
2323
CSHA256();
2424
CSHA256& Write(const unsigned char* data, size_t len);
2525
void Finalize(unsigned char hash[OUTPUT_SIZE]);
26+
//TODO: Midstate is a hack'ish speedup that probably should make way for something
27+
//akin to the SHA256D64 speedups
28+
void Midstate(unsigned char hash[OUTPUT_SIZE], uint64_t* len, unsigned char *buffer);
2629
CSHA256& Reset();
2730
};
2831

0 commit comments

Comments
 (0)