diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-01-18 18:44:21 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-01-18 18:44:21 +0300 |
commit | 0074fcbdce03fec5691282ec55b2cb617c7f9427 (patch) | |
tree | d707705615a530c2a42760fcab3155b47b46fafa | |
parent | 6ed5b52d3e41a706904e05277afad92087a54784 (diff) | |
download | libmicrohttpd-0074fcbdce03fec5691282ec55b2cb617c7f9427.tar.gz libmicrohttpd-0074fcbdce03fec5691282ec55b2cb617c7f9427.zip |
digestauth: Moved hex printing function to mhd_str
-rw-r--r-- | src/microhttpd/digestauth.c | 62 | ||||
-rw-r--r-- | src/microhttpd/mhd_str.c | 21 | ||||
-rw-r--r-- | src/microhttpd/mhd_str.h | 13 |
3 files changed, 52 insertions, 44 deletions
diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c index 81ca2ac9..0d7a62e6 100644 --- a/src/microhttpd/digestauth.c +++ b/src/microhttpd/digestauth.c | |||
@@ -184,32 +184,6 @@ struct DigestAlgorithm | |||
184 | 184 | ||
185 | 185 | ||
186 | /** | 186 | /** |
187 | * convert bin to hex | ||
188 | * | ||
189 | * @param bin binary data | ||
190 | * @param len number of bytes in bin | ||
191 | * @param hex pointer to len*2+1 bytes | ||
192 | */ | ||
193 | static void | ||
194 | cvthex (const unsigned char *bin, | ||
195 | size_t len, | ||
196 | char *hex) | ||
197 | { | ||
198 | size_t i; | ||
199 | unsigned int j; | ||
200 | |||
201 | for (i = 0; i < len; ++i) | ||
202 | { | ||
203 | j = (bin[i] >> 4) & 0x0f; | ||
204 | hex[i * 2] = (char) ((j <= 9) ? (j + '0') : (j - 10 + 'a')); | ||
205 | j = bin[i] & 0x0f; | ||
206 | hex[i * 2 + 1] = (char) ((j <= 9) ? (j + '0') : (j - 10 + 'a')); | ||
207 | } | ||
208 | hex[len * 2] = '\0'; | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * calculate H(A1) from given hash as per RFC2617 spec | 187 | * calculate H(A1) from given hash as per RFC2617 spec |
214 | * and store the * result in 'sessionkey'. | 188 | * and store the * result in 'sessionkey'. |
215 | * | 189 | * |
@@ -258,15 +232,15 @@ digest_calc_ha1_from_digest (const char *alg, | |||
258 | strlen (cnonce)); | 232 | strlen (cnonce)); |
259 | da->digest (da->ctx, | 233 | da->digest (da->ctx, |
260 | dig); | 234 | dig); |
261 | cvthex (dig, | 235 | MHD_bin_to_hex (dig, |
262 | digest_size, | 236 | digest_size, |
263 | da->sessionkey); | 237 | da->sessionkey); |
264 | } | 238 | } |
265 | else | 239 | else |
266 | { | 240 | { |
267 | cvthex (digest, | 241 | MHD_bin_to_hex (digest, |
268 | digest_size, | 242 | digest_size, |
269 | da->sessionkey); | 243 | da->sessionkey); |
270 | } | 244 | } |
271 | } | 245 | } |
272 | 246 | ||
@@ -383,9 +357,9 @@ digest_calc_response (const char *ha1, | |||
383 | #endif | 357 | #endif |
384 | da->digest (da->ctx, | 358 | da->digest (da->ctx, |
385 | ha2); | 359 | ha2); |
386 | cvthex (ha2, | 360 | MHD_bin_to_hex (ha2, |
387 | digest_size, | 361 | digest_size, |
388 | da->sessionkey); | 362 | da->sessionkey); |
389 | da->init (da->ctx); | 363 | da->init (da->ctx); |
390 | /* calculate response */ | 364 | /* calculate response */ |
391 | da->update (da->ctx, | 365 | da->update (da->ctx, |
@@ -426,9 +400,9 @@ digest_calc_response (const char *ha1, | |||
426 | digest_size * 2); | 400 | digest_size * 2); |
427 | da->digest (da->ctx, | 401 | da->digest (da->ctx, |
428 | resphash); | 402 | resphash); |
429 | cvthex (resphash, | 403 | MHD_bin_to_hex (resphash, |
430 | digest_size, | 404 | digest_size, |
431 | da->sessionkey); | 405 | da->sessionkey); |
432 | } | 406 | } |
433 | 407 | ||
434 | 408 | ||
@@ -736,12 +710,12 @@ calculate_nonce (uint32_t nonce_time, | |||
736 | strlen (realm)); | 710 | strlen (realm)); |
737 | da->digest (da->ctx, | 711 | da->digest (da->ctx, |
738 | tmpnonce); | 712 | tmpnonce); |
739 | cvthex (tmpnonce, | 713 | MHD_bin_to_hex (tmpnonce, |
740 | digest_size, | 714 | digest_size, |
741 | nonce); | 715 | nonce); |
742 | cvthex (timestamp, | 716 | MHD_bin_to_hex (timestamp, |
743 | sizeof (timestamp), | 717 | sizeof (timestamp), |
744 | nonce + digest_size * 2); | 718 | nonce + digest_size * 2); |
745 | } | 719 | } |
746 | 720 | ||
747 | 721 | ||
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c index 822feccc..08781cb9 100644 --- a/src/microhttpd/mhd_str.c +++ b/src/microhttpd/mhd_str.c | |||
@@ -1354,3 +1354,24 @@ MHD_uint8_to_str_pad (uint8_t val, | |||
1354 | buf[pos++] = '0' + val; | 1354 | buf[pos++] = '0' + val; |
1355 | return pos; | 1355 | return pos; |
1356 | } | 1356 | } |
1357 | |||
1358 | |||
1359 | size_t | ||
1360 | MHD_bin_to_hex (const void *bin, | ||
1361 | size_t size, | ||
1362 | char *hex) | ||
1363 | { | ||
1364 | size_t i; | ||
1365 | |||
1366 | for (i = 0; i < size; ++i) | ||
1367 | { | ||
1368 | uint8_t j; | ||
1369 | const uint8_t b = ((uint8_t *) bin)[i]; | ||
1370 | j = b >> 4; | ||
1371 | hex[i * 2] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a')); | ||
1372 | j = b & 0x0f; | ||
1373 | hex[i * 2 + 1] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a')); | ||
1374 | } | ||
1375 | hex[i * 2] = 0; | ||
1376 | return i; | ||
1377 | } | ||
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h index 09c63578..6455d84e 100644 --- a/src/microhttpd/mhd_str.h +++ b/src/microhttpd/mhd_str.h | |||
@@ -457,5 +457,18 @@ MHD_uint8_to_str_pad (uint8_t val, | |||
457 | char *buf, | 457 | char *buf, |
458 | size_t buf_size); | 458 | size_t buf_size); |
459 | 459 | ||
460 | /** | ||
461 | * Convert @a size bytes from input binary data to lower case | ||
462 | * hexadecimal digits, zero-terminate the result. | ||
463 | * @param bin the pointer to the binary data to convert | ||
464 | * @param size the size in bytes of the binary data to convert | ||
465 | * @param hex the output buffer, but be at least 2 * @a size + 1 | ||
466 | * @return The number of characters written to the output buffer, | ||
467 | * not including terminating zero. | ||
468 | */ | ||
469 | size_t | ||
470 | MHD_bin_to_hex (const void *bin, | ||
471 | size_t size, | ||
472 | char *hex); | ||
460 | 473 | ||
461 | #endif /* MHD_STR_H */ | 474 | #endif /* MHD_STR_H */ |