BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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
fc::ripemd160::encoder::write
void write(const char *d, uint32_t dlen)
Definition: ripemd160.cpp:60
fc::from_hex
uint8_t from_hex(char c)
Definition: hex.cpp:6
fc::ripemd160::operator<<
friend T & operator<<(T &ds, const ripemd160 &ep)
Definition: ripemd160.hpp:53
fc::ripemd160::encoder::impl
Definition: ripemd160.cpp:29
fc
Definition: api.hpp:15
fc::ripemd160::encoder::reset
void reset()
Definition: ripemd160.cpp:68
hex.hpp
fc::sha256
Definition: sha256.hpp:10
fc::ripemd160::data
char * data() const
Definition: ripemd160.cpp:26
fc::ripemd160::ripemd160
ripemd160()
Definition: ripemd160.cpp:16
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::ripemd160::_hash
boost::endian::little_uint32_buf_t _hash[5]
Definition: ripemd160.hpp:71
fc::to_hex
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
sha512.hpp
fc::ripemd160::operator<
friend bool operator<(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:92
fc::sha512
Definition: sha512.hpp:9
_digest_common.hpp
fc::ripemd160::encoder::impl::impl
impl()
Definition: ripemd160.cpp:31
fc::ripemd160::encoder::result
ripemd160 result()
Definition: ripemd160.cpp:63
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
fc::ripemd160
Definition: ripemd160.hpp:11
fc::ripemd160::encoder
Definition: ripemd160.hpp:36
fc::ripemd160::str
string str() const
Definition: ripemd160.cpp:21
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::ripemd160::encoder::impl::ctx
RIPEMD160_CTX ctx
Definition: ripemd160.cpp:35
fc::ripemd160::operator==
friend bool operator==(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:98
ripemd160.hpp
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::ripemd160::operator>
friend bool operator>(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:89
fc::ripemd160::encoder::encoder
encoder()
Definition: ripemd160.cpp:39
fc::ripemd160::operator>=
friend bool operator>=(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:86
fc::ripemd160::hash
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
fc::ripemd160::encoder::~encoder
~encoder()
Definition: ripemd160.cpp:38
variant.hpp
fc::ripemd160::data_size
static constexpr size_t data_size()
Definition: ripemd160.hpp:21
fc::ripemd160::operator^
friend ripemd160 operator^(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:77
fc::ripemd160::operator!=
friend bool operator!=(const ripemd160 &h1, const ripemd160 &h2)
Definition: ripemd160.cpp:95
fwd_impl.hpp
sha256.hpp