BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
ripemd160.cpp
Go to the documentation of this file.
1 #include <fc/crypto/hex.hpp>
2 #include <fc/fwd_impl.hpp>
3 #include <openssl/sha.h>
4 #include <openssl/ripemd.h>
5 #include <string.h>
7 #include <fc/crypto/sha512.hpp>
8 #include <fc/crypto/sha256.hpp>
9 #include <fc/variant.hpp>
10 #include <vector>
11 #include "_digest_common.hpp"
12 
13 namespace fc
14 {
15 
16 ripemd160::ripemd160() { memset( _hash, 0, sizeof(_hash) ); }
17 ripemd160::ripemd160( const string& hex_str ) {
18  fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
19 }
20 
21 string ripemd160::str()const {
22  return fc::to_hex( (char*)_hash, sizeof(_hash) );
23 }
24 ripemd160::operator string()const { return str(); }
25 
26 char* ripemd160::data()const { return (char*)&_hash[0]; }
27 
28 
30 public:
31  impl()
32  {
33  memset( (char*)&ctx, 0, sizeof(ctx) );
34  }
35  RIPEMD160_CTX ctx;
36 };
37 
40  reset();
41 }
42 
44 {
45  return hash( (const char*)&h, sizeof(h) );
46 }
48 {
49  return hash( (const char*)&h, sizeof(h) );
50 }
51 ripemd160 ripemd160::hash( const char* d, uint32_t dlen ) {
52  encoder e;
53  e.write(d,dlen);
54  return e.result();
55 }
56 ripemd160 ripemd160::hash( const string& s ) {
57  return hash( s.c_str(), s.size() );
58 }
59 
60 void ripemd160::encoder::write( const char* d, uint32_t dlen ) {
61  RIPEMD160_Update( &my->ctx, d, dlen);
62 }
64  ripemd160 h;
65  RIPEMD160_Final((uint8_t*)h.data(), &my->ctx );
66  return h;
67 }
69  RIPEMD160_Init( &my->ctx);
70 }
71 
72 ripemd160 operator << ( const ripemd160& h1, uint32_t i ) {
73  ripemd160 result;
74  fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
75  return result;
76 }
77 ripemd160 operator ^ ( const ripemd160& h1, const ripemd160& h2 ) {
78  ripemd160 result;
79  result._hash[0] = h1._hash[0].value() ^ h2._hash[0].value();
80  result._hash[1] = h1._hash[1].value() ^ h2._hash[1].value();
81  result._hash[2] = h1._hash[2].value() ^ h2._hash[2].value();
82  result._hash[3] = h1._hash[3].value() ^ h2._hash[3].value();
83  result._hash[4] = h1._hash[4].value() ^ h2._hash[4].value();
84  return result;
85 }
86 bool operator >= ( const ripemd160& h1, const ripemd160& h2 ) {
87  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) >= 0;
88 }
89 bool operator > ( const ripemd160& h1, const ripemd160& h2 ) {
90  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) > 0;
91 }
92 bool operator < ( const ripemd160& h1, const ripemd160& h2 ) {
93  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) < 0;
94 }
95 bool operator != ( const ripemd160& h1, const ripemd160& h2 ) {
96  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) != 0;
97 }
98 bool operator == ( const ripemd160& h1, const ripemd160& h2 ) {
99  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
100 }
101 
102  void to_variant( const ripemd160& 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, ripemd160& 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 } // fc
boost::endian::little_uint32_buf_t _hash[5]
Definition: ripemd160.hpp:71
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: ripemd160.hpp:21
friend T & operator<<(T &ds, const ripemd160 &ep)
Definition: ripemd160.hpp:53
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
friend bool operator<(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:92
char * data() const
Definition: ripemd160.cpp:26
friend bool operator>=(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:86
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
string str() const
Definition: ripemd160.cpp:21
friend bool operator==(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:98
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
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: ripemd160.cpp:60
Definition: api.hpp:15
friend bool operator!=(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:95
friend bool operator>(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:89
friend ripemd160 operator^(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:77
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17