BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
sha256.cpp
Go to the documentation of this file.
1 #include <fc/crypto/hex.hpp>
2 #include <fc/crypto/hmac.hpp>
3 #include <fc/fwd_impl.hpp>
4 #include <openssl/sha.h>
5 #include <string.h>
6 #include <cmath>
7 #include <fc/crypto/sha256.hpp>
8 #include <fc/variant.hpp>
10 #include "_digest_common.hpp"
11 
12 namespace fc {
13 
14  sha256::sha256() { memset( _hash, 0, sizeof(_hash) ); }
15  sha256::sha256( const char *data, size_t size ) {
16  if (size != sizeof(_hash))
17  FC_THROW_EXCEPTION( exception, "sha256: size mismatch" );
18  memcpy(_hash, data, size );
19  }
20  sha256::sha256( const string& hex_str ) {
21  fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
22  }
23 
24  string sha256::str()const {
25  return fc::to_hex( (char*)_hash, sizeof(_hash) );
26  }
27  sha256::operator string()const { return str(); }
28 
29  char* sha256::data()const { return (char*)&_hash[0]; }
30 
31 
33  SHA256_CTX ctx;
34  };
35 
38  reset();
39  }
40 
41  sha256 sha256::hash( const char* d, uint32_t dlen ) {
42  encoder e;
43  e.write(d,dlen);
44  return e.result();
45  }
46 
47  sha256 sha256::hash( const string& s ) {
48  return hash( s.c_str(), s.size() );
49  }
50 
52  {
53  return hash( s.data(), sizeof( s._hash ) );
54  }
55 
56  void sha256::encoder::write( const char* d, uint32_t dlen ) {
57  SHA256_Update( &my->ctx, d, dlen);
58  }
60  sha256 h;
61  SHA256_Final((uint8_t*)h.data(), &my->ctx );
62  return h;
63  }
65  SHA256_Init( &my->ctx);
66  }
67 
68  sha256 operator << ( const sha256& h1, uint32_t i ) {
69  sha256 result;
70  fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
71  return result;
72  }
73  sha256 operator >> ( const sha256& h1, uint32_t i ) {
74  sha256 result;
75  fc::detail::shift_r( h1.data(), result.data(), result.data_size(), i );
76  return result;
77  }
78  sha256 operator ^ ( const sha256& h1, const sha256& h2 ) {
79  sha256 result;
80  result._hash[0] = h1._hash[0].value() ^ h2._hash[0].value();
81  result._hash[1] = h1._hash[1].value() ^ h2._hash[1].value();
82  result._hash[2] = h1._hash[2].value() ^ h2._hash[2].value();
83  result._hash[3] = h1._hash[3].value() ^ h2._hash[3].value();
84  return result;
85  }
86  bool operator >= ( const sha256& h1, const sha256& h2 ) {
87  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) >= 0;
88  }
89  bool operator > ( const sha256& h1, const sha256& h2 ) {
90  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) > 0;
91  }
92  bool operator < ( const sha256& h1, const sha256& h2 ) {
93  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) < 0;
94  }
95  bool operator != ( const sha256& h1, const sha256& h2 ) {
96  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) != 0;
97  }
98  bool operator == ( const sha256& h1, const sha256& h2 ) {
99  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
100  }
101 
102  void to_variant( const sha256& bi, variant& v, uint32_t max_depth )
103  {
104  to_variant( std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) ), v, max_depth );
105  }
106  void from_variant( const variant& v, sha256& bi, uint32_t max_depth )
107  {
108  std::vector<char> ve = v.as< std::vector<char> >( max_depth );
109  memset( &bi, char(0), sizeof(bi) );
110  if( ve.size() )
111  memcpy( &bi, ve.data(), std::min<size_t>(ve.size(),sizeof(bi)) );
112  }
113 
114  template<>
115  unsigned int hmac<sha256>::internal_block_size() const { return 64; }
116 } //end namespace fc
friend T & operator<<(T &ds, const sha256 &ep)
Definition: sha256.hpp:52
friend bool operator<(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:92
friend bool operator>(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:89
uint8_t from_hex(char c)
Definition: hex.cpp:6
T as(uint32_t max_depth) const
Definition: variant.hpp:336
static constexpr size_t data_size()
Definition: sha256.hpp:21
void write(const char *d, uint32_t dlen)
Definition: sha256.cpp:56
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
sha256 result()
Definition: sha256.cpp:59
static sha256 hash(const char *d, uint32_t dlen)
Definition: sha256.cpp:41
boost::endian::little_uint64_buf_t _hash[4]
Definition: sha256.hpp:71
char * data() const
Definition: sha256.cpp:29
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Definition: exception.hpp:378
string str() const
Definition: sha256.cpp:24
friend T & operator>>(T &ds, sha256 &ep)
Definition: sha256.hpp:58
friend bool operator>=(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:86
friend sha256 operator^(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:78
Defines exception&#39;s used by fc.
friend bool operator!=(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:95
friend bool operator==(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:98
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
Definition: api.hpp:15
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17