39 void update(
const std::string &s);
40 void update(std::istream &is);
42 static std::string
from_file(
const std::string &filename);
55 inline static void reset(uint32_t digest[], std::string &
buffer, uint64_t &transforms)
58 digest[0] = 0x67452301;
59 digest[1] = 0xefcdab89;
60 digest[2] = 0x98badcfe;
61 digest[3] = 0x10325476;
62 digest[4] = 0xc3d2e1f0;
70 inline static uint32_t
rol(
const uint32_t value,
const size_t bits)
72 return (value << bits) | (value >> (32 - bits));
76 inline static uint32_t
blk(
const uint32_t block[
BLOCK_INTS],
const size_t i)
78 return rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1);
86 inline static void R0(
const uint32_t block[
BLOCK_INTS],
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
88 z += ((w&(x^y))^y) + block[i] + 0x5a827999 +
rol(v, 5);
93 inline static void R1(uint32_t block[
BLOCK_INTS],
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
95 block[i] =
blk(block, i);
96 z += ((w&(x^y))^y) + block[i] + 0x5a827999 +
rol(v, 5);
101 inline static void R2(uint32_t block[
BLOCK_INTS],
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
103 block[i] =
blk(block, i);
104 z += (w^x^y) + block[i] + 0x6ed9eba1 +
rol(v, 5);
109 inline static void R3(uint32_t block[
BLOCK_INTS],
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
111 block[i] =
blk(block, i);
112 z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc +
rol(v, 5);
117 inline static void R4(uint32_t block[
BLOCK_INTS],
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
119 block[i] =
blk(block, i);
120 z += (w^x^y) + block[i] + 0xca62c1d6 +
rol(v, 5);
132 uint32_t a = digest[0];
133 uint32_t b = digest[1];
134 uint32_t c = digest[2];
135 uint32_t d = digest[3];
136 uint32_t e = digest[4];
139 R0(block, a, b, c, d, e, 0);
140 R0(block, e, a, b, c, d, 1);
141 R0(block, d, e, a, b, c, 2);
142 R0(block, c, d, e, a, b, 3);
143 R0(block, b, c, d, e, a, 4);
144 R0(block, a, b, c, d, e, 5);
145 R0(block, e, a, b, c, d, 6);
146 R0(block, d, e, a, b, c, 7);
147 R0(block, c, d, e, a, b, 8);
148 R0(block, b, c, d, e, a, 9);
149 R0(block, a, b, c, d, e, 10);
150 R0(block, e, a, b, c, d, 11);
151 R0(block, d, e, a, b, c, 12);
152 R0(block, c, d, e, a, b, 13);
153 R0(block, b, c, d, e, a, 14);
154 R0(block, a, b, c, d, e, 15);
155 R1(block, e, a, b, c, d, 0);
156 R1(block, d, e, a, b, c, 1);
157 R1(block, c, d, e, a, b, 2);
158 R1(block, b, c, d, e, a, 3);
159 R2(block, a, b, c, d, e, 4);
160 R2(block, e, a, b, c, d, 5);
161 R2(block, d, e, a, b, c, 6);
162 R2(block, c, d, e, a, b, 7);
163 R2(block, b, c, d, e, a, 8);
164 R2(block, a, b, c, d, e, 9);
165 R2(block, e, a, b, c, d, 10);
166 R2(block, d, e, a, b, c, 11);
167 R2(block, c, d, e, a, b, 12);
168 R2(block, b, c, d, e, a, 13);
169 R2(block, a, b, c, d, e, 14);
170 R2(block, e, a, b, c, d, 15);
171 R2(block, d, e, a, b, c, 0);
172 R2(block, c, d, e, a, b, 1);
173 R2(block, b, c, d, e, a, 2);
174 R2(block, a, b, c, d, e, 3);
175 R2(block, e, a, b, c, d, 4);
176 R2(block, d, e, a, b, c, 5);
177 R2(block, c, d, e, a, b, 6);
178 R2(block, b, c, d, e, a, 7);
179 R3(block, a, b, c, d, e, 8);
180 R3(block, e, a, b, c, d, 9);
181 R3(block, d, e, a, b, c, 10);
182 R3(block, c, d, e, a, b, 11);
183 R3(block, b, c, d, e, a, 12);
184 R3(block, a, b, c, d, e, 13);
185 R3(block, e, a, b, c, d, 14);
186 R3(block, d, e, a, b, c, 15);
187 R3(block, c, d, e, a, b, 0);
188 R3(block, b, c, d, e, a, 1);
189 R3(block, a, b, c, d, e, 2);
190 R3(block, e, a, b, c, d, 3);
191 R3(block, d, e, a, b, c, 4);
192 R3(block, c, d, e, a, b, 5);
193 R3(block, b, c, d, e, a, 6);
194 R3(block, a, b, c, d, e, 7);
195 R3(block, e, a, b, c, d, 8);
196 R3(block, d, e, a, b, c, 9);
197 R3(block, c, d, e, a, b, 10);
198 R3(block, b, c, d, e, a, 11);
199 R4(block, a, b, c, d, e, 12);
200 R4(block, e, a, b, c, d, 13);
201 R4(block, d, e, a, b, c, 14);
202 R4(block, c, d, e, a, b, 15);
203 R4(block, b, c, d, e, a, 0);
204 R4(block, a, b, c, d, e, 1);
205 R4(block, e, a, b, c, d, 2);
206 R4(block, d, e, a, b, c, 3);
207 R4(block, c, d, e, a, b, 4);
208 R4(block, b, c, d, e, a, 5);
209 R4(block, a, b, c, d, e, 6);
210 R4(block, e, a, b, c, d, 7);
211 R4(block, d, e, a, b, c, 8);
212 R4(block, c, d, e, a, b, 9);
213 R4(block, b, c, d, e, a, 10);
214 R4(block, a, b, c, d, e, 11);
215 R4(block, e, a, b, c, d, 12);
216 R4(block, d, e, a, b, c, 13);
217 R4(block, c, d, e, a, b, 14);
218 R4(block, b, c, d, e, a, 15);
237 block[i] = (
buffer[4*i+3] & 0xff)
238 | (
buffer[4*i+2] & 0xff)<<8
239 | (
buffer[4*i+1] & 0xff)<<16
240 | (
buffer[4*i+0] & 0xff)<<24;
253 std::istringstream is(s);
264 buffer.append(sbuf, (std::size_t)is.gcount());
288 size_t orig_size =
buffer.size();
308 block[
BLOCK_INTS - 2] = (uint32_t)(total_bits >> 32);
312 std::ostringstream result;
313 for (
size_t i = 0; i <
sizeof(
digest) /
sizeof(
digest[0]); i++)
315 result << std::hex << std::setfill(
'0') << std::setw(8);
328 std::ifstream stream(filename.c_str(), std::ios::binary);