diff options
| author | Denis Evsyukov <[email protected]> | 2020-02-22 10:29:00 +0300 |
|---|---|---|
| committer | Denis Evsyukov <[email protected]> | 2020-02-22 10:29:00 +0300 |
| commit | 40cb3be8b7630971cdfb3d80de2d3598607f4193 (patch) | |
| tree | be9fafe775e260cb208414c9e2f5a21f89a24387 /src | |
| parent | [~] updates (diff) | |
| download | t-40cb3be8b7630971cdfb3d80de2d3598607f4193.tar.xz t-40cb3be8b7630971cdfb3d80de2d3598607f4193.zip | |
[~] sha1 to sha256
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 13 | ||||
| -rw-r--r-- | src/sha1/sha1.hpp | 255 | ||||
| -rw-r--r-- | src/sha256/picosha2.h | 376 |
3 files changed, 384 insertions, 260 deletions
diff --git a/src/main.cpp b/src/main.cpp index c2c5b4f..c5ffd45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ #include "functions.hpp" #include "opts/cxxopts.hpp" -#include "sha1/sha1.hpp" +#include "sha256/picosha2.h" using namespace std; @@ -37,10 +37,13 @@ int main(int argc, char *argv[]) { for (const auto &s : v) { str += s + " "; } - str = trim(str); - char hex[SHA1_HEX_SIZE]; - sha1(str.c_str()).finalize().print_hex(hex); - cout << "sha1('" << str << "'): " << hex << endl; + auto src_str = trim(str); + std::vector<unsigned char> hash(picosha2::k_digest_size); + picosha2::hash256(src_str.begin(), src_str.end(), hash.begin(), hash.end()); + + std::string hex_str = + picosha2::bytes_to_hex_string(hash.begin(), hash.end()); + cout << "sha256('" << str << "'): " << hex_str << endl; } std::string taskdir; if (result.count("taskdir")) { diff --git a/src/sha1/sha1.hpp b/src/sha1/sha1.hpp deleted file mode 100644 index 3baeec4..0000000 --- a/src/sha1/sha1.hpp +++ /dev/null @@ -1,255 +0,0 @@ -#include <stdint.h> -#include <string.h> - -#define SHA1_HEX_SIZE (40 + 1) -#define SHA1_BASE64_SIZE (28 + 1) - -class sha1 { -private: - - void add_byte_dont_count_bits(uint8_t x) { - buf[i++] = x; - - if (i >= sizeof(buf)) { - i = 0; - process_block(buf); - } - } - - static uint32_t rol32(uint32_t x, uint32_t n) { - return (x << n) | (x >> (32 - n)); - } - - static uint32_t make_word(const uint8_t *p) { - return - ((uint32_t) p[0] << 3 * 8) | - ((uint32_t) p[1] << 2 * 8) | - ((uint32_t) p[2] << 1 * 8) | - ((uint32_t) p[3] << 0 * 8); - } - - void process_block(const uint8_t *ptr) { - const uint32_t c0 = 0x5a827999; - const uint32_t c1 = 0x6ed9eba1; - const uint32_t c2 = 0x8f1bbcdc; - const uint32_t c3 = 0xca62c1d6; - - uint32_t a = state[0]; - uint32_t b = state[1]; - uint32_t c = state[2]; - uint32_t d = state[3]; - uint32_t e = state[4]; - - uint32_t w[16]; - - for (int i = 0; i < 16; i++) w[i] = make_word(ptr + i * 4); - -#define SHA1_LOAD(i) w[i&15] = rol32(w[(i+13)&15] ^ w[(i+8)&15] ^ w[(i+2)&15] ^ w[i&15], 1); -#define SHA1_ROUND_0(v, u, x, y, z, i) z += ((u & (x ^ y)) ^ y) + w[i&15] + c0 + rol32(v, 5); u = rol32(u, 30); -#define SHA1_ROUND_1(v, u, x, y, z, i) SHA1_LOAD(i) z += ((u & (x ^ y)) ^ y) + w[i&15] + c0 + rol32(v, 5); u = rol32(u, 30); -#define SHA1_ROUND_2(v, u, x, y, z, i) SHA1_LOAD(i) z += (u ^ x ^ y) + w[i&15] + c1 + rol32(v, 5); u = rol32(u, 30); -#define SHA1_ROUND_3(v, u, x, y, z, i) SHA1_LOAD(i) z += (((u | x) & y) | (u & x)) + w[i&15] + c2 + rol32(v, 5); u = rol32(u, 30); -#define SHA1_ROUND_4(v, u, x, y, z, i) SHA1_LOAD(i) z += (u ^ x ^ y) + w[i&15] + c3 + rol32(v, 5); u = rol32(u, 30); - - SHA1_ROUND_0(a, b, c, d, e, 0); - SHA1_ROUND_0(e, a, b, c, d, 1); - SHA1_ROUND_0(d, e, a, b, c, 2); - SHA1_ROUND_0(c, d, e, a, b, 3); - SHA1_ROUND_0(b, c, d, e, a, 4); - SHA1_ROUND_0(a, b, c, d, e, 5); - SHA1_ROUND_0(e, a, b, c, d, 6); - SHA1_ROUND_0(d, e, a, b, c, 7); - SHA1_ROUND_0(c, d, e, a, b, 8); - SHA1_ROUND_0(b, c, d, e, a, 9); - SHA1_ROUND_0(a, b, c, d, e, 10); - SHA1_ROUND_0(e, a, b, c, d, 11); - SHA1_ROUND_0(d, e, a, b, c, 12); - SHA1_ROUND_0(c, d, e, a, b, 13); - SHA1_ROUND_0(b, c, d, e, a, 14); - SHA1_ROUND_0(a, b, c, d, e, 15); - SHA1_ROUND_1(e, a, b, c, d, 16); - SHA1_ROUND_1(d, e, a, b, c, 17); - SHA1_ROUND_1(c, d, e, a, b, 18); - SHA1_ROUND_1(b, c, d, e, a, 19); - SHA1_ROUND_2(a, b, c, d, e, 20); - SHA1_ROUND_2(e, a, b, c, d, 21); - SHA1_ROUND_2(d, e, a, b, c, 22); - SHA1_ROUND_2(c, d, e, a, b, 23); - SHA1_ROUND_2(b, c, d, e, a, 24); - SHA1_ROUND_2(a, b, c, d, e, 25); - SHA1_ROUND_2(e, a, b, c, d, 26); - SHA1_ROUND_2(d, e, a, b, c, 27); - SHA1_ROUND_2(c, d, e, a, b, 28); - SHA1_ROUND_2(b, c, d, e, a, 29); - SHA1_ROUND_2(a, b, c, d, e, 30); - SHA1_ROUND_2(e, a, b, c, d, 31); - SHA1_ROUND_2(d, e, a, b, c, 32); - SHA1_ROUND_2(c, d, e, a, b, 33); - SHA1_ROUND_2(b, c, d, e, a, 34); - SHA1_ROUND_2(a, b, c, d, e, 35); - SHA1_ROUND_2(e, a, b, c, d, 36); - SHA1_ROUND_2(d, e, a, b, c, 37); - SHA1_ROUND_2(c, d, e, a, b, 38); - SHA1_ROUND_2(b, c, d, e, a, 39); - SHA1_ROUND_3(a, b, c, d, e, 40); - SHA1_ROUND_3(e, a, b, c, d, 41); - SHA1_ROUND_3(d, e, a, b, c, 42); - SHA1_ROUND_3(c, d, e, a, b, 43); - SHA1_ROUND_3(b, c, d, e, a, 44); - SHA1_ROUND_3(a, b, c, d, e, 45); - SHA1_ROUND_3(e, a, b, c, d, 46); - SHA1_ROUND_3(d, e, a, b, c, 47); - SHA1_ROUND_3(c, d, e, a, b, 48); - SHA1_ROUND_3(b, c, d, e, a, 49); - SHA1_ROUND_3(a, b, c, d, e, 50); - SHA1_ROUND_3(e, a, b, c, d, 51); - SHA1_ROUND_3(d, e, a, b, c, 52); - SHA1_ROUND_3(c, d, e, a, b, 53); - SHA1_ROUND_3(b, c, d, e, a, 54); - SHA1_ROUND_3(a, b, c, d, e, 55); - SHA1_ROUND_3(e, a, b, c, d, 56); - SHA1_ROUND_3(d, e, a, b, c, 57); - SHA1_ROUND_3(c, d, e, a, b, 58); - SHA1_ROUND_3(b, c, d, e, a, 59); - SHA1_ROUND_4(a, b, c, d, e, 60); - SHA1_ROUND_4(e, a, b, c, d, 61); - SHA1_ROUND_4(d, e, a, b, c, 62); - SHA1_ROUND_4(c, d, e, a, b, 63); - SHA1_ROUND_4(b, c, d, e, a, 64); - SHA1_ROUND_4(a, b, c, d, e, 65); - SHA1_ROUND_4(e, a, b, c, d, 66); - SHA1_ROUND_4(d, e, a, b, c, 67); - SHA1_ROUND_4(c, d, e, a, b, 68); - SHA1_ROUND_4(b, c, d, e, a, 69); - SHA1_ROUND_4(a, b, c, d, e, 70); - SHA1_ROUND_4(e, a, b, c, d, 71); - SHA1_ROUND_4(d, e, a, b, c, 72); - SHA1_ROUND_4(c, d, e, a, b, 73); - SHA1_ROUND_4(b, c, d, e, a, 74); - SHA1_ROUND_4(a, b, c, d, e, 75); - SHA1_ROUND_4(e, a, b, c, d, 76); - SHA1_ROUND_4(d, e, a, b, c, 77); - SHA1_ROUND_4(c, d, e, a, b, 78); - SHA1_ROUND_4(b, c, d, e, a, 79); - -#undef SHA1_LOAD -#undef SHA1_ROUND_0 -#undef SHA1_ROUND_1 -#undef SHA1_ROUND_2 -#undef SHA1_ROUND_3 -#undef SHA1_ROUND_4 - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - state[4] += e; - } - -public: - - uint32_t state[5]; - uint8_t buf[64]; - uint32_t i; - uint64_t n_bits; - - sha1(const char *text = NULL) : i(0), n_bits(0) { - state[0] = 0x67452301; - state[1] = 0xEFCDAB89; - state[2] = 0x98BADCFE; - state[3] = 0x10325476; - state[4] = 0xC3D2E1F0; - if (text) add(text); - } - - sha1 &add(uint8_t x) { - add_byte_dont_count_bits(x); - n_bits += 8; - return *this; - } - - sha1 &add(char c) { - return add(*(uint8_t *) &c); - } - - sha1 &add(const void *data, uint32_t n) { - if (!data) return *this; - - const uint8_t *ptr = (const uint8_t *) data; - - // fill up block if not full - for (; n && i % sizeof(buf); n--) add(*ptr++); - - // process full blocks - for (; n >= sizeof(buf); n -= sizeof(buf)) { - process_block(ptr); - ptr += sizeof(buf); - n_bits += sizeof(buf) * 8; - } - - // process remaining part of block - for (; n; n--) add(*ptr++); - - return *this; - } - - sha1 &add(const char *text) { - if (!text) return *this; - return add(text, strlen(text)); - } - - sha1 &finalize() { - // hashed text ends with 0x80, some padding 0x00 and the length in bits - add_byte_dont_count_bits(0x80); - while (i % 64 != 56) add_byte_dont_count_bits(0x00); - for (int j = 7; j >= 0; j--) add_byte_dont_count_bits(n_bits >> j * 8); - - return *this; - } - - const sha1 &print_hex( - char *hex, - bool zero_terminate = true, - const char *alphabet = "0123456789abcdef" - ) const { - // print hex - int k = 0; - for (int i = 0; i < 5; i++) { - for (int j = 7; j >= 0; j--) { - hex[k++] = alphabet[(state[i] >> j * 4) & 0xf]; - } - } - if (zero_terminate) hex[k] = '\0'; - return *this; - } - - const sha1 &print_base64(char *base64, bool zero_terminate = true) const { - static const uint8_t *table = (const uint8_t *) - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789" - "+/"; - - uint32_t triples[7] = { - ((state[0] & 0xffffff00) >> 1 * 8), - ((state[0] & 0x000000ff) << 2 * 8) | ((state[1] & 0xffff0000) >> 2 * 8), - ((state[1] & 0x0000ffff) << 1 * 8) | ((state[2] & 0xff000000) >> 3 * 8), - ((state[2] & 0x00ffffff) << 0 * 8), - ((state[3] & 0xffffff00) >> 1 * 8), - ((state[3] & 0x000000ff) << 2 * 8) | ((state[4] & 0xffff0000) >> 2 * 8), - ((state[4] & 0x0000ffff) << 1 * 8), - }; - - for (int i = 0; i < 7; i++) { - uint32_t x = triples[i]; - base64[i * 4 + 0] = table[(x >> 3 * 6) % 64]; - base64[i * 4 + 1] = table[(x >> 2 * 6) % 64]; - base64[i * 4 + 2] = table[(x >> 1 * 6) % 64]; - base64[i * 4 + 3] = table[(x >> 0 * 6) % 64]; - } - - base64[SHA1_BASE64_SIZE - 2] = '='; - if (zero_terminate) base64[SHA1_BASE64_SIZE - 1] = '\0'; - return *this; - } -}; diff --git a/src/sha256/picosha2.h b/src/sha256/picosha2.h new file mode 100644 index 0000000..6bfe2d0 --- /dev/null +++ b/src/sha256/picosha2.h @@ -0,0 +1,376 @@ +/* +The MIT License (MIT) + +Copyright (C) 2017 okdshin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +#ifndef PICOSHA2_H +#define PICOSHA2_H +// picosha2:20140213 + +#ifndef PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR +#define PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR \ + 1048576 //=1024*1024: default is 1MB memory +#endif + +#include <algorithm> +#include <cassert> +#include <fstream> +#include <iterator> +#include <sstream> +#include <vector> +namespace picosha2 { +typedef unsigned long word_t; +typedef unsigned char byte_t; + +static const size_t k_digest_size = 32; + +namespace detail { +inline byte_t mask_8bit(byte_t x) { return x & 0xff; } + +inline word_t mask_32bit(word_t x) { return x & 0xffffffff; } + +const word_t add_constant[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, + 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, + 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, + 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, + 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, + 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + +const word_t initial_message_digest[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, + 0xa54ff53a, 0x510e527f, 0x9b05688c, + 0x1f83d9ab, 0x5be0cd19}; + +inline word_t ch(word_t x, word_t y, word_t z) { return (x & y) ^ ((~x) & z); } + +inline word_t maj(word_t x, word_t y, word_t z) { + return (x & y) ^ (x & z) ^ (y & z); +} + +inline word_t rotr(word_t x, std::size_t n) { + assert(n < 32); + return mask_32bit((x >> n) | (x << (32 - n))); +} + +inline word_t bsig0(word_t x) { return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22); } + +inline word_t bsig1(word_t x) { return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25); } + +inline word_t shr(word_t x, std::size_t n) { + assert(n < 32); + return x >> n; +} + +inline word_t ssig0(word_t x) { return rotr(x, 7) ^ rotr(x, 18) ^ shr(x, 3); } + +inline word_t ssig1(word_t x) { return rotr(x, 17) ^ rotr(x, 19) ^ shr(x, 10); } + +template <typename RaIter1, typename RaIter2> +void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) { + assert(first + 64 == last); + static_cast<void>(last); // for avoiding unused-variable warning + word_t w[64]; + std::fill(w, w + 64, 0); + for (std::size_t i = 0; i < 16; ++i) { + w[i] = (static_cast<word_t>(mask_8bit(*(first + i * 4))) << 24) | + (static_cast<word_t>(mask_8bit(*(first + i * 4 + 1))) << 16) | + (static_cast<word_t>(mask_8bit(*(first + i * 4 + 2))) << 8) | + (static_cast<word_t>(mask_8bit(*(first + i * 4 + 3)))); + } + for (std::size_t i = 16; i < 64; ++i) { + w[i] = + mask_32bit(ssig1(w[i - 2]) + w[i - 7] + ssig0(w[i - 15]) + w[i - 16]); + } + + word_t a = *message_digest; + word_t b = *(message_digest + 1); + word_t c = *(message_digest + 2); + word_t d = *(message_digest + 3); + word_t e = *(message_digest + 4); + word_t f = *(message_digest + 5); + word_t g = *(message_digest + 6); + word_t h = *(message_digest + 7); + + for (std::size_t i = 0; i < 64; ++i) { + word_t temp1 = h + bsig1(e) + ch(e, f, g) + add_constant[i] + w[i]; + word_t temp2 = bsig0(a) + maj(a, b, c); + h = g; + g = f; + f = e; + e = mask_32bit(d + temp1); + d = c; + c = b; + b = a; + a = mask_32bit(temp1 + temp2); + } + *message_digest += a; + *(message_digest + 1) += b; + *(message_digest + 2) += c; + *(message_digest + 3) += d; + *(message_digest + 4) += e; + *(message_digest + 5) += f; + *(message_digest + 6) += g; + *(message_digest + 7) += h; + for (std::size_t i = 0; i < 8; ++i) { + *(message_digest + i) = mask_32bit(*(message_digest + i)); + } +} + +} // namespace detail + +template <typename InIter> +void output_hex(InIter first, InIter last, std::ostream &os) { + os.setf(std::ios::hex, std::ios::basefield); + while (first != last) { + os.width(2); + os.fill('0'); + os << static_cast<unsigned int>(*first); + ++first; + } + os.setf(std::ios::dec, std::ios::basefield); +} + +template <typename InIter> +void bytes_to_hex_string(InIter first, InIter last, std::string &hex_str) { + std::ostringstream oss; + output_hex(first, last, oss); + hex_str.assign(oss.str()); +} + +template <typename InContainer> +void bytes_to_hex_string(const InContainer &bytes, std::string &hex_str) { + bytes_to_hex_string(bytes.begin(), bytes.end(), hex_str); +} + +template <typename InIter> +std::string bytes_to_hex_string(InIter first, InIter last) { + std::string hex_str; + bytes_to_hex_string(first, last, hex_str); + return hex_str; +} + +template <typename InContainer> +std::string bytes_to_hex_string(const InContainer &bytes) { + std::string hex_str; + bytes_to_hex_string(bytes, hex_str); + return hex_str; +} + +class hash256_one_by_one { +public: + hash256_one_by_one() { init(); } + + void init() { + buffer_.clear(); + std::fill(data_length_digits_, data_length_digits_ + 4, 0); + std::copy(detail::initial_message_digest, + detail::initial_message_digest + 8, h_); + } + + template <typename RaIter> void process(RaIter first, RaIter last) { + add_to_data_length(static_cast<word_t>(std::distance(first, last))); + std::copy(first, last, std::back_inserter(buffer_)); + std::size_t i = 0; + for (; i + 64 <= buffer_.size(); i += 64) { + detail::hash256_block(h_, buffer_.begin() + i, buffer_.begin() + i + 64); + } + buffer_.erase(buffer_.begin(), buffer_.begin() + i); + } + + void finish() { + byte_t temp[64]; + std::fill(temp, temp + 64, 0); + std::size_t remains = buffer_.size(); + std::copy(buffer_.begin(), buffer_.end(), temp); + temp[remains] = 0x80; + + if (remains > 55) { + std::fill(temp + remains + 1, temp + 64, 0); + detail::hash256_block(h_, temp, temp + 64); + std::fill(temp, temp + 64 - 4, 0); + } else { + std::fill(temp + remains + 1, temp + 64 - 4, 0); + } + + write_data_bit_length(&(temp[56])); + detail::hash256_block(h_, temp, temp + 64); + } + + template <typename OutIter> + void get_hash_bytes(OutIter first, OutIter last) const { + for (const word_t *iter = h_; iter != h_ + 8; ++iter) { + for (std::size_t i = 0; i < 4 && first != last; ++i) { + *(first++) = + detail::mask_8bit(static_cast<byte_t>((*iter >> (24 - 8 * i)))); + } + } + } + +private: + void add_to_data_length(word_t n) { + word_t carry = 0; + data_length_digits_[0] += n; + for (std::size_t i = 0; i < 4; ++i) { + data_length_digits_[i] += carry; + if (data_length_digits_[i] >= 65536u) { + carry = data_length_digits_[i] >> 16; + data_length_digits_[i] &= 65535u; + } else { + break; + } + } + } + void write_data_bit_length(byte_t *begin) { + word_t data_bit_length_digits[4]; + std::copy(data_length_digits_, data_length_digits_ + 4, + data_bit_length_digits); + + // convert byte length to bit length (multiply 8 or shift 3 times left) + word_t carry = 0; + for (std::size_t i = 0; i < 4; ++i) { + word_t before_val = data_bit_length_digits[i]; + data_bit_length_digits[i] <<= 3; + data_bit_length_digits[i] |= carry; + data_bit_length_digits[i] &= 65535u; + carry = (before_val >> (16 - 3)) & 65535u; + } + + // write data_bit_length + for (int i = 3; i >= 0; --i) { + (*begin++) = static_cast<byte_t>(data_bit_length_digits[i] >> 8); + (*begin++) = static_cast<byte_t>(data_bit_length_digits[i]); + } + } + std::vector<byte_t> buffer_; + word_t data_length_digits_[4]; // as 64bit integer (16bit x 4 integer) + word_t h_[8]; +}; + +inline void get_hash_hex_string(const hash256_one_by_one &hasher, + std::string &hex_str) { + byte_t hash[k_digest_size]; + hasher.get_hash_bytes(hash, hash + k_digest_size); + return bytes_to_hex_string(hash, hash + k_digest_size, hex_str); +} + +inline std::string get_hash_hex_string(const hash256_one_by_one &hasher) { + std::string hex_str; + get_hash_hex_string(hasher, hex_str); + return hex_str; +} + +namespace impl { +template <typename RaIter, typename OutIter> +void hash256_impl(RaIter first, RaIter last, OutIter first2, OutIter last2, int, + std::random_access_iterator_tag) { + hash256_one_by_one hasher; + // hasher.init(); + hasher.process(first, last); + hasher.finish(); + hasher.get_hash_bytes(first2, last2); +} + +template <typename InputIter, typename OutIter> +void hash256_impl(InputIter first, InputIter last, OutIter first2, + OutIter last2, int buffer_size, std::input_iterator_tag) { + std::vector<byte_t> buffer(buffer_size); + hash256_one_by_one hasher; + // hasher.init(); + while (first != last) { + int size = buffer_size; + for (int i = 0; i != buffer_size; ++i, ++first) { + if (first == last) { + size = i; + break; + } + buffer[i] = *first; + } + hasher.process(buffer.begin(), buffer.begin() + size); + } + hasher.finish(); + hasher.get_hash_bytes(first2, last2); +} +} // namespace impl + +template <typename InIter, typename OutIter> +void hash256(InIter first, InIter last, OutIter first2, OutIter last2, + int buffer_size = PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR) { + picosha2::impl::hash256_impl( + first, last, first2, last2, buffer_size, + typename std::iterator_traits<InIter>::iterator_category()); +} + +template <typename InIter, typename OutContainer> +void hash256(InIter first, InIter last, OutContainer &dst) { + hash256(first, last, dst.begin(), dst.end()); +} + +template <typename InContainer, typename OutIter> +void hash256(const InContainer &src, OutIter first, OutIter last) { + hash256(src.begin(), src.end(), first, last); +} + +template <typename InContainer, typename OutContainer> +void hash256(const InContainer &src, OutContainer &dst) { + hash256(src.begin(), src.end(), dst.begin(), dst.end()); +} + +template <typename InIter> +void hash256_hex_string(InIter first, InIter last, std::string &hex_str) { + byte_t hashed[k_digest_size]; + hash256(first, last, hashed, hashed + k_digest_size); + std::ostringstream oss; + output_hex(hashed, hashed + k_digest_size, oss); + hex_str.assign(oss.str()); +} + +template <typename InIter> +std::string hash256_hex_string(InIter first, InIter last) { + std::string hex_str; + hash256_hex_string(first, last, hex_str); + return hex_str; +} + +inline void hash256_hex_string(const std::string &src, std::string &hex_str) { + hash256_hex_string(src.begin(), src.end(), hex_str); +} + +template <typename InContainer> +void hash256_hex_string(const InContainer &src, std::string &hex_str) { + hash256_hex_string(src.begin(), src.end(), hex_str); +} + +template <typename InContainer> +std::string hash256_hex_string(const InContainer &src) { + return hash256_hex_string(src.begin(), src.end()); +} +template <typename OutIter> +void hash256(std::ifstream &f, OutIter first, OutIter last) { + hash256(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>(), + first, last); +} +} // namespace picosha2 +#endif // PICOSHA2_H
\ No newline at end of file |