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