28#ifndef PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR
29#define PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR \
40typedef unsigned long word_t;
41typedef unsigned char byte_t;
44inline byte_t mask_8bit(byte_t x) {
return x & 0xff; }
46inline word_t mask_32bit(word_t x) {
return x & 0xffffffff; }
48const word_t add_constant[64] = {
49 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
50 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
51 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
52 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
53 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
54 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
55 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
56 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
57 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
58 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
59 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
61const word_t initial_message_digest[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372,
62 0xa54ff53a, 0x510e527f, 0x9b05688c,
63 0x1f83d9ab, 0x5be0cd19};
65inline word_t ch(word_t x, word_t y, word_t z) {
return (x & y) ^ ((~x) & z); }
67inline word_t maj(word_t x, word_t y, word_t z) {
68 return (x & y) ^ (x & z) ^ (y & z);
71inline word_t rotr(word_t x, std::size_t n) {
73 return mask_32bit((x >> n) | (x << (32 - n)));
76inline word_t bsig0(word_t x) {
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22); }
78inline word_t bsig1(word_t x) {
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25); }
80inline word_t shr(word_t x, std::size_t n) {
85inline word_t ssig0(word_t x) {
return rotr(x, 7) ^ rotr(x, 18) ^ shr(x, 3); }
87inline word_t ssig1(word_t x) {
return rotr(x, 17) ^ rotr(x, 19) ^ shr(x, 10); }
89template <
typename RaIter1,
typename RaIter2>
90void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) {
91 assert(first + 64 == last);
92 static_cast<void>(last);
94 std::fill(w, w + 64, 0);
95 for (std::size_t i = 0; i < 16; ++i) {
96 w[i] = (
static_cast<word_t
>(mask_8bit(*(first + i * 4))) << 24) |
97 (
static_cast<word_t
>(mask_8bit(*(first + i * 4 + 1))) << 16) |
98 (
static_cast<word_t
>(mask_8bit(*(first + i * 4 + 2))) << 8) |
99 (
static_cast<word_t
>(mask_8bit(*(first + i * 4 + 3))));
101 for (std::size_t i = 16; i < 64; ++i) {
102 w[i] = mask_32bit(ssig1(w[i - 2]) + w[i - 7] + ssig0(w[i - 15]) +
106 word_t a = *message_digest;
107 word_t b = *(message_digest + 1);
108 word_t c = *(message_digest + 2);
109 word_t d = *(message_digest + 3);
110 word_t e = *(message_digest + 4);
111 word_t f = *(message_digest + 5);
112 word_t g = *(message_digest + 6);
113 word_t h = *(message_digest + 7);
115 for (std::size_t i = 0; i < 64; ++i) {
116 word_t temp1 = h + bsig1(e) + ch(e, f, g) + add_constant[i] + w[i];
117 word_t temp2 = bsig0(a) + maj(a, b, c);
121 e = mask_32bit(d + temp1);
125 a = mask_32bit(temp1 + temp2);
127 *message_digest += a;
128 *(message_digest + 1) += b;
129 *(message_digest + 2) += c;
130 *(message_digest + 3) += d;
131 *(message_digest + 4) += e;
132 *(message_digest + 5) += f;
133 *(message_digest + 6) += g;
134 *(message_digest + 7) += h;
135 for (std::size_t i = 0; i < 8; ++i) {
136 *(message_digest + i) = mask_32bit(*(message_digest + i));
142template <
typename InIter>
143void output_hex(InIter first, InIter last, std::ostream& os) {
144 os.setf(std::ios::hex, std::ios::basefield);
145 char orig_fill_char = os.fill();
146 while (first != last) {
149 os << static_cast<unsigned int>(*first);
152 os.setf(std::ios::dec, std::ios::basefield);
153 os.fill(orig_fill_char);
156template <
typename InIter>
157void bytes_to_hex_string(InIter first, InIter last, std::string& hex_str) {
158 std::ostringstream oss;
159 output_hex(first, last, oss);
160 hex_str.assign(oss.str());
163template <
typename InContainer>
164void bytes_to_hex_string(
const InContainer& bytes, std::string& hex_str) {
165 bytes_to_hex_string(bytes.begin(), bytes.end(), hex_str);
168template <
typename InIter>
169std::string bytes_to_hex_string(InIter first, InIter last) {
171 bytes_to_hex_string(first, last, hex_str);
175template <
typename InContainer>
176std::string bytes_to_hex_string(
const InContainer& bytes) {
178 bytes_to_hex_string(bytes, hex_str);
182class hash256_one_by_one {
184 hash256_one_by_one() { init(); }
188 std::fill(data_length_digits_, data_length_digits_ + 4, 0);
189 std::copy(detail::initial_message_digest,
190 detail::initial_message_digest + 8, h_);
193 template <
typename RaIter>
194 void process(RaIter first, RaIter last) {
195 add_to_data_size(std::distance(first, last));
196 std::copy(first, last, std::back_inserter(buffer_));
198 for (; i + 64 <= buffer_.size(); i += 64) {
199 detail::hash256_block(h_, buffer_.begin() + i,
200 buffer_.begin() + i + 64);
202 buffer_.erase(buffer_.begin(), buffer_.begin() + i);
207 std::fill(temp, temp + 64, 0);
208 std::size_t remains = buffer_.size();
209 std::copy(buffer_.begin(), buffer_.end(), temp);
210 temp[remains] = 0x80;
213 std::fill(temp + remains + 1, temp + 64, 0);
214 detail::hash256_block(h_, temp, temp + 64);
215 std::fill(temp, temp + 64 - 4, 0);
217 std::fill(temp + remains + 1, temp + 64 - 4, 0);
220 write_data_bit_size(&(temp[56]));
221 detail::hash256_block(h_, temp, temp + 64);
224 template <
typename OutIter>
225 void get_hash_bytes(OutIter first, OutIter last)
const {
226 for (
const word_t* iter = h_; iter != h_ + 8; ++iter) {
227 for (std::size_t i = 0; i < 4 && first != last; ++i) {
228 *(first++) = detail::mask_8bit(
229 static_cast<byte_t
>((*iter >> (24 - 8 * i))));
235 void add_to_data_size(word_t n) {
237 data_length_digits_[0] += n;
238 for (std::size_t i = 0; i < 4; ++i) {
239 data_length_digits_[i] += carry;
240 if (data_length_digits_[i] >= 65536u) {
241 carry = data_length_digits_[i] >> 16;
242 data_length_digits_[i] &= 65535u;
248 void write_data_bit_size(byte_t* begin) {
249 word_t data_bit_length_digits[4];
250 std::copy(data_length_digits_, data_length_digits_ + 4,
251 data_bit_length_digits);
255 for (std::size_t i = 0; i < 4; ++i) {
256 word_t before_val = data_bit_length_digits[i];
257 data_bit_length_digits[i] <<= 3;
258 data_bit_length_digits[i] |= carry;
259 data_bit_length_digits[i] &= 65535u;
260 carry = (before_val >> (16 - 3)) & 65535u;
264 for (
int i = 3; i >= 0; --i) {
265 (*begin++) =
static_cast<byte_t
>(data_bit_length_digits[i] >> 8);
266 (*begin++) =
static_cast<byte_t
>(data_bit_length_digits[i]);
269 std::vector<byte_t> buffer_;
270 word_t data_length_digits_[4];
275 std::string& hex_str) {
277 hasher.get_hash_bytes(hash, hash + 32);
278 return bytes_to_hex_string(hash, hash + 32, hex_str);
281inline std::string get_hash_hex_string(
const hash256_one_by_one& hasher) {
283 get_hash_hex_string(hasher, hex_str);
288template <
typename RaIter,
typename OutIter>
289void hash256_impl(RaIter first, RaIter last, OutIter first2, OutIter last2,
int,
290 std::random_access_iterator_tag) {
291 hash256_one_by_one hasher;
293 hasher.process(first, last);
295 hasher.get_hash_bytes(first2, last2);
298template <
typename InputIter,
typename OutIter>
299void hash256_impl(InputIter first, InputIter last, OutIter first2,
300 OutIter last2,
int buffer_size, std::input_iterator_tag) {
301 std::vector<byte_t> buffer(buffer_size);
302 hash256_one_by_one hasher;
304 while (first != last) {
305 int size = buffer_size;
306 for (
int i = 0; i != buffer_size; ++i, ++first) {
313 hasher.process(buffer.begin(), buffer.begin() + size);
316 hasher.get_hash_bytes(first2, last2);
320template <
typename InIter,
typename OutIter>
321void hash256(InIter first, InIter last, OutIter first2, OutIter last2,
322 int buffer_size = PICOSHA2_BUFFER_SIZE_FOR_INPUT_ITERATOR) {
323 picosha2::impl::hash256_impl(
324 first, last, first2, last2, buffer_size,
325 typename std::iterator_traits<InIter>::iterator_category());
328template <
typename InIter,
typename OutContainer>
329void hash256(InIter first, InIter last, OutContainer& dst) {
330 hash256(first, last, dst.begin(), dst.end());
333template <
typename InContainer,
typename OutIter>
334void hash256(
const InContainer& src, OutIter first, OutIter last) {
335 hash256(src.begin(), src.end(), first, last);
338template <
typename InContainer,
typename OutContainer>
339void hash256(
const InContainer& src, OutContainer& dst) {
340 hash256(src.begin(), src.end(), dst.begin(), dst.end());
343template <
typename InIter>
344void hash256_hex_string(InIter first, InIter last, std::string& hex_str) {
346 hash256(first, last, hashed, hashed + 32);
347 std::ostringstream oss;
348 output_hex(hashed, hashed + 32, oss);
349 hex_str.assign(oss.str());
352template <
typename InIter>
353std::string hash256_hex_string(InIter first, InIter last) {
355 hash256_hex_string(first, last, hex_str);
359inline void hash256_hex_string(
const std::string& src, std::string& hex_str) {
360 hash256_hex_string(src.begin(), src.end(), hex_str);
363template <
typename InContainer>
364void hash256_hex_string(
const InContainer& src, std::string& hex_str) {
365 hash256_hex_string(src.begin(), src.end(), hex_str);
368template <
typename InContainer>
369std::string hash256_hex_string(
const InContainer& src) {
370 return hash256_hex_string(src.begin(), src.end());