Add <Hasher>::OUTPUT_SIZE

This commit is contained in:
Pieter Wuille 2014-06-12 13:34:29 +02:00 committed by Pieter Wuille
parent 4791b99e2d
commit a0495bb68c
8 changed files with 31 additions and 16 deletions

View file

@ -184,7 +184,7 @@ CRIPEMD160& CRIPEMD160::Write(const unsigned char *data, size_t len) {
return *this; return *this;
} }
void CRIPEMD160::Finalize(unsigned char *hash) { void CRIPEMD160::Finalize(unsigned char hash[OUTPUT_SIZE]) {
static const unsigned char pad[64] = {0x80}; static const unsigned char pad[64] = {0x80};
unsigned char sizedesc[8]; unsigned char sizedesc[8];
WriteLE64(sizedesc, bytes << 3); WriteLE64(sizedesc, bytes << 3);

View file

@ -16,9 +16,11 @@ private:
size_t bytes; size_t bytes;
public: public:
static const size_t OUTPUT_SIZE = 20;
CRIPEMD160(); CRIPEMD160();
CRIPEMD160& Write(const unsigned char *data, size_t len); CRIPEMD160& Write(const unsigned char *data, size_t len);
void Finalize(unsigned char *hash); void Finalize(unsigned char hash[OUTPUT_SIZE]);
CRIPEMD160& Reset(); CRIPEMD160& Reset();
}; };

View file

@ -172,7 +172,7 @@ CSHA1& CSHA1::Write(const unsigned char *data, size_t len) {
return *this; return *this;
} }
void CSHA1::Finalize(unsigned char *hash) { void CSHA1::Finalize(unsigned char hash[OUTPUT_SIZE]) {
static const unsigned char pad[64] = {0x80}; static const unsigned char pad[64] = {0x80};
unsigned char sizedesc[8]; unsigned char sizedesc[8];
WriteBE64(sizedesc, bytes << 3); WriteBE64(sizedesc, bytes << 3);

View file

@ -16,9 +16,11 @@ private:
size_t bytes; size_t bytes;
public: public:
static const size_t OUTPUT_SIZE = 20;
CSHA1(); CSHA1();
CSHA1& Write(const unsigned char *data, size_t len); CSHA1& Write(const unsigned char *data, size_t len);
void Finalize(unsigned char *hash); void Finalize(unsigned char hash[OUTPUT_SIZE]);
CSHA1& Reset(); CSHA1& Reset();
}; };

View file

@ -295,7 +295,7 @@ CSHA256& CSHA256::Write(const unsigned char *data, size_t len) {
return *this; return *this;
} }
void CSHA256::Finalize(unsigned char *hash) { void CSHA256::Finalize(unsigned char hash[OUTPUT_SIZE]) {
static const unsigned char pad[64] = {0x80}; static const unsigned char pad[64] = {0x80};
unsigned char sizedesc[8]; unsigned char sizedesc[8];
WriteBE64(sizedesc, bytes << 3); WriteBE64(sizedesc, bytes << 3);
@ -348,7 +348,7 @@ CSHA512& CSHA512::Write(const unsigned char *data, size_t len) {
return *this; return *this;
} }
void CSHA512::Finalize(unsigned char *hash) { void CSHA512::Finalize(unsigned char hash[OUTPUT_SIZE]) {
static const unsigned char pad[128] = {0x80}; static const unsigned char pad[128] = {0x80};
unsigned char sizedesc[16] = {0x00}; unsigned char sizedesc[16] = {0x00};
WriteBE64(sizedesc+8, bytes << 3); WriteBE64(sizedesc+8, bytes << 3);
@ -391,7 +391,7 @@ CHMAC_SHA512::CHMAC_SHA512(const unsigned char *key, size_t keylen) {
inner.Write(rkey, 128); inner.Write(rkey, 128);
} }
void CHMAC_SHA512::Finalize(unsigned char *hash) { void CHMAC_SHA512::Finalize(unsigned char hash[OUTPUT_SIZE]) {
unsigned char temp[64]; unsigned char temp[64];
inner.Finalize(temp); inner.Finalize(temp);
outer.Write(temp, 64).Finalize(hash); outer.Write(temp, 64).Finalize(hash);

View file

@ -16,9 +16,11 @@ private:
size_t bytes; size_t bytes;
public: public:
static const size_t OUTPUT_SIZE = 32;
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); void Finalize(unsigned char hash[OUTPUT_SIZE]);
CSHA256& Reset(); CSHA256& Reset();
}; };
@ -30,9 +32,11 @@ private:
size_t bytes; size_t bytes;
public: public:
static const size_t OUTPUT_SIZE = 64;
CSHA512(); CSHA512();
CSHA512& Write(const unsigned char *data, size_t len); CSHA512& Write(const unsigned char *data, size_t len);
void Finalize(unsigned char *hash); void Finalize(unsigned char hash[OUTPUT_SIZE]);
CSHA512& Reset(); CSHA512& Reset();
}; };
@ -43,12 +47,14 @@ private:
CSHA512 inner; CSHA512 inner;
public: public:
static const size_t OUTPUT_SIZE = 64;
CHMAC_SHA512(const unsigned char *key, size_t keylen); CHMAC_SHA512(const unsigned char *key, size_t keylen);
CHMAC_SHA512& Write(const unsigned char *data, size_t len) { CHMAC_SHA512& Write(const unsigned char *data, size_t len) {
inner.Write(data, len); inner.Write(data, len);
return *this; return *this;
} }
void Finalize(unsigned char *hash); void Finalize(unsigned char hash[OUTPUT_SIZE]);
}; };
#endif #endif

View file

@ -19,10 +19,12 @@ class CHash256 {
private: private:
CSHA256 sha; CSHA256 sha;
public: public:
void Finalize(unsigned char *hash) { static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
unsigned char buf[32];
void Finalize(unsigned char hash[OUTPUT_SIZE]) {
unsigned char buf[sha.OUTPUT_SIZE];
sha.Finalize(buf); sha.Finalize(buf);
sha.Reset().Write(buf, 32).Finalize(hash); sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
} }
CHash256& Write(const unsigned char *data, size_t len) { CHash256& Write(const unsigned char *data, size_t len) {
@ -41,10 +43,12 @@ class CHash160 {
private: private:
CSHA256 sha; CSHA256 sha;
public: public:
void Finalize(unsigned char *hash) { static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
unsigned char buf[32];
void Finalize(unsigned char hash[OUTPUT_SIZE]) {
unsigned char buf[sha.OUTPUT_SIZE];
sha.Finalize(buf); sha.Finalize(buf);
CRIPEMD160().Write(buf, 32).Finalize(hash); CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
} }
CHash160& Write(const unsigned char *data, size_t len) { CHash160& Write(const unsigned char *data, size_t len) {

View file

@ -16,6 +16,7 @@ BOOST_AUTO_TEST_SUITE(crypto_tests)
template<typename Hasher, typename In, typename Out> template<typename Hasher, typename In, typename Out>
void TestVector(const Hasher &h, const In &in, const Out &out) { void TestVector(const Hasher &h, const In &in, const Out &out) {
Out hash; Out hash;
BOOST_CHECK(out.size() == h.OUTPUT_SIZE);
hash.resize(out.size()); hash.resize(out.size());
{ {
// Test that writing the whole input string at once works. // Test that writing the whole input string at once works.