BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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
fc::sha256::encoder::impl::ctx
SHA256_CTX ctx
Definition: sha256.cpp:33
fc::exception
Used to generate a useful error report when an exception is thrown.
Definition: exception.hpp:56
fc::from_hex
uint8_t from_hex(char c)
Definition: hex.cpp:6
fc::sha256::encoder::write
void write(const char *d, uint32_t dlen)
Definition: sha256.cpp:56
fc
Definition: api.hpp:15
hex.hpp
fc::sha256::operator<
friend bool operator<(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:92
fc::sha256
Definition: sha256.hpp:10
fc::sha256::data_size
static constexpr size_t data_size()
Definition: sha256.hpp:21
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::to_hex
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
_digest_common.hpp
fc::sha256::operator>>
friend T & operator>>(T &ds, sha256 &ep)
Definition: sha256.hpp:58
fc::sha256::encoder::result
sha256 result()
Definition: sha256.cpp:59
fc::sha256::operator!=
friend bool operator!=(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:95
fc::sha256::operator==
friend bool operator==(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:98
fc::sha256::data
char * data() const
Definition: sha256.cpp:29
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
fc::sha256::operator>
friend bool operator>(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:89
fc::sha256::encoder::reset
void reset()
Definition: sha256.cpp:64
fc::sha256::_hash
boost::endian::little_uint64_buf_t _hash[4]
Definition: sha256.hpp:71
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::sha256::str
string str() const
Definition: sha256.cpp:24
fc::sha256::operator<<
friend T & operator<<(T &ds, const sha256 &ep)
Definition: sha256.hpp:52
fc::sha256::encoder::~encoder
~encoder()
Definition: sha256.cpp:36
fc::sha256::encoder::impl
Definition: sha256.cpp:32
fc::sha256::encoder
Definition: sha256.hpp:35
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
exception.hpp
Defines exception's used by fc.
hmac.hpp
fc::sha256::encoder::encoder
encoder()
Definition: sha256.cpp:37
fc::sha256::sha256
sha256()
Definition: sha256.cpp:14
variant.hpp
fc::sha256::operator>=
friend bool operator>=(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:86
fc::sha256::operator^
friend sha256 operator^(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:78
FC_THROW_EXCEPTION
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Definition: exception.hpp:379
fwd_impl.hpp
fc::sha256::hash
static sha256 hash(const char *d, uint32_t dlen)
Definition: sha256.cpp:41
sha256.hpp