Browse Source

Added SHA256Compress to Bitcoin's SHA256 implementation.

pull/4/head
Sean Bowe 8 years ago
parent
commit
8466467a35
  1. 3
      src/Makefile.test.include
  2. 10
      src/crypto/sha256.cpp
  3. 1
      src/crypto/sha256.h
  4. 35
      src/test/sha256compress_tests.cpp

3
src/Makefile.test.include

@ -76,7 +76,8 @@ BITCOIN_TESTS =\
test/transaction_tests.cpp \
test/uint256_tests.cpp \
test/univalue_tests.cpp \
test/util_tests.cpp
test/util_tests.cpp \
test/sha256compress_tests.cpp
if ENABLE_WALLET
BITCOIN_TESTS += \

10
src/crypto/sha256.cpp

@ -7,6 +7,7 @@
#include "crypto/common.h"
#include <string.h>
#include <stdexcept>
// Internal implementation code.
namespace
@ -171,6 +172,15 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
WriteBE64(sizedesc, bytes << 3);
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
Write(sizedesc, 8);
FinalizeNoPadding(hash, false);
}
void CSHA256::FinalizeNoPadding(unsigned char hash[OUTPUT_SIZE], bool enforce_compression)
{
if (enforce_compression && bytes != 64) {
throw std::length_error("SHA256Compress should be invoked with a 512-bit block");
}
WriteBE32(hash, s[0]);
WriteBE32(hash + 4, s[1]);
WriteBE32(hash + 8, s[2]);

1
src/crypto/sha256.h

@ -22,6 +22,7 @@ public:
CSHA256();
CSHA256& Write(const unsigned char* data, size_t len);
void Finalize(unsigned char hash[OUTPUT_SIZE]);
void FinalizeNoPadding(unsigned char hash[OUTPUT_SIZE], bool enforce_compression = true);
CSHA256& Reset();
};

35
src/test/sha256compress_tests.cpp

@ -0,0 +1,35 @@
#include "test/test_bitcoin.h"
#include "crypto/sha256.h"
#include "uint256.h"
#include <stdexcept>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(sha256compress_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(compression)
{
{
unsigned char preimage[64] = {};
CSHA256 hasher;
hasher.Write(&preimage[0], 64);
uint256 digest;
hasher.FinalizeNoPadding(digest.begin());
BOOST_CHECK_MESSAGE(digest == uint256S("d8a93718eaf9feba4362d2c091d4e58ccabe9f779957336269b4b917be9856da"),
digest.GetHex());
}
{
unsigned char preimage[63] = {};
CSHA256 hasher;
hasher.Write(&preimage[0], 63);
uint256 digest;
BOOST_CHECK_THROW(hasher.FinalizeNoPadding(digest.begin()), std::length_error);
}
}
BOOST_AUTO_TEST_SUITE_END()
Loading…
Cancel
Save