/src/solidity/libsolutil/picosha2.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | The MIT License (MIT) |
3 | | |
4 | | Copyright (C) 2017 okdshin |
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 | | #ifndef PICOSHA2_H |
25 | | #define PICOSHA2_H |
26 | | // picosha2:20140213 |
27 | | |
28 | | #ifndef PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR |
29 | | #define PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR \ |
30 | | 1048576 //=1024*1024: default is 1MB memory |
31 | | #endif |
32 | | |
33 | | #include <algorithm> |
34 | | #include <cassert> |
35 | | #include <iterator> |
36 | | #include <sstream> |
37 | | #include <vector> |
38 | | #include <fstream> |
39 | | namespace picosha2 { |
40 | | typedef unsigned long word_t; |
41 | | typedef unsigned char byte_t; |
42 | | |
43 | | static const size_t k_digest_size = 32; |
44 | | |
45 | | namespace detail { |
46 | 11.2M | inline byte_t mask_8bit(byte_t x) { return x & 0xff; } |
47 | | |
48 | 127M | inline word_t mask_32bit(word_t x) { return x & 0xffffffff; } |
49 | | |
50 | | const word_t add_constant[64] = { |
51 | | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, |
52 | | 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
53 | | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, |
54 | | 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
55 | | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, |
56 | | 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
57 | | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, |
58 | | 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
59 | | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, |
60 | | 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
61 | | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; |
62 | | |
63 | | const word_t initial_message_digest[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, |
64 | | 0xa54ff53a, 0x510e527f, 0x9b05688c, |
65 | | 0x1f83d9ab, 0x5be0cd19}; |
66 | | |
67 | 10.7M | inline word_t ch(word_t x, word_t y, word_t z) { return (x & y) ^ ((~x) & z); } |
68 | | |
69 | 10.7M | inline word_t maj(word_t x, word_t y, word_t z) { |
70 | 10.7M | return (x & y) ^ (x & z) ^ (y & z); |
71 | 10.7M | } |
72 | | |
73 | 96.8M | inline word_t rotr(word_t x, std::size_t n) { |
74 | 96.8M | assert(n < 32); |
75 | 96.8M | return mask_32bit((x >> n) | (x << (32 - n))); |
76 | 96.8M | } |
77 | | |
78 | 10.7M | inline word_t bsig0(word_t x) { return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22); } |
79 | | |
80 | 10.7M | inline word_t bsig1(word_t x) { return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25); } |
81 | | |
82 | 16.1M | inline word_t shr(word_t x, std::size_t n) { |
83 | 16.1M | assert(n < 32); |
84 | 16.1M | return x >> n; |
85 | 16.1M | } |
86 | | |
87 | 8.06M | inline word_t ssig0(word_t x) { return rotr(x, 7) ^ rotr(x, 18) ^ shr(x, 3); } |
88 | | |
89 | 8.06M | inline word_t ssig1(word_t x) { return rotr(x, 17) ^ rotr(x, 19) ^ shr(x, 10); } |
90 | | |
91 | | template <typename RaIter1, typename RaIter2> |
92 | 168k | void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) { |
93 | 168k | assert(first + 64 == last); |
94 | 168k | static_cast<void>(last); // for avoiding unused-variable warning |
95 | 168k | word_t w[64]; |
96 | 168k | std::fill(w, w + 64, 0); |
97 | 2.85M | for (std::size_t i = 0; i < 16; ++i) { |
98 | 2.68M | w[i] = (static_cast<word_t>(mask_8bit(*(first + long(i) * 4))) << 24) | |
99 | 2.68M | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 1))) << 16) | |
100 | 2.68M | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 2))) << 8) | |
101 | 2.68M | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 3)))); |
102 | 2.68M | } |
103 | 8.23M | for (std::size_t i = 16; i < 64; ++i) { |
104 | 8.06M | w[i] = mask_32bit(ssig1(w[i - 2]) + w[i - 7] + ssig0(w[i - 15]) + |
105 | 8.06M | w[i - 16]); |
106 | 8.06M | } |
107 | | |
108 | 168k | word_t a = *message_digest; |
109 | 168k | word_t b = *(message_digest + 1); |
110 | 168k | word_t c = *(message_digest + 2); |
111 | 168k | word_t d = *(message_digest + 3); |
112 | 168k | word_t e = *(message_digest + 4); |
113 | 168k | word_t f = *(message_digest + 5); |
114 | 168k | word_t g = *(message_digest + 6); |
115 | 168k | word_t h = *(message_digest + 7); |
116 | | |
117 | 10.9M | for (std::size_t i = 0; i < 64; ++i) { |
118 | 10.7M | word_t temp1 = h + bsig1(e) + ch(e, f, g) + add_constant[i] + w[i]; |
119 | 10.7M | word_t temp2 = bsig0(a) + maj(a, b, c); |
120 | 10.7M | h = g; |
121 | 10.7M | g = f; |
122 | 10.7M | f = e; |
123 | 10.7M | e = mask_32bit(d + temp1); |
124 | 10.7M | d = c; |
125 | 10.7M | c = b; |
126 | 10.7M | b = a; |
127 | 10.7M | a = mask_32bit(temp1 + temp2); |
128 | 10.7M | } |
129 | 168k | *message_digest += a; |
130 | 168k | *(message_digest + 1) += b; |
131 | 168k | *(message_digest + 2) += c; |
132 | 168k | *(message_digest + 3) += d; |
133 | 168k | *(message_digest + 4) += e; |
134 | 168k | *(message_digest + 5) += f; |
135 | 168k | *(message_digest + 6) += g; |
136 | 168k | *(message_digest + 7) += h; |
137 | 1.51M | for (std::size_t i = 0; i < 8; ++i) { |
138 | 1.34M | *(message_digest + i) = mask_32bit(*(message_digest + i)); |
139 | 1.34M | } |
140 | 168k | } void picosha2::detail::hash256_block<unsigned long*, std::__1::__wrap_iter<unsigned char*> >(unsigned long*, std::__1::__wrap_iter<unsigned char*>, std::__1::__wrap_iter<unsigned char*>) Line | Count | Source | 92 | 150k | void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) { | 93 | 150k | assert(first + 64 == last); | 94 | 150k | static_cast<void>(last); // for avoiding unused-variable warning | 95 | 150k | word_t w[64]; | 96 | 150k | std::fill(w, w + 64, 0); | 97 | 2.55M | for (std::size_t i = 0; i < 16; ++i) { | 98 | 2.40M | w[i] = (static_cast<word_t>(mask_8bit(*(first + long(i) * 4))) << 24) | | 99 | 2.40M | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 1))) << 16) | | 100 | 2.40M | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 2))) << 8) | | 101 | 2.40M | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 3)))); | 102 | 2.40M | } | 103 | 7.36M | for (std::size_t i = 16; i < 64; ++i) { | 104 | 7.21M | w[i] = mask_32bit(ssig1(w[i - 2]) + w[i - 7] + ssig0(w[i - 15]) + | 105 | 7.21M | w[i - 16]); | 106 | 7.21M | } | 107 | | | 108 | 150k | word_t a = *message_digest; | 109 | 150k | word_t b = *(message_digest + 1); | 110 | 150k | word_t c = *(message_digest + 2); | 111 | 150k | word_t d = *(message_digest + 3); | 112 | 150k | word_t e = *(message_digest + 4); | 113 | 150k | word_t f = *(message_digest + 5); | 114 | 150k | word_t g = *(message_digest + 6); | 115 | 150k | word_t h = *(message_digest + 7); | 116 | | | 117 | 9.77M | for (std::size_t i = 0; i < 64; ++i) { | 118 | 9.62M | word_t temp1 = h + bsig1(e) + ch(e, f, g) + add_constant[i] + w[i]; | 119 | 9.62M | word_t temp2 = bsig0(a) + maj(a, b, c); | 120 | 9.62M | h = g; | 121 | 9.62M | g = f; | 122 | 9.62M | f = e; | 123 | 9.62M | e = mask_32bit(d + temp1); | 124 | 9.62M | d = c; | 125 | 9.62M | c = b; | 126 | 9.62M | b = a; | 127 | 9.62M | a = mask_32bit(temp1 + temp2); | 128 | 9.62M | } | 129 | 150k | *message_digest += a; | 130 | 150k | *(message_digest + 1) += b; | 131 | 150k | *(message_digest + 2) += c; | 132 | 150k | *(message_digest + 3) += d; | 133 | 150k | *(message_digest + 4) += e; | 134 | 150k | *(message_digest + 5) += f; | 135 | 150k | *(message_digest + 6) += g; | 136 | 150k | *(message_digest + 7) += h; | 137 | 1.35M | for (std::size_t i = 0; i < 8; ++i) { | 138 | 1.20M | *(message_digest + i) = mask_32bit(*(message_digest + i)); | 139 | 1.20M | } | 140 | 150k | } |
void picosha2::detail::hash256_block<unsigned long*, unsigned char*>(unsigned long*, unsigned char*, unsigned char*) Line | Count | Source | 92 | 17.7k | void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) { | 93 | 17.7k | assert(first + 64 == last); | 94 | 17.7k | static_cast<void>(last); // for avoiding unused-variable warning | 95 | 17.7k | word_t w[64]; | 96 | 17.7k | std::fill(w, w + 64, 0); | 97 | 301k | for (std::size_t i = 0; i < 16; ++i) { | 98 | 283k | w[i] = (static_cast<word_t>(mask_8bit(*(first + long(i) * 4))) << 24) | | 99 | 283k | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 1))) << 16) | | 100 | 283k | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 2))) << 8) | | 101 | 283k | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 3)))); | 102 | 283k | } | 103 | 867k | for (std::size_t i = 16; i < 64; ++i) { | 104 | 850k | w[i] = mask_32bit(ssig1(w[i - 2]) + w[i - 7] + ssig0(w[i - 15]) + | 105 | 850k | w[i - 16]); | 106 | 850k | } | 107 | | | 108 | 17.7k | word_t a = *message_digest; | 109 | 17.7k | word_t b = *(message_digest + 1); | 110 | 17.7k | word_t c = *(message_digest + 2); | 111 | 17.7k | word_t d = *(message_digest + 3); | 112 | 17.7k | word_t e = *(message_digest + 4); | 113 | 17.7k | word_t f = *(message_digest + 5); | 114 | 17.7k | word_t g = *(message_digest + 6); | 115 | 17.7k | word_t h = *(message_digest + 7); | 116 | | | 117 | 1.15M | for (std::size_t i = 0; i < 64; ++i) { | 118 | 1.13M | word_t temp1 = h + bsig1(e) + ch(e, f, g) + add_constant[i] + w[i]; | 119 | 1.13M | word_t temp2 = bsig0(a) + maj(a, b, c); | 120 | 1.13M | h = g; | 121 | 1.13M | g = f; | 122 | 1.13M | f = e; | 123 | 1.13M | e = mask_32bit(d + temp1); | 124 | 1.13M | d = c; | 125 | 1.13M | c = b; | 126 | 1.13M | b = a; | 127 | 1.13M | a = mask_32bit(temp1 + temp2); | 128 | 1.13M | } | 129 | 17.7k | *message_digest += a; | 130 | 17.7k | *(message_digest + 1) += b; | 131 | 17.7k | *(message_digest + 2) += c; | 132 | 17.7k | *(message_digest + 3) += d; | 133 | 17.7k | *(message_digest + 4) += e; | 134 | 17.7k | *(message_digest + 5) += f; | 135 | 17.7k | *(message_digest + 6) += g; | 136 | 17.7k | *(message_digest + 7) += h; | 137 | 159k | for (std::size_t i = 0; i < 8; ++i) { | 138 | 141k | *(message_digest + i) = mask_32bit(*(message_digest + i)); | 139 | 141k | } | 140 | 17.7k | } |
|
141 | | |
142 | | } // namespace detail |
143 | | |
144 | | template <typename InIter> |
145 | 0 | void output_hex(InIter first, InIter last, std::ostream& os) { |
146 | 0 | os.setf(std::ios::hex, std::ios::basefield); |
147 | 0 | while (first != last) { |
148 | 0 | os.width(2); |
149 | 0 | os.fill('0'); |
150 | 0 | os << static_cast<unsigned int>(*first); |
151 | 0 | ++first; |
152 | 0 | } |
153 | 0 | os.setf(std::ios::dec, std::ios::basefield); |
154 | 0 | } |
155 | | |
156 | | template <typename InIter> |
157 | 0 | void bytes_to_hex_string(InIter first, InIter last, std::string& hex_str) { |
158 | 0 | std::ostringstream oss; |
159 | 0 | output_hex(first, last, oss); |
160 | 0 | hex_str.assign(oss.str()); |
161 | 0 | } |
162 | | |
163 | | template <typename InContainer> |
164 | | void bytes_to_hex_string(const InContainer& bytes, std::string& hex_str) { |
165 | | bytes_to_hex_string(bytes.begin(), bytes.end(), hex_str); |
166 | | } |
167 | | |
168 | | template <typename InIter> |
169 | | std::string bytes_to_hex_string(InIter first, InIter last) { |
170 | | std::string hex_str; |
171 | | bytes_to_hex_string(first, last, hex_str); |
172 | | return hex_str; |
173 | | } |
174 | | |
175 | | template <typename InContainer> |
176 | | std::string bytes_to_hex_string(const InContainer& bytes) { |
177 | | std::string hex_str; |
178 | | bytes_to_hex_string(bytes, hex_str); |
179 | | return hex_str; |
180 | | } |
181 | | |
182 | | class hash256_one_by_one { |
183 | | public: |
184 | 16.2k | hash256_one_by_one() { init(); } |
185 | | |
186 | 16.2k | void init() { |
187 | 16.2k | buffer_.clear(); |
188 | 16.2k | std::fill(data_length_digits_, data_length_digits_ + 4, 0); |
189 | 16.2k | std::copy(detail::initial_message_digest, |
190 | 16.2k | detail::initial_message_digest + 8, h_); |
191 | 16.2k | } |
192 | | |
193 | | template <typename RaIter> |
194 | 16.2k | void process(RaIter first, RaIter last) { |
195 | 16.2k | add_to_data_length(static_cast<word_t>(std::distance(first, last))); |
196 | 16.2k | std::copy(first, last, std::back_inserter(buffer_)); |
197 | 16.2k | std::size_t i = 0; |
198 | 166k | for (; i + 64 <= buffer_.size(); i += 64) { |
199 | 150k | detail::hash256_block(h_, buffer_.begin() + long(i), |
200 | 150k | buffer_.begin() + long(i) + 64); |
201 | 150k | } |
202 | 16.2k | buffer_.erase(buffer_.begin(), buffer_.begin() + long(i)); |
203 | 16.2k | } void picosha2::hash256_one_by_one::process<std::__1::__wrap_iter<unsigned char const*> >(std::__1::__wrap_iter<unsigned char const*>, std::__1::__wrap_iter<unsigned char const*>) Line | Count | Source | 194 | 16.2k | void process(RaIter first, RaIter last) { | 195 | 16.2k | add_to_data_length(static_cast<word_t>(std::distance(first, last))); | 196 | 16.2k | std::copy(first, last, std::back_inserter(buffer_)); | 197 | 16.2k | std::size_t i = 0; | 198 | 166k | for (; i + 64 <= buffer_.size(); i += 64) { | 199 | 150k | detail::hash256_block(h_, buffer_.begin() + long(i), | 200 | 150k | buffer_.begin() + long(i) + 64); | 201 | 150k | } | 202 | 16.2k | buffer_.erase(buffer_.begin(), buffer_.begin() + long(i)); | 203 | 16.2k | } |
Unexecuted instantiation: void picosha2::hash256_one_by_one::process<std::__1::__wrap_iter<char const*> >(std::__1::__wrap_iter<char const*>, std::__1::__wrap_iter<char const*>) |
204 | | |
205 | 16.2k | void finish() { |
206 | 16.2k | byte_t temp[64]; |
207 | 16.2k | std::fill(temp, temp + 64, 0); |
208 | 16.2k | std::size_t remains = buffer_.size(); |
209 | 16.2k | std::copy(buffer_.begin(), buffer_.end(), temp); |
210 | 16.2k | temp[remains] = 0x80; |
211 | | |
212 | 16.2k | if (remains > 55) { |
213 | 1.47k | std::fill(temp + remains + 1, temp + 64, 0); |
214 | 1.47k | detail::hash256_block(h_, temp, temp + 64); |
215 | 1.47k | std::fill(temp, temp + 64 - 4, 0); |
216 | 14.7k | } else { |
217 | 14.7k | std::fill(temp + remains + 1, temp + 64 - 4, 0); |
218 | 14.7k | } |
219 | | |
220 | 16.2k | write_data_bit_length(&(temp[56])); |
221 | 16.2k | detail::hash256_block(h_, temp, temp + 64); |
222 | 16.2k | } |
223 | | |
224 | | template <typename OutIter> |
225 | 16.2k | void get_hash_bytes(OutIter first, OutIter last) const { |
226 | 146k | for (const word_t* iter = h_; iter != h_ + 8; ++iter) { |
227 | 649k | for (std::size_t i = 0; i < 4 && first != last; ++i) { |
228 | 519k | *(first++) = detail::mask_8bit( |
229 | 519k | static_cast<byte_t>((*iter >> (24 - 8 * i)))); |
230 | 519k | } |
231 | 129k | } |
232 | 16.2k | } void picosha2::hash256_one_by_one::get_hash_bytes<std::__1::__wrap_iter<unsigned char*> >(std::__1::__wrap_iter<unsigned char*>, std::__1::__wrap_iter<unsigned char*>) const Line | Count | Source | 225 | 16.2k | void get_hash_bytes(OutIter first, OutIter last) const { | 226 | 146k | for (const word_t* iter = h_; iter != h_ + 8; ++iter) { | 227 | 649k | for (std::size_t i = 0; i < 4 && first != last; ++i) { | 228 | 519k | *(first++) = detail::mask_8bit( | 229 | 519k | static_cast<byte_t>((*iter >> (24 - 8 * i)))); | 230 | 519k | } | 231 | 129k | } | 232 | 16.2k | } |
Unexecuted instantiation: void picosha2::hash256_one_by_one::get_hash_bytes<unsigned char*>(unsigned char*, unsigned char*) const |
233 | | |
234 | | private: |
235 | 16.2k | void add_to_data_length(word_t n) { |
236 | 16.2k | word_t carry = 0; |
237 | 16.2k | data_length_digits_[0] += n; |
238 | 16.2k | for (std::size_t i = 0; i < 4; ++i) { |
239 | 16.2k | data_length_digits_[i] += carry; |
240 | 16.2k | if (data_length_digits_[i] >= 65536u) { |
241 | 0 | carry = data_length_digits_[i] >> 16; |
242 | 0 | data_length_digits_[i] &= 65535u; |
243 | 16.2k | } else { |
244 | 16.2k | break; |
245 | 16.2k | } |
246 | 16.2k | } |
247 | 16.2k | } |
248 | 16.2k | void write_data_bit_length(byte_t* begin) { |
249 | 16.2k | word_t data_bit_length_digits[4]; |
250 | 16.2k | std::copy(data_length_digits_, data_length_digits_ + 4, |
251 | 16.2k | data_bit_length_digits); |
252 | | |
253 | | // convert byte length to bit length (multiply 8 or shift 3 times left) |
254 | 16.2k | word_t carry = 0; |
255 | 81.2k | for (std::size_t i = 0; i < 4; ++i) { |
256 | 64.9k | word_t before_val = data_bit_length_digits[i]; |
257 | 64.9k | data_bit_length_digits[i] <<= 3; |
258 | 64.9k | data_bit_length_digits[i] |= carry; |
259 | 64.9k | data_bit_length_digits[i] &= 65535u; |
260 | 64.9k | carry = (before_val >> (16 - 3)) & 65535u; |
261 | 64.9k | } |
262 | | |
263 | | // write data_bit_length |
264 | 81.2k | for (int i = 3; i >= 0; --i) { |
265 | 64.9k | (*begin++) = static_cast<byte_t>(data_bit_length_digits[i] >> 8); |
266 | 64.9k | (*begin++) = static_cast<byte_t>(data_bit_length_digits[i]); |
267 | 64.9k | } |
268 | 16.2k | } |
269 | | std::vector<byte_t> buffer_; |
270 | | word_t data_length_digits_[4]; // as 64bit integer (16bit x 4 integer) |
271 | | word_t h_[8]; |
272 | | }; |
273 | | |
274 | | inline void get_hash_hex_string(const hash256_one_by_one& hasher, |
275 | 0 | std::string& hex_str) { |
276 | 0 | byte_t hash[k_digest_size]; |
277 | 0 | hasher.get_hash_bytes(hash, hash + k_digest_size); |
278 | 0 | return bytes_to_hex_string(hash, hash + k_digest_size, hex_str); |
279 | 0 | } |
280 | | |
281 | 0 | inline std::string get_hash_hex_string(const hash256_one_by_one& hasher) { |
282 | 0 | std::string hex_str; |
283 | 0 | get_hash_hex_string(hasher, hex_str); |
284 | 0 | return hex_str; |
285 | 0 | } |
286 | | |
287 | | namespace impl { |
288 | | template <typename RaIter, typename OutIter> |
289 | | void hash256_impl(RaIter first, RaIter last, OutIter first2, OutIter last2, int, |
290 | 16.2k | std::random_access_iterator_tag) { |
291 | 16.2k | hash256_one_by_one hasher; |
292 | | // hasher.init(); |
293 | 16.2k | hasher.process(first, last); |
294 | 16.2k | hasher.finish(); |
295 | 16.2k | hasher.get_hash_bytes(first2, last2); |
296 | 16.2k | } void picosha2::impl::hash256_impl<std::__1::__wrap_iter<unsigned char const*>, std::__1::__wrap_iter<unsigned char*> >(std::__1::__wrap_iter<unsigned char const*>, std::__1::__wrap_iter<unsigned char const*>, std::__1::__wrap_iter<unsigned char*>, std::__1::__wrap_iter<unsigned char*>, int, std::__1::random_access_iterator_tag) Line | Count | Source | 290 | 16.2k | std::random_access_iterator_tag) { | 291 | 16.2k | hash256_one_by_one hasher; | 292 | | // hasher.init(); | 293 | 16.2k | hasher.process(first, last); | 294 | 16.2k | hasher.finish(); | 295 | 16.2k | hasher.get_hash_bytes(first2, last2); | 296 | 16.2k | } |
Unexecuted instantiation: void picosha2::impl::hash256_impl<std::__1::__wrap_iter<char const*>, unsigned char*>(std::__1::__wrap_iter<char const*>, std::__1::__wrap_iter<char const*>, unsigned char*, unsigned char*, int, std::__1::random_access_iterator_tag) |
297 | | |
298 | | template <typename InputIter, typename OutIter> |
299 | | void hash256_impl(InputIter first, InputIter last, OutIter first2, |
300 | | OutIter last2, size_t buffer_size, std::input_iterator_tag) { |
301 | | std::vector<byte_t> buffer(buffer_size); |
302 | | hash256_one_by_one hasher; |
303 | | // hasher.init(); |
304 | | while (first != last) { |
305 | | size_t size = buffer_size; |
306 | | for (size_t i = 0; i != buffer_size; ++i, ++first) { |
307 | | if (first == last) { |
308 | | size = i; |
309 | | break; |
310 | | } |
311 | | buffer[i] = *first; |
312 | | } |
313 | | hasher.process(buffer.begin(), buffer.begin() + ptrdiff_t(size)); |
314 | | } |
315 | | hasher.finish(); |
316 | | hasher.get_hash_bytes(first2, last2); |
317 | | } |
318 | | } |
319 | | |
320 | | template <typename InIter, typename OutIter> |
321 | | void hash256(InIter first, InIter last, OutIter first2, OutIter last2, |
322 | 16.2k | int buffer_size = PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR) { |
323 | 16.2k | picosha2::impl::hash256_impl( |
324 | 16.2k | first, last, first2, last2, buffer_size, |
325 | 16.2k | typename std::iterator_traits<InIter>::iterator_category()); |
326 | 16.2k | } void picosha2::hash256<std::__1::__wrap_iter<unsigned char const*>, std::__1::__wrap_iter<unsigned char*> >(std::__1::__wrap_iter<unsigned char const*>, std::__1::__wrap_iter<unsigned char const*>, std::__1::__wrap_iter<unsigned char*>, std::__1::__wrap_iter<unsigned char*>, int) Line | Count | Source | 322 | 16.2k | int buffer_size = PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR) { | 323 | 16.2k | picosha2::impl::hash256_impl( | 324 | 16.2k | first, last, first2, last2, buffer_size, | 325 | 16.2k | typename std::iterator_traits<InIter>::iterator_category()); | 326 | 16.2k | } |
Unexecuted instantiation: void picosha2::hash256<std::__1::__wrap_iter<char const*>, unsigned char*>(std::__1::__wrap_iter<char const*>, std::__1::__wrap_iter<char const*>, unsigned char*, unsigned char*, int) |
327 | | |
328 | | template <typename InIter, typename OutContainer> |
329 | | void hash256(InIter first, InIter last, OutContainer& dst) { |
330 | | hash256(first, last, dst.begin(), dst.end()); |
331 | | } |
332 | | |
333 | | template <typename InContainer, typename OutIter> |
334 | | void hash256(const InContainer& src, OutIter first, OutIter last) { |
335 | | hash256(src.begin(), src.end(), first, last); |
336 | | } |
337 | | |
338 | | template <typename InContainer, typename OutContainer> |
339 | | void hash256(const InContainer& src, OutContainer& dst) { |
340 | | hash256(src.begin(), src.end(), dst.begin(), dst.end()); |
341 | | } |
342 | | |
343 | | template <typename InIter> |
344 | 0 | void hash256_hex_string(InIter first, InIter last, std::string& hex_str) { |
345 | 0 | byte_t hashed[k_digest_size]; |
346 | 0 | hash256(first, last, hashed, hashed + k_digest_size); |
347 | 0 | std::ostringstream oss; |
348 | 0 | output_hex(hashed, hashed + k_digest_size, oss); |
349 | 0 | hex_str.assign(oss.str()); |
350 | 0 | } |
351 | | |
352 | | template <typename RaContainer> |
353 | | std::vector<uint8_t> hash256(RaContainer const& _src) |
354 | 16.2k | { |
355 | 16.2k | std::vector<uint8_t> ret(32); |
356 | 16.2k | hash256(_src.begin(), _src.end(), ret.begin(), ret.end()); |
357 | 16.2k | return ret; |
358 | 16.2k | } |
359 | | |
360 | | template <typename InIter> |
361 | | std::string hash256_hex_string(InIter first, InIter last) { |
362 | | std::string hex_str; |
363 | | hash256_hex_string(first, last, hex_str); |
364 | | return hex_str; |
365 | | } |
366 | | |
367 | 0 | inline void hash256_hex_string(const std::string& src, std::string& hex_str) { |
368 | 0 | hash256_hex_string(src.begin(), src.end(), hex_str); |
369 | 0 | } |
370 | | |
371 | | template <typename InContainer> |
372 | | void hash256_hex_string(const InContainer& src, std::string& hex_str) { |
373 | | hash256_hex_string(src.begin(), src.end(), hex_str); |
374 | | } |
375 | | |
376 | | template <typename InContainer> |
377 | | std::string hash256_hex_string(const InContainer& src) { |
378 | | return hash256_hex_string(src.begin(), src.end()); |
379 | | } |
380 | | template<typename OutIter>void hash256(std::ifstream& f, OutIter first, OutIter last){ |
381 | | hash256(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>(), first,last); |
382 | | |
383 | | } |
384 | | }// namespace picosha2 |
385 | | #endif // PICOSHA2_H |