BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
hash160.cpp
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 
25 #include <fc/crypto/hex.hpp>
26 #include <fc/crypto/hmac.hpp>
27 #include <fc/fwd_impl.hpp>
28 #include <openssl/sha.h>
29 #include <openssl/ripemd.h>
30 #include <string.h>
31 #include <cmath>
32 #include <fc/crypto/hash160.hpp>
33 #include <fc/variant.hpp>
35 #include "_digest_common.hpp"
36 
37 namespace fc
38 {
39 
40 hash160::hash160() { memset( _hash, 0, sizeof(_hash) ); }
41 
42 hash160::hash160( const string& hex_str ) {
43  fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
44 }
45 
46 string hash160::str()const {
47  return fc::to_hex( (char*)_hash, sizeof(_hash) );
48 }
49 
50 hash160::operator string()const { return str(); }
51 
52 char* hash160::data()const { return (char*)&_hash[0]; }
53 
55  SHA256_CTX ctx;
56 };
57 
59 hash160::encoder::encoder() { SHA256_Init(&my->ctx); }
60 
61 hash160 hash160::hash( const char* d, uint32_t dlen ) {
62  encoder e;
63  e.write(d,dlen);
64  return e.result();
65 }
66 
67 hash160 hash160::hash( const string& s ) {
68  return hash( s.c_str(), s.size() );
69 }
70 
71 void hash160::encoder::write( const char* d, uint32_t dlen )
72 {
73  SHA256_Update( &my->ctx, d, dlen);
74 }
75 
77  // finalize the first hash
78  unsigned char sha_hash[SHA256_DIGEST_LENGTH];
79  SHA256_Final( sha_hash, &my->ctx );
80  // perform the second hashing function
81  RIPEMD160_CTX ripe_ctx;
82  RIPEMD160_Init(&ripe_ctx);
83  RIPEMD160_Update( &ripe_ctx, sha_hash, SHA256_DIGEST_LENGTH );
84  hash160 h;
85  RIPEMD160_Final( (uint8_t *)h.data(), &ripe_ctx );
86  return h;
87 }
88 
90 {
91  SHA256_Init(&my->ctx);
92 }
93 
94 hash160 operator << ( const hash160& h1, uint32_t i ) {
95  hash160 result;
96  fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
97  return result;
98 }
99 
100 hash160 operator ^ ( const hash160& h1, const hash160& h2 ) {
101  hash160 result;
102  result._hash[0] = h1._hash[0].value() ^ h2._hash[0].value();
103  result._hash[1] = h1._hash[1].value() ^ h2._hash[1].value();
104  result._hash[2] = h1._hash[2].value() ^ h2._hash[2].value();
105  result._hash[3] = h1._hash[3].value() ^ h2._hash[3].value();
106  result._hash[4] = h1._hash[4].value() ^ h2._hash[4].value();
107  return result;
108 }
109 
110 bool operator >= ( const hash160& h1, const hash160& h2 ) {
111  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) >= 0;
112 }
113 
114 bool operator > ( const hash160& h1, const hash160& h2 ) {
115  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) > 0;
116 }
117 
118 bool operator < ( const hash160& h1, const hash160& h2 ) {
119  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) < 0;
120 }
121 
122 bool operator != ( const hash160& h1, const hash160& h2 ) {
123  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) != 0;
124 }
125 
126 bool operator == ( const hash160& h1, const hash160& h2 ) {
127  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
128 }
129 
130 void to_variant( const hash160& bi, variant& v, uint32_t max_depth )
131 {
132  to_variant( std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) ), v, max_depth );
133 }
134 
135 void from_variant( const variant& v, hash160& bi, uint32_t max_depth )
136 {
137  std::vector<char> ve = v.as< std::vector<char> >( max_depth );
138  memset( &bi, char(0), sizeof(bi) );
139  if( ve.size() )
140  memcpy( &bi, ve.data(), std::min<size_t>(ve.size(),sizeof(bi)) );
141 }
142 
143 } // fc
fc::hash160::encoder::~encoder
~encoder()
Definition: hash160.cpp:58
fc::hash160::data
char * data() const
Definition: hash160.cpp:52
fc::hash160::encoder::impl
Definition: hash160.cpp:54
fc::hash160::data_size
static constexpr size_t data_size()
Definition: hash160.hpp:42
fc::hash160::str
string str() const
Definition: hash160.cpp:46
fc::from_hex
uint8_t from_hex(char c)
Definition: hex.cpp:6
fc::hash160::operator<
friend bool operator<(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:118
fc
Definition: api.hpp:15
hex.hpp
fc::hash160::encoder::impl::ctx
SHA256_CTX ctx
Definition: hash160.cpp:55
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::hash160::encoder::encoder
encoder()
Definition: hash160.cpp:59
fc::hash160::encoder::result
hash160 result()
Definition: hash160.cpp:76
fc::to_hex
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
_digest_common.hpp
fc::hash160::encoder::reset
void reset()
Definition: hash160.cpp:89
fc::hash160::operator^
friend hash160 operator^(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:100
fc::hash160::operator==
friend bool operator==(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:126
hash160.hpp
fc::hash160::hash
static hash160 hash(const char *d, uint32_t dlen)
Definition: hash160.cpp:61
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::hash160::encoder::write
void write(const char *d, uint32_t dlen)
Definition: hash160.cpp:71
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
exception.hpp
Defines exception's used by fc.
hmac.hpp
fc::hash160::operator<<
friend T & operator<<(T &ds, const hash160 &ep)
Definition: hash160.hpp:72
fc::hash160::operator!=
friend bool operator!=(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:122
fc::hash160::hash160
hash160()
Definition: hash160.cpp:40
variant.hpp
fc::hash160
Definition: hash160.hpp:32
fc::hash160::operator>
friend bool operator>(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:114
fc::hash160::operator>=
friend bool operator>=(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:110
fc::hash160::_hash
boost::endian::little_uint32_buf_t _hash[5]
Definition: hash160.hpp:90
fwd_impl.hpp
fc::hash160::encoder
Definition: hash160.hpp:55