Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize remaining bytes of hash data to 0 when constructing from hex string #21

Merged
merged 1 commit into from Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/crypto/ripemd160.cpp
Expand Up @@ -10,12 +10,14 @@
#include <vector>
#include "_digest_common.hpp"

namespace fc
namespace fc
{

ripemd160::ripemd160() { memset( _hash, 0, sizeof(_hash) ); }
ripemd160::ripemd160( const string& hex_str ) {
fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
if( bytes_written < sizeof(_hash) )
memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
}

string ripemd160::str()const {
Expand Down Expand Up @@ -57,15 +59,15 @@ ripemd160 ripemd160::hash( const string& s ) {
}

void ripemd160::encoder::write( const char* d, uint32_t dlen ) {
RIPEMD160_Update( &my->ctx, d, dlen);
RIPEMD160_Update( &my->ctx, d, dlen);
}
ripemd160 ripemd160::encoder::result() {
ripemd160 h;
RIPEMD160_Final((uint8_t*)h.data(), &my->ctx );
return h;
}
void ripemd160::encoder::reset() {
RIPEMD160_Init( &my->ctx);
RIPEMD160_Init( &my->ctx);
}

ripemd160 operator << ( const ripemd160& h1, uint32_t i ) {
Expand Down Expand Up @@ -97,7 +99,7 @@ bool operator != ( const ripemd160& h1, const ripemd160& h2 ) {
bool operator == ( const ripemd160& h1, const ripemd160& h2 ) {
return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
}

void to_variant( const ripemd160& bi, variant& v )
{
v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
Expand All @@ -112,5 +114,5 @@ bool operator == ( const ripemd160& h1, const ripemd160& h2 ) {
else
memset( &bi, char(0), sizeof(bi) );
}

} // fc
16 changes: 9 additions & 7 deletions src/crypto/sha1.cpp
Expand Up @@ -7,12 +7,14 @@
#include <vector>
#include "_digest_common.hpp"

namespace fc
namespace fc
{

sha1::sha1() { memset( _hash, 0, sizeof(_hash) ); }
sha1::sha1( const string& hex_str ) {
fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
if( bytes_written < sizeof(_hash) )
memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
}

string sha1::str()const {
Expand Down Expand Up @@ -42,15 +44,15 @@ sha1 sha1::hash( const string& s ) {
}

void sha1::encoder::write( const char* d, uint32_t dlen ) {
SHA1_Update( &my->ctx, d, dlen);
SHA1_Update( &my->ctx, d, dlen);
}
sha1 sha1::encoder::result() {
sha1 h;
SHA1_Final((uint8_t*)h.data(), &my->ctx );
return h;
}
void sha1::encoder::reset() {
SHA1_Init( &my->ctx);
SHA1_Init( &my->ctx);
}

sha1 operator << ( const sha1& h1, uint32_t i ) {
Expand Down Expand Up @@ -82,7 +84,7 @@ bool operator != ( const sha1& h1, const sha1& h2 ) {
bool operator == ( const sha1& h1, const sha1& h2 ) {
return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
}

void to_variant( const sha1& bi, variant& v )
{
v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
Expand All @@ -97,5 +99,5 @@ bool operator == ( const sha1& h1, const sha1& h2 ) {
else
memset( &bi, char(0), sizeof(bi) );
}

} // fc
10 changes: 6 additions & 4 deletions src/crypto/sha224.cpp
Expand Up @@ -11,7 +11,9 @@ namespace fc {

sha224::sha224() { memset( _hash, 0, sizeof(_hash) ); }
sha224::sha224( const string& hex_str ) {
fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
if( bytes_written < sizeof(_hash) )
memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
}

string sha224::str()const {
Expand Down Expand Up @@ -41,15 +43,15 @@ namespace fc {
}

void sha224::encoder::write( const char* d, uint32_t dlen ) {
SHA224_Update( &my->ctx, d, dlen);
SHA224_Update( &my->ctx, d, dlen);
}
sha224 sha224::encoder::result() {
sha224 h;
SHA224_Final((uint8_t*)h.data(), &my->ctx );
return h;
}
void sha224::encoder::reset() {
SHA224_Init( &my->ctx);
SHA224_Init( &my->ctx);
}

sha224 operator << ( const sha224& h1, uint32_t i ) {
Expand Down Expand Up @@ -78,7 +80,7 @@ namespace fc {
bool operator == ( const sha224& h1, const sha224& h2 ) {
return memcmp( h1._hash, h2._hash, sizeof(sha224) ) == 0;
}

void to_variant( const sha224& bi, variant& v )
{
v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
Expand Down
12 changes: 7 additions & 5 deletions src/crypto/sha256.cpp
Expand Up @@ -12,13 +12,15 @@
namespace fc {

sha256::sha256() { memset( _hash, 0, sizeof(_hash) ); }
sha256::sha256( const char *data, size_t size ) {
if (size != sizeof(_hash))
sha256::sha256( const char *data, size_t size ) {
if (size != sizeof(_hash))
FC_THROW_EXCEPTION( exception, "sha256: size mismatch" );
memcpy(_hash, data, size );
}
sha256::sha256( const string& hex_str ) {
fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
if( bytes_written < sizeof(_hash) )
memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
}

string sha256::str()const {
Expand Down Expand Up @@ -54,15 +56,15 @@ namespace fc {
}

void sha256::encoder::write( const char* d, uint32_t dlen ) {
SHA256_Update( &my->ctx, d, dlen);
SHA256_Update( &my->ctx, d, dlen);
}
sha256 sha256::encoder::result() {
sha256 h;
SHA256_Final((uint8_t*)h.data(), &my->ctx );
return h;
}
void sha256::encoder::reset() {
SHA256_Init( &my->ctx);
SHA256_Init( &my->ctx);
}

sha256 operator << ( const sha256& h1, uint32_t i ) {
Expand Down
12 changes: 7 additions & 5 deletions src/crypto/sha512.cpp
Expand Up @@ -6,12 +6,14 @@
#include <fc/crypto/sha512.hpp>
#include <fc/variant.hpp>
#include "_digest_common.hpp"

namespace fc {

sha512::sha512() { memset( _hash, 0, sizeof(_hash) ); }
sha512::sha512( const string& hex_str ) {
fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
if( bytes_written < sizeof(_hash) )
memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
}

string sha512::str()const {
Expand Down Expand Up @@ -41,15 +43,15 @@ namespace fc {
}

void sha512::encoder::write( const char* d, uint32_t dlen ) {
SHA512_Update( &my->ctx, d, dlen);
SHA512_Update( &my->ctx, d, dlen);
}
sha512 sha512::encoder::result() {
sha512 h;
SHA512_Final((uint8_t*)h.data(), &my->ctx );
return h;
}
void sha512::encoder::reset() {
SHA512_Init( &my->ctx);
SHA512_Init( &my->ctx);
}

sha512 operator << ( const sha512& h1, uint32_t i ) {
Expand Down Expand Up @@ -84,7 +86,7 @@ namespace fc {
bool operator == ( const sha512& h1, const sha512& h2 ) {
return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
}

void to_variant( const sha512& bi, variant& v )
{
v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
Expand Down