49 static const uint32_t add_constant[64] = {
50 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
51 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
52 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
53 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
54 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
55 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
56 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
57 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
58 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
59 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
60 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
61 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
62 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
63 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
64 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
65 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
68 static const uint32_t initial_message_digest[8] = {
69 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
70 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
73 inline uint32_t
ch(uint32_t
x, uint32_t y, uint32_t
z){
74 return (x&y)^((~x)&z);
77 inline uint32_t
maj(uint32_t
x, uint32_t y, uint32_t
z){
78 return (x&y)^(x&
z)^(y&z);
81 inline uint32_t
rotr(uint32_t
x, std::size_t n){
94 inline uint32_t
shr(uint32_t
x, std::size_t n){
107 template<
typename RaIter1,
typename RaIter2>
111 std::fill(w, w+64, 0);
112 for(std::size_t i = 0; i < 16; ++i){
113 w[i] = (
static_cast<uint32_t
>(
mask_8bit(*(first+i*4)))<<24)
114 |(
static_cast<uint32_t
>(
mask_8bit(*(first+i*4+1)))<<16)
115 |(
static_cast<uint32_t
>(
mask_8bit(*(first+i*4+2)))<<8)
116 |(
static_cast<uint32_t
>(
mask_8bit(*(first+i*4+3))));
118 for(std::size_t i = 16; i < 64; ++i){
122 uint32_t
a = *message_digest;
123 uint32_t
b = *(message_digest+1);
124 uint32_t
c = *(message_digest+2);
125 uint32_t
d = *(message_digest+3);
126 uint32_t
e = *(message_digest+4);
127 uint32_t
f = *(message_digest+5);
128 uint32_t
g = *(message_digest+6);
129 uint32_t
h = *(message_digest+7);
131 for(std::size_t i = 0; i < 64; ++i){
132 uint32_t temp1 = h+
bsig1(e)+
ch(e,f,g)+add_constant[i]+w[i];
133 uint32_t temp2 =
bsig0(a)+
maj(a,b,c);
143 *message_digest +=
a;
144 *(message_digest+1) += b;
145 *(message_digest+2) += c;
146 *(message_digest+3) += d;
147 *(message_digest+4) += e;
148 *(message_digest+5) += f;
149 *(message_digest+6) += g;
150 *(message_digest+7) += h;
151 for(std::size_t i = 0; i < 8; ++i){
152 *(message_digest+i) =
mask_32bit(*(message_digest+i));
158 template<
typename InIter>
159 void output_hex(InIter first, InIter last, std::ostream& os){
160 os.setf(std::ios::hex, std::ios::basefield);
161 while(first != last){
164 os << static_cast<unsigned int>(*first);
167 os.setf(std::ios::dec, std::ios::basefield);
170 template<
typename InIter>
172 std::ostringstream oss;
174 hex_str.assign(oss.str());
177 template<
typename InContainer>
182 template<
typename InIter>
189 template<
typename InContainer>
204 std::fill(data_length_digits_, data_length_digits_+4, 0);
205 std::copy(detail::initial_message_digest, detail::initial_message_digest+8, h_);
208 template<
typename RaIter>
210 add_to_data_length(std::distance(first, last));
211 std::copy(first, last, std::back_inserter(buffer_));
213 for(;i+64 <= buffer_.size(); i+=64){
216 buffer_.erase(buffer_.begin(), buffer_.begin()+i);
221 std::fill(temp, temp+64, 0);
222 std::size_t remains = buffer_.size();
223 std::copy(buffer_.begin(), buffer_.end(), temp);
224 temp[remains] = 0x80;
227 std::fill(temp+remains+1, temp+64, 0);
229 std::fill(temp, temp+64-4, 0);
232 std::fill(temp+remains+1, temp+64-4, 0);
235 write_data_bit_length(&(temp[56]));
239 template<
typename OutIter>
241 for(
const uint32_t* iter = h_; iter != h_+8; ++iter){
242 for(std::size_t i = 0; i < 4 && first != last; ++i){
251 data_length_digits_[0] += n;
252 for(std::size_t i = 0; i < 4; ++i) {
253 data_length_digits_[i] += carry;
254 if(data_length_digits_[i] >= 65536u) {
255 data_length_digits_[i] -= 65536u;
264 uint32_t data_bit_length_digits[4];
266 data_length_digits_, data_length_digits_+4,
267 data_bit_length_digits
272 for(std::size_t i = 0; i < 4; ++i) {
273 uint32_t before_val = data_bit_length_digits[i];
274 data_bit_length_digits[i] <<= 3;
275 data_bit_length_digits[i] |= carry;
276 data_bit_length_digits[i] &= 65535u;
277 carry = (before_val >> (16-3)) & 65535u;
281 for(
int i = 3; i >= 0; --i) {
282 (*begin++) = static_cast<uint8_t>(data_bit_length_digits[i] >> 8);
283 (*begin++) = static_cast<uint8_t>(data_bit_length_digits[i]);
287 uint32_t data_length_digits_[4];
303 template<
typename RaIter,
typename OutIter>
304 void hash256(RaIter first, RaIter last, OutIter first2, OutIter last2){
312 template<
typename RaIter,
typename OutContainer>
313 void hash256(RaIter first, RaIter last, OutContainer& dst){
314 hash256(first, last, dst.begin(), dst.end());
317 template<
typename RaContainer,
typename OutIter>
318 void hash256(
const RaContainer& src, OutIter first, OutIter last){
319 hash256(src.begin(), src.end(), first, last);
322 template<
typename RaContainer,
typename OutContainer>
323 void hash256(
const RaContainer& src, OutContainer& dst){
324 hash256(src.begin(), src.end(), dst.begin(), dst.end());
328 template<
typename RaIter>
331 hash256(first, last, hashed, hashed+32);
332 std::ostringstream oss;
334 hex_str.assign(oss.str());
337 template<
typename RaIter>
348 template<
typename RaContainer>
353 template<
typename RaContainer>
void bytes_to_hex_string(InIter first, InIter last, std::string &hex_str)
uint32_t maj(uint32_t x, uint32_t y, uint32_t z)
uint8_t mask_8bit(uint8_t x)
void hash256(RaIter first, RaIter last, OutIter first2, OutIter last2)
void hash256_hex_string(RaIter first, RaIter last, std::string &hex_str)
void get_hash_bytes(OutIter first, OutIter last) const
assert(len-trim+(2 *lenIndices)<=WIDTH)
void add_to_data_length(uint32_t n)
void process(RaIter first, RaIter last)
uint32_t mask_32bit(uint32_t x)
std::vector< uint8_t > buffer_
void output_hex(InIter first, InIter last, std::ostream &os)
void get_hash_hex_string(const hash256_one_by_one &hasher, std::string &hex_str)
uint32_t shr(uint32_t x, std::size_t n)
std::vector< byte > bytes
uint32_t rotr(uint32_t x, std::size_t n)
uint32_t ssig1(uint32_t x)
void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last)
uint32_t bsig1(uint32_t x)
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
void write_data_bit_length(uint8_t *begin)
uint32_t ssig0(uint32_t x)
uint32_t bsig0(uint32_t x)