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