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