28 #ifndef PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR
29 #define PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR \
30 1048576 //=1024*1024: default is 1MB memory
43 static const size_t k_digest_size = 32;
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
65 0x6a09e667, 0xbb67ae85, 0x3c6ef372,
66 0xa54ff53a, 0x510e527f, 0x9b05688c,
67 0x1f83d9ab, 0x5be0cd19
73 return (
x &
y) ^ (
x &
z) ^ (
y &
z);
94 template <
typename RaIter1,
typename RaIter2>
96 assert(
first + 64 == last);
97 static_cast<void>(last);
100 for (std::size_t
i = 0;
i < 16; ++
i) {
106 for (std::size_t
i = 16;
i < 64; ++
i) {
112 word_t b = *(message_digest + 1);
113 word_t c = *(message_digest + 2);
114 word_t d = *(message_digest + 3);
115 word_t e = *(message_digest + 4);
116 word_t f = *(message_digest + 5);
117 word_t g = *(message_digest + 6);
118 word_t h = *(message_digest + 7);
120 for (std::size_t
i = 0;
i < 64; ++
i) {
132 *message_digest +=
a;
133 *(message_digest + 1) +=
b;
134 *(message_digest + 2) +=
c;
135 *(message_digest + 3) +=
d;
136 *(message_digest + 4) +=
e;
137 *(message_digest + 5) +=
f;
138 *(message_digest + 6) +=
g;
139 *(message_digest + 7) +=
h;
140 for (std::size_t
i = 0;
i < 8; ++
i) {
141 *(message_digest +
i) =
mask_32bit(*(message_digest +
i));
146 template <
typename InIter>
148 os.setf(std::ios::hex, std::ios::basefield);
149 while (
first != last) {
152 os << static_cast<unsigned int>(*
first);
155 os.setf(std::ios::dec, std::ios::basefield);
158 template <
typename InIter>
160 std::ostringstream oss;
162 hex_str.assign(oss.str());
165 template <
typename InContainer>
170 template <
typename InIter>
177 template <
typename InContainer>
195 template <
typename RaIter>
213 temp[remains] = 0x80;
216 std::fill(temp + remains + 1, temp + 64, 0);
220 std::fill(temp + remains + 1, temp + 64 - 4, 0);
227 template <
typename OutIter>
230 for (std::size_t
i = 0;
i < 4 &&
first != last; ++
i) {
232 static_cast<byte_t>((*iter >> (24 - 8 *
i))));
242 for (std::size_t
i = 0;
i < 4; ++
i) {
254 word_t data_bit_length_digits[4];
257 data_bit_length_digits);
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] &= 65535
u;
266 carry = (before_val >> (16 - 3)) & 65535
u;
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]);
282 std::string& hex_str) {
296 template <
typename RaIter,
typename OutIter>
298 std::random_access_iterator_tag) {
307 template <
typename InputIter,
typename OutIter>
309 OutIter last2,
int buffer_size, std::input_iterator_tag) {
310 std::vector<byte_t>
buffer(buffer_size);
313 while (
first != last) {
314 int size = buffer_size;
315 for (
int i = 0;
i != buffer_size; ++
i, ++
first) {
329 template <
typename InIter,
typename OutIter>
333 first, last, first2, last2, buffer_size,
334 typename std::iterator_traits<InIter>::iterator_category());
337 template <
typename InIter,
typename OutContainer>
342 template <
typename InContainer,
typename OutIter>
347 template <
typename InContainer,
typename OutContainer>
352 template <
typename InIter>
354 byte_t hashed[k_digest_size];
357 std::ostringstream oss;
358 output_hex(hashed, hashed + k_digest_size, oss);
359 hex_str.assign(oss.str());
362 template <
typename InIter>
373 template <
typename InContainer>
378 template <
typename InContainer>
383 template<
typename OutIter>
void hash256(std::ifstream&
f, OutIter
first, OutIter last) {
384 hash256(std::istreambuf_iterator<char>(
f), std::istreambuf_iterator<char>(),
first, last);