BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
sha224.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 <fc/crypto/sha224.hpp>
7 #include <fc/variant.hpp>
8 #include "_digest_common.hpp"
9 
10 namespace fc {
11 
12  sha224::sha224() { memset( _hash, 0, sizeof(_hash) ); }
13  sha224::sha224( const string& hex_str ) {
14  fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
15  }
16 
17  string sha224::str()const {
18  return fc::to_hex( (char*)_hash, sizeof(_hash) );
19  }
20  sha224::operator string()const { return str(); }
21 
22  char* sha224::data()const { return (char*)&_hash[0]; }
23 
24 
26  SHA256_CTX ctx;
27  };
28 
31  reset();
32  }
33 
34  sha224 sha224::hash( const char* d, uint32_t dlen ) {
35  encoder e;
36  e.write(d,dlen);
37  return e.result();
38  }
39  sha224 sha224::hash( const string& s ) {
40  return hash( s.c_str(), s.size() );
41  }
42 
43  void sha224::encoder::write( const char* d, uint32_t dlen ) {
44  SHA224_Update( &my->ctx, d, dlen);
45  }
47  sha224 h;
48  SHA224_Final((uint8_t*)h.data(), &my->ctx );
49  return h;
50  }
52  SHA224_Init( &my->ctx);
53  }
54 
55  sha224 operator << ( const sha224& h1, uint32_t i ) {
56  sha224 result;
57  fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
58  return result;
59  }
60  sha224 operator ^ ( const sha224& h1, const sha224& h2 ) {
61  sha224 result;
62  for( uint32_t i = 0; i < 7; ++i )
63  result._hash[i] = h1._hash[i].value() ^ h2._hash[i].value();
64  return result;
65  }
66  bool operator >= ( const sha224& h1, const sha224& h2 ) {
67  return memcmp( h1._hash, h2._hash, sizeof(sha224) ) >= 0;
68  }
69  bool operator > ( const sha224& h1, const sha224& h2 ) {
70  return memcmp( h1._hash, h2._hash, sizeof(sha224) ) > 0;
71  }
72  bool operator < ( const sha224& h1, const sha224& h2 ) {
73  return memcmp( h1._hash, h2._hash, sizeof(sha224) ) < 0;
74  }
75  bool operator != ( const sha224& h1, const sha224& h2 ) {
76  return memcmp( h1._hash, h2._hash, sizeof(sha224) ) != 0;
77  }
78  bool operator == ( const sha224& h1, const sha224& h2 ) {
79  return memcmp( h1._hash, h2._hash, sizeof(sha224) ) == 0;
80  }
81 
82  void to_variant( const sha224& bi, variant& v, uint32_t max_depth )
83  {
84  to_variant( std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) ), v, max_depth );
85  }
86  void from_variant( const variant& v, sha224& bi, uint32_t max_depth )
87  {
88  std::vector<char> ve = v.as< std::vector<char> >( max_depth );
89  memset( &bi, char(0), sizeof(bi) );
90  if( ve.size() )
91  memcpy( &bi, ve.data(), std::min<size_t>(ve.size(),sizeof(bi)) );
92  }
93 
94  template<>
95  unsigned int hmac<sha224>::internal_block_size() const { return 64; }
96 }
friend bool operator!=(const sha224 &h1, const sha224 &h2)
Definition: sha224.cpp:75
uint8_t from_hex(char c)
Definition: hex.cpp:6
T as(uint32_t max_depth) const
Definition: variant.hpp:336
friend bool operator==(const sha224 &h1, const sha224 &h2)
Definition: sha224.cpp:78
char * data() const
Definition: sha224.cpp:22
friend bool operator<(const sha224 &h1, const sha224 &h2)
Definition: sha224.cpp:72
static sha224 hash(const char *d, uint32_t dlen)
Definition: sha224.cpp:34
boost::endian::little_uint32_buf_t _hash[7]
Definition: sha224.hpp:68
friend bool operator>(const sha224 &h1, const sha224 &h2)
Definition: sha224.cpp:69
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
string str() const
Definition: sha224.cpp:17
friend sha224 operator^(const sha224 &h1, const sha224 &h2)
Definition: sha224.cpp:60
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
friend T & operator<<(T &ds, const sha224 &ep)
Definition: sha224.hpp:49
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
void write(const char *d, uint32_t dlen)
Definition: sha224.cpp:43
Definition: api.hpp:15
friend bool operator>=(const sha224 &h1, const sha224 &h2)
Definition: sha224.cpp:66
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
static constexpr size_t data_size()
Definition: sha224.hpp:19
sha224 result()
Definition: sha224.cpp:46