safer sha256 binary constructor

This commit is contained in:
Tzadik Vanderhoof 2014-02-24 02:08:48 -05:00
parent 57769e4df9
commit 0761d32d01
2 changed files with 7 additions and 2 deletions

View file

@ -11,7 +11,7 @@ class sha256
public:
sha256();
explicit sha256( const string& hex_str );
explicit sha256( const char *data );
explicit sha256( const char *data, size_t size );
string str()const;
operator string()const;

View file

@ -4,11 +4,16 @@
#include <string.h>
#include <fc/crypto/sha256.hpp>
#include <fc/variant.hpp>
#include <fc/exception/exception.hpp>
namespace fc {
sha256::sha256() { memset( _hash, 0, sizeof(_hash) ); }
sha256::sha256( const char *data ) { memcpy(_hash, data, 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) );
}