|
| 1 | +/* |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (C) 2014 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 | +#pragma once |
| 25 | + |
| 26 | +//picosha2:20140213 |
| 27 | +#include <cstdint> |
| 28 | +#include <iostream> |
| 29 | +#include <vector> |
| 30 | +#include <iterator> |
| 31 | +#include <cassert> |
| 32 | +#include <sstream> |
| 33 | +#include <algorithm> |
| 34 | + |
| 35 | +namespace picosha2 |
| 36 | +{ |
| 37 | + |
| 38 | +namespace detail |
| 39 | +{ |
| 40 | + |
| 41 | +inline uint8_t mask_8bit(uint8_t x) |
| 42 | +{ |
| 43 | + return x&0xff; |
| 44 | +} |
| 45 | + |
| 46 | +inline uint32_t mask_32bit(uint32_t x) |
| 47 | +{ |
| 48 | + return x&0xffffffff; |
| 49 | +} |
| 50 | + |
| 51 | +static const uint32_t add_constant[64] = { |
| 52 | + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
| 53 | + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 54 | + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
| 55 | + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 56 | + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
| 57 | + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 58 | + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| 59 | + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 60 | + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
| 61 | + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 62 | + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
| 63 | + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 64 | + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
| 65 | + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 66 | + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
| 67 | + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
| 68 | +}; |
| 69 | + |
| 70 | +static const uint32_t initial_message_digest[8] = { |
| 71 | + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
| 72 | + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 |
| 73 | +}; |
| 74 | + |
| 75 | +inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z) |
| 76 | +{ |
| 77 | + return (x&y)^((~x)&z); |
| 78 | +} |
| 79 | + |
| 80 | +inline uint32_t maj(uint32_t x, uint32_t y, uint32_t z) |
| 81 | +{ |
| 82 | + return (x&y)^(x&z)^(y&z); |
| 83 | +} |
| 84 | + |
| 85 | +inline uint32_t rotr(uint32_t x, std::size_t n) |
| 86 | +{ |
| 87 | + assert(n < 32); |
| 88 | + return mask_32bit((x>>n)|(x<<(32-n))); |
| 89 | +} |
| 90 | + |
| 91 | +inline uint32_t bsig0(uint32_t x) |
| 92 | +{ |
| 93 | + return rotr(x, 2)^rotr(x, 13)^rotr(x, 22); |
| 94 | +} |
| 95 | + |
| 96 | +inline uint32_t bsig1(uint32_t x) |
| 97 | +{ |
| 98 | + return rotr(x, 6)^rotr(x, 11)^rotr(x, 25); |
| 99 | +} |
| 100 | + |
| 101 | +inline uint32_t shr(uint32_t x, std::size_t n) |
| 102 | +{ |
| 103 | + assert(n < 32); |
| 104 | + return x >> n; |
| 105 | +} |
| 106 | + |
| 107 | +inline uint32_t ssig0(uint32_t x) |
| 108 | +{ |
| 109 | + return rotr(x, 7)^rotr(x, 18)^shr(x, 3); |
| 110 | +} |
| 111 | + |
| 112 | +inline uint32_t ssig1(uint32_t x) |
| 113 | +{ |
| 114 | + return rotr(x, 17)^rotr(x, 19)^shr(x, 10); |
| 115 | +} |
| 116 | + |
| 117 | +template<typename RaIter1, typename RaIter2> |
| 118 | +void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) |
| 119 | +{ |
| 120 | + (void)last; // FIXME: check this is valid |
| 121 | + uint32_t w[64]; |
| 122 | + std::fill(w, w+64, 0); |
| 123 | + for (std::size_t i = 0; i < 16; ++i) |
| 124 | + w[i] = (static_cast<uint32_t>(mask_8bit(*(first+i*4)))<<24) |
| 125 | + |(static_cast<uint32_t>(mask_8bit(*(first+i*4+1)))<<16) |
| 126 | + |(static_cast<uint32_t>(mask_8bit(*(first+i*4+2)))<<8) |
| 127 | + |(static_cast<uint32_t>(mask_8bit(*(first+i*4+3)))); |
| 128 | + for (std::size_t i = 16; i < 64; ++i) |
| 129 | + w[i] = mask_32bit(ssig1(w[i-2])+w[i-7]+ssig0(w[i-15])+w[i-16]); |
| 130 | + |
| 131 | + uint32_t a = *message_digest; |
| 132 | + uint32_t b = *(message_digest+1); |
| 133 | + uint32_t c = *(message_digest+2); |
| 134 | + uint32_t d = *(message_digest+3); |
| 135 | + uint32_t e = *(message_digest+4); |
| 136 | + uint32_t f = *(message_digest+5); |
| 137 | + uint32_t g = *(message_digest+6); |
| 138 | + uint32_t h = *(message_digest+7); |
| 139 | + |
| 140 | + for (std::size_t i = 0; i < 64; ++i) |
| 141 | + { |
| 142 | + uint32_t temp1 = h+bsig1(e)+ch(e,f,g)+add_constant[i]+w[i]; |
| 143 | + uint32_t temp2 = bsig0(a)+maj(a,b,c); |
| 144 | + h = g; |
| 145 | + g = f; |
| 146 | + f = e; |
| 147 | + e = mask_32bit(d+temp1); |
| 148 | + d = c; |
| 149 | + c = b; |
| 150 | + b = a; |
| 151 | + a = mask_32bit(temp1+temp2); |
| 152 | + } |
| 153 | + *message_digest += a; |
| 154 | + *(message_digest+1) += b; |
| 155 | + *(message_digest+2) += c; |
| 156 | + *(message_digest+3) += d; |
| 157 | + *(message_digest+4) += e; |
| 158 | + *(message_digest+5) += f; |
| 159 | + *(message_digest+6) += g; |
| 160 | + *(message_digest+7) += h; |
| 161 | + for (std::size_t i = 0; i < 8; ++i) |
| 162 | + *(message_digest+i) = mask_32bit(*(message_digest+i)); |
| 163 | +} |
| 164 | + |
| 165 | +}//namespace detail |
| 166 | + |
| 167 | +class hash256_one_by_one |
| 168 | +{ |
| 169 | +public: |
| 170 | + hash256_one_by_one() |
| 171 | + { |
| 172 | + init(); |
| 173 | + } |
| 174 | + |
| 175 | + void init() |
| 176 | + { |
| 177 | + buffer_.clear(); |
| 178 | + std::fill(data_length_digits_, data_length_digits_+4, 0); |
| 179 | + std::copy(detail::initial_message_digest, detail::initial_message_digest+8, h_); |
| 180 | + } |
| 181 | + |
| 182 | + template<typename RaIter> |
| 183 | + void process(RaIter first, RaIter last) |
| 184 | + { |
| 185 | + add_to_data_length(std::distance(first, last)); |
| 186 | + std::copy(first, last, std::back_inserter(buffer_)); |
| 187 | + std::size_t i = 0; |
| 188 | + for (;i+64 <= buffer_.size(); i+=64) |
| 189 | + detail::hash256_block(h_, buffer_.begin()+i, buffer_.begin()+i+64); |
| 190 | + buffer_.erase(buffer_.begin(), buffer_.begin()+i); |
| 191 | + } |
| 192 | + |
| 193 | + void finish() |
| 194 | + { |
| 195 | + uint8_t temp[64]; |
| 196 | + std::fill(temp, temp+64, 0); |
| 197 | + std::size_t remains = buffer_.size(); |
| 198 | + std::copy(buffer_.begin(), buffer_.end(), temp); |
| 199 | + temp[remains] = 0x80; |
| 200 | + |
| 201 | + if(remains > 55) |
| 202 | + { |
| 203 | + std::fill(temp+remains+1, temp+64, 0); |
| 204 | + detail::hash256_block(h_, temp, temp+64); |
| 205 | + std::fill(temp, temp+64-4, 0); |
| 206 | + } |
| 207 | + else |
| 208 | + std::fill(temp+remains+1, temp+64-4, 0); |
| 209 | + |
| 210 | + write_data_bit_length(&(temp[56])); |
| 211 | + detail::hash256_block(h_, temp, temp+64); |
| 212 | + } |
| 213 | + |
| 214 | + template<typename OutIter> |
| 215 | + void get_hash_bytes(OutIter first, OutIter last) const |
| 216 | + { |
| 217 | + for (const uint32_t* iter = h_; iter != h_+8; ++iter) |
| 218 | + for (std::size_t i = 0; i < 4 && first != last; ++i) |
| 219 | + *(first++) = detail::mask_8bit(static_cast<uint8_t>((*iter >> (24-8*i)))); |
| 220 | + } |
| 221 | + |
| 222 | +private: |
| 223 | + void add_to_data_length(uint32_t n) |
| 224 | + { |
| 225 | + uint32_t carry = 0; |
| 226 | + data_length_digits_[0] += n; |
| 227 | + for (std::size_t i = 0; i < 4; ++i) |
| 228 | + { |
| 229 | + data_length_digits_[i] += carry; |
| 230 | + if(data_length_digits_[i] >= 65536u) |
| 231 | + { |
| 232 | + data_length_digits_[i] -= 65536u; |
| 233 | + carry = 1; |
| 234 | + } |
| 235 | + else |
| 236 | + break; |
| 237 | + } |
| 238 | + } |
| 239 | + void write_data_bit_length(uint8_t* begin) |
| 240 | + { |
| 241 | + uint32_t data_bit_length_digits[4]; |
| 242 | + std::copy( |
| 243 | + data_length_digits_, data_length_digits_+4, |
| 244 | + data_bit_length_digits |
| 245 | + ); |
| 246 | + |
| 247 | + // convert byte length to bit length (multiply 8 or shift 3 times left) |
| 248 | + uint32_t carry = 0; |
| 249 | + for (std::size_t i = 0; i < 4; ++i) |
| 250 | + { |
| 251 | + uint32_t before_val = data_bit_length_digits[i]; |
| 252 | + data_bit_length_digits[i] <<= 3; |
| 253 | + data_bit_length_digits[i] |= carry; |
| 254 | + data_bit_length_digits[i] &= 65535u; |
| 255 | + carry = (before_val >> (16-3)) & 65535u; |
| 256 | + } |
| 257 | + |
| 258 | + // write data_bit_length |
| 259 | + for (int i = 3; i >= 0; --i) |
| 260 | + { |
| 261 | + (*begin++) = static_cast<uint8_t>(data_bit_length_digits[i] >> 8); |
| 262 | + (*begin++) = static_cast<uint8_t>(data_bit_length_digits[i]); |
| 263 | + } |
| 264 | + } |
| 265 | + std::vector<uint8_t> buffer_; |
| 266 | + uint32_t data_length_digits_[4]; //as 64bit integer (16bit x 4 integer) |
| 267 | + uint32_t h_[8]; |
| 268 | +}; |
| 269 | + |
| 270 | +template<typename RaIter, typename OutIter> |
| 271 | +void hash256(RaIter first, RaIter last, OutIter first2, OutIter last2) |
| 272 | +{ |
| 273 | + hash256_one_by_one hasher; |
| 274 | + //hasher.init(); |
| 275 | + hasher.process(first, last); |
| 276 | + hasher.finish(); |
| 277 | + hasher.get_hash_bytes(first2, last2); |
| 278 | +} |
| 279 | + |
| 280 | +template <typename RaContainer> |
| 281 | +std::vector<uint8_t> hash256(RaContainer const& _src) |
| 282 | +{ |
| 283 | + std::vector<uint8_t> ret(32); |
| 284 | + hash256(_src.begin(), _src.end(), ret.begin(), ret.end()); |
| 285 | + return ret; |
| 286 | +} |
| 287 | + |
| 288 | +}//namespace picosha2 |
0 commit comments