BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
ripemd160.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <boost/endian/buffers.hpp>
3 #include <fc/fwd.hpp>
4 #include <fc/io/raw_fwd.hpp>
6 
7 namespace fc{
8 class sha512;
9 class sha256;
10 
11 class ripemd160
12 {
13  public:
14  ripemd160();
15  explicit ripemd160( const string& hex_str );
16 
17  string str()const;
18  explicit operator string()const;
19 
20  char* data()const;
21  static constexpr size_t data_size() { return 160/8; }
22 
23  static ripemd160 hash( const fc::sha512& h );
24  static ripemd160 hash( const fc::sha256& h );
25  static ripemd160 hash( const char* d, uint32_t dlen );
26  static ripemd160 hash( const string& );
27 
28  template<typename T>
29  static ripemd160 hash( const T& t )
30  {
32  fc::raw::pack(e,t);
33  return e.result();
34  }
35 
36  class encoder
37  {
38  public:
39  encoder();
40  ~encoder();
41 
42  void write( const char* d, uint32_t dlen );
43  void put( char c ) { write( &c, 1 ); }
44  void reset();
45  ripemd160 result();
46 
47  private:
48  class impl;
50  };
51 
52  template<typename T>
53  inline friend T& operator<<( T& ds, const ripemd160& ep ) {
54  ds.write( ep.data(), sizeof(ep) );
55  return ds;
56  }
57 
58  template<typename T>
59  inline friend T& operator>>( T& ds, ripemd160& ep ) {
60  ds.read( ep.data(), sizeof(ep) );
61  return ds;
62  }
63  friend ripemd160 operator << ( const ripemd160& h1, uint32_t i );
64  friend bool operator == ( const ripemd160& h1, const ripemd160& h2 );
65  friend bool operator != ( const ripemd160& h1, const ripemd160& h2 );
66  friend ripemd160 operator ^ ( const ripemd160& h1, const ripemd160& h2 );
67  friend bool operator >= ( const ripemd160& h1, const ripemd160& h2 );
68  friend bool operator > ( const ripemd160& h1, const ripemd160& h2 );
69  friend bool operator < ( const ripemd160& h1, const ripemd160& h2 );
70 
71  boost::endian::little_uint32_buf_t _hash[5];
72 };
73 
74 namespace raw {
75 
76  template<typename T>
77  inline void pack( T& ds, const ripemd160& ep, uint32_t _max_depth ) {
78  ds << ep;
79  }
80 
81  template<typename T>
82  inline void unpack( T& ds, ripemd160& ep, uint32_t _max_depth ) {
83  ds >> ep;
84  }
85 
86 }
87 
88  class variant;
89  void to_variant( const ripemd160& bi, variant& v, uint32_t max_depth );
90  void from_variant( const variant& v, ripemd160& bi, uint32_t max_depth );
91 
93  typedef ripemd160 uint160;
94 
95  template<> struct get_typename<uint160_t> { static const char* name() { return "uint160_t"; } };
96 
97 } // namespace fc
98 
99 namespace std
100 {
101  template<>
102  struct hash<fc::ripemd160>
103  {
104  size_t operator()( const fc::ripemd160& s )const
105  {
106  return *((size_t*)&s);
107  }
108  };
109 }
Used to forward declare value types.
Definition: fwd.hpp:10
boost::endian::little_uint32_buf_t _hash[5]
Definition: ripemd160.hpp:71
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
static constexpr size_t data_size()
Definition: ripemd160.hpp:21
void pack(T &ds, const ripemd160 &ep, uint32_t _max_depth)
Definition: ripemd160.hpp:77
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
void unpack(T &ds, ripemd160 &ep, uint32_t _max_depth)
Definition: ripemd160.hpp:82
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
static const char * name()
Definition: ripemd160.hpp:95
static ripemd160 hash(const T &t)
Definition: ripemd160.hpp:29
ripemd160 uint160_t
Definition: ripemd160.hpp:92
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
friend T & operator>>(T &ds, ripemd160 &ep)
Definition: ripemd160.hpp:59
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
ripemd160 uint160
Definition: ripemd160.hpp:93
size_t operator()(const fc::ripemd160 &s) const
Definition: ripemd160.hpp:104