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