Added SHA256Compress to Bitcoin's SHA256 implementation.
This commit is contained in:
@@ -76,7 +76,8 @@ BITCOIN_TESTS =\
|
|||||||
test/transaction_tests.cpp \
|
test/transaction_tests.cpp \
|
||||||
test/uint256_tests.cpp \
|
test/uint256_tests.cpp \
|
||||||
test/univalue_tests.cpp \
|
test/univalue_tests.cpp \
|
||||||
test/util_tests.cpp
|
test/util_tests.cpp \
|
||||||
|
test/sha256compress_tests.cpp
|
||||||
|
|
||||||
if ENABLE_WALLET
|
if ENABLE_WALLET
|
||||||
BITCOIN_TESTS += \
|
BITCOIN_TESTS += \
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "crypto/common.h"
|
#include "crypto/common.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
// Internal implementation code.
|
// Internal implementation code.
|
||||||
namespace
|
namespace
|
||||||
@@ -171,6 +172,15 @@ void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
|
|||||||
WriteBE64(sizedesc, bytes << 3);
|
WriteBE64(sizedesc, bytes << 3);
|
||||||
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
|
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
|
||||||
Write(sizedesc, 8);
|
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, s[0]);
|
||||||
WriteBE32(hash + 4, s[1]);
|
WriteBE32(hash + 4, s[1]);
|
||||||
WriteBE32(hash + 8, s[2]);
|
WriteBE32(hash + 8, s[2]);
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ public:
|
|||||||
CSHA256();
|
CSHA256();
|
||||||
CSHA256& Write(const unsigned char* data, size_t len);
|
CSHA256& Write(const unsigned char* data, size_t len);
|
||||||
void Finalize(unsigned char hash[OUTPUT_SIZE]);
|
void Finalize(unsigned char hash[OUTPUT_SIZE]);
|
||||||
|
void FinalizeNoPadding(unsigned char hash[OUTPUT_SIZE], bool enforce_compression = true);
|
||||||
CSHA256& Reset();
|
CSHA256& Reset();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
35
src/test/sha256compress_tests.cpp
Normal file
35
src/test/sha256compress_tests.cpp
Normal file
@@ -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()
|
||||||
Reference in New Issue
Block a user