11 #include <restinio/utils/impl/bitops.hpp> 68 template<
unsigned int Shift >
75 template<
unsigned int Shift >
82 static uint32_t blk(
const int_block_t & block,
const size_t i)
84 return rotate_left<1>(
85 block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i] );
89 R0(
const int_block_t & block,
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
91 z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rotate_left<5>( v );
92 w = rotate_left<30>( w );
97 R1(int_block_t & block,
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
99 block[i] = blk(block, i);
100 z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rotate_left<5>(v);
101 w = rotate_left<30>(w);
106 R2(int_block_t & block,
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
108 block[i] = blk(block, i);
109 z += (w^x^y) + block[i] + 0x6ed9eba1 + rotate_left<5>(v);
110 w = rotate_left<30>(w);
114 R3(int_block_t & block,
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
116 block[i] = blk(block, i);
117 z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rotate_left<5>(v);
118 w = rotate_left<30>(w);
123 R4(int_block_t & block,
const uint32_t v, uint32_t &w,
const uint32_t x,
const uint32_t y, uint32_t &z,
const size_t i)
125 block[i] = blk(block, i);
126 z += (w^x^y) + block[i] + 0xca62c1d6 + rotate_left<5>(v);
127 w = rotate_left<30>(w);
131 transform( digest_t & digest,
const byte_block_t & buf )
135 for (size_t i = 0; i < block_ints; i++)
138 as_uint32(buf[4*i+3] & 0xff) |
139 as_uint32(buf[4*i+2] & 0xff)<<8 |
140 as_uint32(buf[4*i+1] & 0xff)<<16 |
141 as_uint32(buf[4*i+0] & 0xff)<<24;
144 std::uint32_t a = digest[0];
145 std::uint32_t b = digest[1];
146 std::uint32_t c = digest[2];
147 std::uint32_t d = digest[3];
148 std::uint32_t e = digest[4];
150 R0(block, a, b, c, d, e, 0);
151 R0(block, e, a, b, c, d, 1);
152 R0(block, d, e, a, b, c, 2);
153 R0(block, c, d, e, a, b, 3);
154 R0(block, b, c, d, e, a, 4);
155 R0(block, a, b, c, d, e, 5);
156 R0(block, e, a, b, c, d, 6);
157 R0(block, d, e, a, b, c, 7);
158 R0(block, c, d, e, a, b, 8);
159 R0(block, b, c, d, e, a, 9);
160 R0(block, a, b, c, d, e, 10);
161 R0(block, e, a, b, c, d, 11);
162 R0(block, d, e, a, b, c, 12);
163 R0(block, c, d, e, a, b, 13);
164 R0(block, b, c, d, e, a, 14);
165 R0(block, a, b, c, d, e, 15);
166 R1(block, e, a, b, c, d, 0);
167 R1(block, d, e, a, b, c, 1);
168 R1(block, c, d, e, a, b, 2);
169 R1(block, b, c, d, e, a, 3);
170 R2(block, a, b, c, d, e, 4);
171 R2(block, e, a, b, c, d, 5);
172 R2(block, d, e, a, b, c, 6);
173 R2(block, c, d, e, a, b, 7);
174 R2(block, b, c, d, e, a, 8);
175 R2(block, a, b, c, d, e, 9);
176 R2(block, e, a, b, c, d, 10);
177 R2(block, d, e, a, b, c, 11);
178 R2(block, c, d, e, a, b, 12);
179 R2(block, b, c, d, e, a, 13);
180 R2(block, a, b, c, d, e, 14);
181 R2(block, e, a, b, c, d, 15);
182 R2(block, d, e, a, b, c, 0);
183 R2(block, c, d, e, a, b, 1);
184 R2(block, b, c, d, e, a, 2);
185 R2(block, a, b, c, d, e, 3);
186 R2(block, e, a, b, c, d, 4);
187 R2(block, d, e, a, b, c, 5);
188 R2(block, c, d, e, a, b, 6);
189 R2(block, b, c, d, e, a, 7);
190 R3(block, a, b, c, d, e, 8);
191 R3(block, e, a, b, c, d, 9);
192 R3(block, d, e, a, b, c, 10);
193 R3(block, c, d, e, a, b, 11);
194 R3(block, b, c, d, e, a, 12);
195 R3(block, a, b, c, d, e, 13);
196 R3(block, e, a, b, c, d, 14);
197 R3(block, d, e, a, b, c, 15);
198 R3(block, c, d, e, a, b, 0);
199 R3(block, b, c, d, e, a, 1);
200 R3(block, a, b, c, d, e, 2);
201 R3(block, e, a, b, c, d, 3);
202 R3(block, d, e, a, b, c, 4);
203 R3(block, c, d, e, a, b, 5);
204 R3(block, b, c, d, e, a, 6);
205 R3(block, a, b, c, d, e, 7);
206 R3(block, e, a, b, c, d, 8);
207 R3(block, d, e, a, b, c, 9);
208 R3(block, c, d, e, a, b, 10);
209 R3(block, b, c, d, e, a, 11);
210 R4(block, a, b, c, d, e, 12);
211 R4(block, e, a, b, c, d, 13);
212 R4(block, d, e, a, b, c, 14);
213 R4(block, c, d, e, a, b, 15);
214 R4(block, b, c, d, e, a, 0);
215 R4(block, a, b, c, d, e, 1);
216 R4(block, e, a, b, c, d, 2);
217 R4(block, d, e, a, b, c, 3);
218 R4(block, c, d, e, a, b, 4);
219 R4(block, b, c, d, e, a, 5);
220 R4(block, a, b, c, d, e, 6);
221 R4(block, e, a, b, c, d, 7);
222 R4(block, d, e, a, b, c, 8);
223 R4(block, c, d, e, a, b, 9);
224 R4(block, b, c, d, e, a, 10);
225 R4(block, a, b, c, d, e, 11);
226 R4(block, e, a, b, c, d, 12);
227 R4(block, d, e, a, b, c, 13);
228 R4(block, c, d, e, a, b, 14);
229 R4(block, b, c, d, e, a, 15);
254 update(
const std::uint8_t * what, std::size_t length )
258 auto part_len = std::min( length, block_size - m_buffer_len );
260 std::copy( what, what + part_len, m_buffer.begin() + m_buffer_len );
261 m_buffer_len += part_len;
263 if( m_buffer_len != block_size )
270 transform( m_digest, m_buffer );
282 const auto total_bits = calculate_total_bits_count();
286 m_buffer[ m_buffer_len ++ ] = 0x80;
288 while( m_buffer_len < block_size )
289 m_buffer[m_buffer_len++] = 0x00;
291 if( original_buf_len > block_size - 8 )
293 transform( m_digest, m_buffer );
295 for( size_t i = 0 ; i < block_size ; ++ i )
300 std::size_t i = block_size - 8u;
301 const auto push_uint_to_buffer = [&](
auto big_value ) {
302 const auto v =
static_cast<std::uint32_t>(big_value);
303 m_buffer[ i++ ] = octet_from<24>(v);
304 m_buffer[ i++ ] = octet_from<16>(v);
305 m_buffer[ i++ ] = octet_from<8>(v);
306 m_buffer[ i++ ] = octet_from<0>(v);
308 push_uint_to_buffer( total_bits >> 32 );
309 push_uint_to_buffer( total_bits & 0xffffffffu );
311 transform( m_digest, m_buffer );
321 return (
static_cast<std::uint_fast64_t>(m_transforms_count)
322 * block_size + m_buffer_len) * 8;
336 m_digest[0] = 0x67452301;
337 m_digest[1] = 0xEFCDAB89;
338 m_digest[2] = 0x98BADCFE;
339 m_digest[3] = 0x10325476;
340 m_digest[4] = 0xC3D2E1F0;
342 std::fill( std::begin(m_buffer), std::end(m_buffer), 0 );
356 template<
unsigned int Shift >
363 template<
unsigned int Shift >
377 static const char digits[] =
"0123456789abcdef";
380 result.reserve( digest_array_size * 8);
382 for(
const auto c : what )
384 result += digits[halfbyte<28>(c)];
385 result += digits[halfbyte<24>(c)];
386 result += digits[halfbyte<20>(c)];
387 result += digits[halfbyte<16>(c)];
388 result += digits[halfbyte<12>(c)];
389 result += digits[halfbyte<8>(c)];
390 result += digits[halfbyte<4>(c)];
391 result += digits[halfbyte<0>(c)];
403 result.reserve( digest_size );
405 for(
const auto c : what )
407 result.push_back(
static_cast<
char>(byte<24>(c)) );
408 result.push_back(
static_cast<
char>(byte<16>(c)) );
409 result.push_back(
static_cast<
char>(byte<8>(c)) );
410 result.push_back(
static_cast<
char>(byte<0>(c)) );
419 return builder_t{}.update( what, length ).finish();
436 return builder_t{}.update(
437 as_uint8_ptr( what ), length ).finish();
443 return make_digest( sv.data(), sv.size() );
unsigned int byte(digest_t::value_type v)
void R1(int_block_t &block, const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
void R3(int_block_t &block, const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
void transform(digest_t &digest, const byte_block_t &buf)
constexpr std::uint8_t word_size
builder_t & update(const std::uint8_t *what, std::size_t length)
constexpr std::uint8_t block_size
digest_t make_digest(const char *what, std::size_t length)
digest_t make_digest(string_view_t sv)
std::uint8_t octet_from(std::uint32_t x)
digest_t make_digest(const std::uint8_t *what, std::size_t length)
void R2(int_block_t &block, const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
std::uint8_t as_uint8(T what)
std::uint32_t rotate_left(const std::uint32_t x)
void R0(const int_block_t &block, const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
constexpr std::uint8_t block_ints
size_t m_transforms_count
void normalize_to(string_view_t what, char *dest)
Perform normalization of URI value.
constexpr std::uint8_t digest_size
unsigned int halfbyte(digest_t::value_type v)
std::string to_hex_string(const digest_t &what)
const std::uint8_t * as_uint8_ptr(const T *what)
void store_total_bits_to_buffer()
constexpr std::size_t digest_array_size
digest_t make_digest(const T *begin, const T *end)
std::uint32_t as_uint32(T what)
std::uint_fast64_t calculate_total_bits_count() const
std::string to_string(const digest_t &what)
void R4(int_block_t &block, const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container ¶ms, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...