BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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>
fc::sha256::encoder::write
void write(const char *d, uint32_t dlen)
Definition: sha256.cpp:56
FC_REFLECT_TYPENAME
FC_REFLECT_TYPENAME(fc::log_message)
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
fwd.hpp
fc
Definition: api.hpp:15
fc::sha256::operator<
friend bool operator<(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:92
fc::sha256
Definition: sha256.hpp:10
fc::uint256
sha256 uint256
Definition: sha256.hpp:88
fc::sha256::data_size
static constexpr size_t data_size()
Definition: sha256.hpp:21
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::sha256::hash
static sha256 hash(const T &t)
Definition: sha256.hpp:28
reflect.hpp
Defines types and macros used to provide reflection.
string.hpp
fc::sha256::operator>>
friend T & operator>>(T &ds, sha256 &ep)
Definition: sha256.hpp:58
fc::sha256::encoder::result
sha256 result()
Definition: sha256.cpp:59
std::hash< fc::sha256 >::operator()
size_t operator()(const fc::sha256 &s) const
Definition: sha256.hpp:100
fc::sha256::operator!=
friend bool operator!=(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:95
fc::sha256::operator==
friend bool operator==(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:98
fc::sha256::data
char * data() const
Definition: sha256.cpp:29
fc::sha256::operator>
friend bool operator>(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:89
fc::sha256::encoder::reset
void reset()
Definition: sha256.cpp:64
fc::sha256::_hash
boost::endian::little_uint64_buf_t _hash[4]
Definition: sha256.hpp:71
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::sha256::str
string str() const
Definition: sha256.cpp:24
fc::sha256::operator<<
friend T & operator<<(T &ds, const sha256 &ep)
Definition: sha256.hpp:52
fc::sha256::encoder::~encoder
~encoder()
Definition: sha256.cpp:36
fc::sha256::encoder
Definition: sha256.hpp:35
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
std
Definition: zeroed_array.hpp:76
fc::sha256::encoder::encoder
encoder()
Definition: sha256.cpp:37
raw_fwd.hpp
fc::sha256::sha256
sha256()
Definition: sha256.cpp:14
fc::sha256::encoder::put
void put(char c)
Definition: sha256.hpp:42
fc::sha256::operator>=
friend bool operator>=(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:86
fc::sha256::operator^
friend sha256 operator^(const sha256 &h1, const sha256 &h2)
Definition: sha256.cpp:78
fc::sha256::hash
static sha256 hash(const char *d, uint32_t dlen)
Definition: sha256.cpp:41
fc::fwd
Used to forward declare value types.
Definition: fwd.hpp:10
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11