BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
hash160.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 jmjatlanta and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 #include <boost/endian/buffers.hpp>
26 #include <fc/fwd.hpp>
27 #include <fc/string.hpp>
28 #include <fc/io/raw_fwd.hpp>
29 
30 namespace fc{
31 
32 class hash160
33 {
34  public:
35  hash160();
36  explicit hash160( const string& hex_str );
37 
38  string str()const;
39  explicit operator string()const;
40 
41  char* data() const;
42  static constexpr size_t data_size() { return 160/8; }
43 
44  static hash160 hash( const char* d, uint32_t dlen );
45  static hash160 hash( const string& );
46 
47  template<typename T>
48  static hash160 hash( const T& t )
49  {
51  fc::raw::pack(e,t);
52  return e.result();
53  }
54 
55  class encoder
56  {
57  public:
58  encoder();
59  ~encoder();
60 
61  void write( const char* d, uint32_t dlen );
62  void put( char c ) { write( &c, 1 ); }
63  void reset();
64  hash160 result();
65 
66  private:
67  class impl;
69  };
70 
71  template<typename T>
72  inline friend T& operator<<( T& ds, const hash160& ep ) {
73  ds.write( ep.data(), sizeof(ep) );
74  return ds;
75  }
76 
77  template<typename T>
78  inline friend T& operator>>( T& ds, hash160& ep ) {
79  ds.read( ep.data(), sizeof(ep) );
80  return ds;
81  }
82  friend hash160 operator << ( const hash160& h1, uint32_t i );
83  friend bool operator == ( const hash160& h1, const hash160& h2 );
84  friend bool operator != ( const hash160& h1, const hash160& h2 );
85  friend hash160 operator ^ ( const hash160& h1, const hash160& h2 );
86  friend bool operator >= ( const hash160& h1, const hash160& h2 );
87  friend bool operator > ( const hash160& h1, const hash160& h2 );
88  friend bool operator < ( const hash160& h1, const hash160& h2 );
89 
90  boost::endian::little_uint32_buf_t _hash[5];
91 };
92 
93 namespace raw {
94 
95  template<typename T>
96  inline void pack( T& ds, const hash160& ep, uint32_t _max_depth ) {
97  ds << ep;
98  }
99 
100  template<typename T>
101  inline void unpack( T& ds, hash160& ep, uint32_t _max_depth ) {
102  ds >> ep;
103  }
104 
105 }
106 
107  class variant;
108  void to_variant( const hash160& bi, variant& v, uint32_t max_depth );
109  void from_variant( const variant& v, hash160& bi, uint32_t max_depth );
110 
111  template<> struct get_typename<hash160> { static const char* name() { return "hash160"; } };
112 } // namespace fc
113 
114 namespace std
115 {
116  template<>
117  struct hash<fc::hash160>
118  {
119  size_t operator()( const fc::hash160& s )const
120  {
121  return *((size_t*)&s);
122  }
123  };
124 }
Used to forward declare value types.
Definition: fwd.hpp:10
string str() const
Definition: hash160.cpp:46
static const char * name()
Definition: hash160.hpp:111
static hash160 hash(const char *d, uint32_t dlen)
Definition: hash160.cpp:61
void pack(T &ds, const hash160 &ep, uint32_t _max_depth)
Definition: hash160.hpp:96
friend T & operator<<(T &ds, const hash160 &ep)
Definition: hash160.hpp:72
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
void write(const char *d, uint32_t dlen)
Definition: hash160.cpp:71
boost::endian::little_uint32_buf_t _hash[5]
Definition: hash160.hpp:90
friend bool operator>(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:114
friend bool operator<(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:118
friend T & operator>>(T &ds, hash160 &ep)
Definition: hash160.hpp:78
friend bool operator!=(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:122
static constexpr size_t data_size()
Definition: hash160.hpp:42
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
friend bool operator==(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:126
size_t operator()(const fc::hash160 &s) const
Definition: hash160.hpp:119
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
void unpack(T &ds, hash160 &ep, uint32_t _max_depth)
Definition: hash160.hpp:101
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
static hash160 hash(const T &t)
Definition: hash160.hpp:48
friend bool operator>=(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:110
Definition: api.hpp:15
char * data() const
Definition: hash160.cpp:52
friend hash160 operator^(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:100
hash160 result()
Definition: hash160.cpp:76
void put(char c)
Definition: hash160.hpp:62