BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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>
fc::sha1::operator^
friend sha1 operator^(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:61
fc::sha1::hash
static sha1 hash(const T &t)
Definition: sha1.hpp:26
fc::sha1::encoder
Definition: sha1.hpp:33
fc::sha1::encoder::reset
void reset()
Definition: sha1.cpp:52
fc::sha1::_hash
boost::endian::little_uint32_buf_t _hash[5]
Definition: sha1.hpp:68
fc::sha1::operator>
friend bool operator>(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:73
fc::sha1::operator>=
friend bool operator>=(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:70
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::sha1::operator==
friend bool operator==(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:82
fc::sha1::operator!=
friend bool operator!=(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:79
fc::sha1::data_size
static constexpr size_t data_size()
Definition: sha1.hpp:20
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::sha1::encoder::result
sha1 result()
Definition: sha1.cpp:47
reflect.hpp
Defines types and macros used to provide reflection.
fc::sha1::operator>>
friend T & operator>>(T &ds, sha1 &ep)
Definition: sha1.hpp:56
std::hash< fc::sha1 >::operator()
size_t operator()(const fc::sha1 &s) const
Definition: sha1.hpp:96
fc::sha1::hash
static sha1 hash(const char *d, uint32_t dlen)
Definition: sha1.cpp:35
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::sha1::str
std::string str() const
Definition: sha1.cpp:18
fc::sha1::operator<
friend bool operator<(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:76
fc::sha1
Definition: sha1.hpp:10
std
Definition: zeroed_array.hpp:76
fc::sha1::sha1
sha1()
Definition: sha1.cpp:13
fc::sha1::encoder::~encoder
~encoder()
Definition: sha1.cpp:30
fc::sha1::encoder::encoder
encoder()
Definition: sha1.cpp:31
fc::sha1::encoder::put
void put(char c)
Definition: sha1.hpp:40
fc::sha1::operator<<
friend T & operator<<(T &ds, const sha1 &ep)
Definition: sha1.hpp:50
fc::sha1::data
char * data() const
Definition: sha1.cpp:23
fc::fwd
Used to forward declare value types.
Definition: fwd.hpp:10
fc::sha1::encoder::write
void write(const char *d, uint32_t dlen)
Definition: sha1.cpp:44
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11