commit 0074fcbdce03fec5691282ec55b2cb617c7f9427
parent 6ed5b52d3e41a706904e05277afad92087a54784
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Tue, 18 Jan 2022 18:44:21 +0300
digestauth: Moved hex printing function to mhd_str
Diffstat:
3 files changed, 52 insertions(+), 44 deletions(-)
diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c
@@ -184,32 +184,6 @@ struct DigestAlgorithm
/**
- * convert bin to hex
- *
- * @param bin binary data
- * @param len number of bytes in bin
- * @param hex pointer to len*2+1 bytes
- */
-static void
-cvthex (const unsigned char *bin,
- size_t len,
- char *hex)
-{
- size_t i;
- unsigned int j;
-
- for (i = 0; i < len; ++i)
- {
- j = (bin[i] >> 4) & 0x0f;
- hex[i * 2] = (char) ((j <= 9) ? (j + '0') : (j - 10 + 'a'));
- j = bin[i] & 0x0f;
- hex[i * 2 + 1] = (char) ((j <= 9) ? (j + '0') : (j - 10 + 'a'));
- }
- hex[len * 2] = '\0';
-}
-
-
-/**
* calculate H(A1) from given hash as per RFC2617 spec
* and store the * result in 'sessionkey'.
*
@@ -258,15 +232,15 @@ digest_calc_ha1_from_digest (const char *alg,
strlen (cnonce));
da->digest (da->ctx,
dig);
- cvthex (dig,
- digest_size,
- da->sessionkey);
+ MHD_bin_to_hex (dig,
+ digest_size,
+ da->sessionkey);
}
else
{
- cvthex (digest,
- digest_size,
- da->sessionkey);
+ MHD_bin_to_hex (digest,
+ digest_size,
+ da->sessionkey);
}
}
@@ -383,9 +357,9 @@ digest_calc_response (const char *ha1,
#endif
da->digest (da->ctx,
ha2);
- cvthex (ha2,
- digest_size,
- da->sessionkey);
+ MHD_bin_to_hex (ha2,
+ digest_size,
+ da->sessionkey);
da->init (da->ctx);
/* calculate response */
da->update (da->ctx,
@@ -426,9 +400,9 @@ digest_calc_response (const char *ha1,
digest_size * 2);
da->digest (da->ctx,
resphash);
- cvthex (resphash,
- digest_size,
- da->sessionkey);
+ MHD_bin_to_hex (resphash,
+ digest_size,
+ da->sessionkey);
}
@@ -736,12 +710,12 @@ calculate_nonce (uint32_t nonce_time,
strlen (realm));
da->digest (da->ctx,
tmpnonce);
- cvthex (tmpnonce,
- digest_size,
- nonce);
- cvthex (timestamp,
- sizeof (timestamp),
- nonce + digest_size * 2);
+ MHD_bin_to_hex (tmpnonce,
+ digest_size,
+ nonce);
+ MHD_bin_to_hex (timestamp,
+ sizeof (timestamp),
+ nonce + digest_size * 2);
}
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
@@ -1354,3 +1354,24 @@ MHD_uint8_to_str_pad (uint8_t val,
buf[pos++] = '0' + val;
return pos;
}
+
+
+size_t
+MHD_bin_to_hex (const void *bin,
+ size_t size,
+ char *hex)
+{
+ size_t i;
+
+ for (i = 0; i < size; ++i)
+ {
+ uint8_t j;
+ const uint8_t b = ((uint8_t *) bin)[i];
+ j = b >> 4;
+ hex[i * 2] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a'));
+ j = b & 0x0f;
+ hex[i * 2 + 1] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a'));
+ }
+ hex[i * 2] = 0;
+ return i;
+}
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h
@@ -457,5 +457,18 @@ MHD_uint8_to_str_pad (uint8_t val,
char *buf,
size_t buf_size);
+/**
+ * Convert @a size bytes from input binary data to lower case
+ * hexadecimal digits, zero-terminate the result.
+ * @param bin the pointer to the binary data to convert
+ * @param size the size in bytes of the binary data to convert
+ * @param hex the output buffer, but be at least 2 * @a size + 1
+ * @return The number of characters written to the output buffer,
+ * not including terminating zero.
+ */
+size_t
+MHD_bin_to_hex (const void *bin,
+ size_t size,
+ char *hex);
#endif /* MHD_STR_H */