Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sha256.cpp
Go to the documentation of this file.
1#include <fc/crypto/hex.hpp>
2#include <fc/crypto/hmac.hpp>
3#include <fc/fwd_impl.hpp>
4#include <openssl/sha.h>
5#include <string.h>
6#include <cmath>
8#include <fc/variant.hpp>
10#include "_digest_common.hpp"
11
12namespace fc {
13
14 sha256::sha256() { memset( _hash, 0, sizeof(_hash) ); }
15 sha256::sha256( const char *data, size_t size ) {
16 if (size != sizeof(_hash))
17 FC_THROW_EXCEPTION( exception, "sha256: size mismatch" );
18 memcpy(_hash, data, size );
19 }
20 sha256::sha256( const string& hex_str ) {
21 auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
22 if( bytes_written < sizeof(_hash) )
23 memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
24 }
25
26 string sha256::str()const {
27 return fc::to_hex( (char*)_hash, sizeof(_hash) );
28 }
29 sha256::operator string()const { return str(); }
30
31 const char* sha256::data()const { return (const char*)&_hash[0]; }
32 char* sha256::data() { return (char*)&_hash[0]; }
33
34
36 SHA256_CTX ctx;
37 };
38
41 reset();
42 }
43
44 sha256 sha256::hash( const char* d, uint32_t dlen ) {
45 encoder e;
46 e.write(d,dlen);
47 return e.result();
48 }
49
50 sha256 sha256::hash( const string& s ) {
51 return hash( s.c_str(), s.size() );
52 }
53
55 {
56 return hash( s.data(), sizeof( s._hash ) );
57 }
58
59 void sha256::encoder::write( const char* d, uint32_t dlen ) {
60 SHA256_Update( &my->ctx, d, dlen);
61 }
63 sha256 h;
64 SHA256_Final((uint8_t*)h.data(), &my->ctx );
65 return h;
66 }
68 SHA256_Init( &my->ctx);
69 }
70
72 sha256 result;
73 fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
74 return result;
75 }
77 sha256 result;
78 fc::detail::shift_r( h1.data(), result.data(), result.data_size(), i );
79 return result;
80 }
81 sha256 operator ^ ( const sha256& h1, const sha256& h2 ) {
82 sha256 result;
83 result._hash[0] = h1._hash[0] ^ h2._hash[0];
84 result._hash[1] = h1._hash[1] ^ h2._hash[1];
85 result._hash[2] = h1._hash[2] ^ h2._hash[2];
86 result._hash[3] = h1._hash[3] ^ h2._hash[3];
87 return result;
88 }
89 bool operator >= ( const sha256& h1, const sha256& h2 ) {
90 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) >= 0;
91 }
92 bool operator > ( const sha256& h1, const sha256& h2 ) {
93 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) > 0;
94 }
95 bool operator < ( const sha256& h1, const sha256& h2 ) {
96 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) < 0;
97 }
98 bool operator != ( const sha256& h1, const sha256& h2 ) {
99 return !(h1 == h2);
100 }
101 bool operator == ( const sha256& h1, const sha256& h2 ) {
102 // idea to not use memcmp, from:
103 // https://lemire.me/blog/2018/08/22/avoid-lexicographical-comparisons-when-testing-for-string-equality/
104 return
105 h1._hash[0] == h2._hash[0] &&
106 h1._hash[1] == h2._hash[1] &&
107 h1._hash[2] == h2._hash[2] &&
108 h1._hash[3] == h2._hash[3];
109 }
110
112 {
113 uint16_t lzbits = clz();
114 if( lzbits >= 0x100 )
115 return 0;
116 uint8_t nzbits = 0xFF-lzbits;
117 size_t offset = (size_t) (lzbits >> 3);
118 uint8_t* my_bytes = (uint8_t*) data();
119 size_t n = data_size();
120 uint32_t y = (uint32_t( my_bytes[offset ] ) << 0x18)
121 | (uint32_t(offset+1 < n ? my_bytes[offset+1] : 0) << 0x10)
122 | (uint32_t(offset+2 < n ? my_bytes[offset+2] : 0) << 0x08)
123 | (uint32_t(offset+3 < n ? my_bytes[offset+3] : 0) )
124 ;
125 //
126 // lzbits&7 == 7 : 00000001 iff nzbits&7 == 0
127 // lzbits&7 == 6 : 0000001x iff nzbits&7 == 1
128 // lzbits&7 == 5 : 000001xx iff nzbits&7 == 2
129 //
130 y >>= (nzbits & 7);
131 y ^= 1 << 0x18;
132 y |= uint32_t( nzbits ) << 0x18;
133 return y;
134 }
135
137 {
138 uint8_t nzbits = uint8_t( x >> 0x18 );
139 _hash[0] = 0;
140 _hash[1] = 0;
141 _hash[2] = 0;
142 _hash[3] = 0;
143 if( nzbits == 0 )
144 return;
145 uint8_t x0 = uint8_t((x ) & 0xFF);
146 uint8_t x1 = uint8_t((x >> 0x08) & 0xFF);
147 uint8_t x2 = uint8_t((x >> 0x10) & 0xFF);
148 uint8_t* my_bytes = (uint8_t*) data();
149 my_bytes[0x1F] = x0;
150 my_bytes[0x1E] = x1;
151 my_bytes[0x1D] = x2;
152 my_bytes[0x1C] = 1;
153
154 if( nzbits <= 0x18 )
155 {
156 (*this) = (*this) >> (0x18 - nzbits);
157 }
158 else
159 (*this) = (*this) << (nzbits - 0x18);
160 return;
161 }
162
164 {
165 uint8_t nzbits = uint8_t( x >> 0x18 );
166 if( nzbits == 0 )
167 return 0.0;
168 uint32_t b = 1 << 0x18;
169 uint32_t y = (x & (b-1)) | b;
170 return std::ldexp( y, int( nzbits ) - 0x18 );
171 }
172
174 {
175 const uint8_t* my_bytes = (uint8_t*) data();
176 size_t size = data_size();
177 size_t lzbits = 0;
178 static const uint8_t char2lzbits[] = {
179 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
180 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
181 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
182 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
183 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
188 };
189
190 size_t i = 0;
191
192 while( true )
193 {
194 uint8_t c = my_bytes[i];
195 lzbits += char2lzbits[c];
196 if( c != 0 )
197 break;
198 ++i;
199 if( i >= size )
200 return 0x100;
201 }
202
203 return lzbits;
204 }
205
206 void to_variant( const sha256& bi, variant& v )
207 {
208 v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
209 }
210 void from_variant( const variant& v, sha256& bi )
211 {
212 std::vector<char> ve = v.as< std::vector<char> >();
213 if( ve.size() )
214 {
215 memcpy(&bi, ve.data(), fc::min<size_t>(ve.size(),sizeof(bi)) );
216 }
217 else
218 memset( &bi, char(0), sizeof(bi) );
219 }
220
221 uint64_t hash64(const char* buf, size_t len)
222 {
223 sha256 sha_value = sha256::hash(buf,len);
224 return sha_value._hash[0];
225 }
226
227 template<>
228 unsigned int hmac<sha256>::internal_block_size() const { return 64; }
229} //end namespace fc
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
void write(const char *d, uint32_t dlen)
Definition sha256.cpp:59
static double inverse_approx_log_32_double(uint32_t x)
Definition sha256.cpp:163
friend sha256 operator^(const sha256 &h1, const sha256 &h2)
Definition sha256.cpp:81
uint32_t approx_log_32() const
Definition sha256.cpp:111
void set_to_inverse_approx_log_32(uint32_t x)
Definition sha256.cpp:136
static sha256 hash(const char *d, uint32_t dlen)
Definition sha256.cpp:44
friend bool operator!=(const sha256 &h1, const sha256 &h2)
Definition sha256.cpp:98
string str() const
Definition sha256.cpp:26
friend T & operator>>(T &ds, sha256 &ep)
Definition sha256.hpp:60
const char * data() const
Definition sha256.cpp:31
size_t data_size() const
Definition sha256.hpp:23
uint64_t _hash[4]
Definition sha256.hpp:100
friend T & operator<<(T &ds, const sha256 &ep)
Definition sha256.hpp:54
friend bool operator==(const sha256 &h1, const sha256 &h2)
Definition sha256.cpp:101
friend bool operator>(const sha256 &h1, const sha256 &h2)
Definition sha256.cpp:92
friend bool operator<(const sha256 &h1, const sha256 &h2)
Definition sha256.cpp:95
friend bool operator>=(const sha256 &h1, const sha256 &h2)
Definition sha256.cpp:89
uint16_t clz() const
Definition sha256.cpp:173
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
T as() const
Definition variant.hpp:327
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
uint8_t from_hex(char c)
Definition hex.cpp:6
const T & min(const T &a, const T &b)
Definition utility.hpp:140
fc::string to_hex(const char *d, uint32_t s)
Definition hex.cpp:17
uint64_t hash64(const char *buf, size_t len)
Definition sha256.cpp:221
uint64_t y
Definition sha3.cpp:34
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
CK_ULONG d
char * s
size_t len
uint8_t buf[2048]
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)