6 #include <libubox/md5.h>
7 #include <libubox/utils.h>
9 #include "uclient-utils.h"
11 static const char *b64 =
12 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
14 void base64_encode(const void *inbuf, unsigned int len, void *outbuf)
16 unsigned char *out = outbuf;
17 const uint8_t *in = inbuf;
21 for (i = 0; i < len - pad; i += 3) {
22 uint32_t in3 = (in[0] << 16) | (in[1] << 8) | in[2];
25 for (k = 3; k >= 0; k--) {
26 out[k] = b64[in3 & 0x3f];
34 uint32_t in2 = in[0] << (16 - 6);
39 in2 |= in[1] << (8 - 6);
40 out[2] = b64[in2 & 0x3f];
46 out[1] = b64[in2 & 0x3f];
48 out[0] = b64[in2 & 0x3f];
56 int uclient_urldecode(const char *in, char *out, bool decode_plus)
62 while ((c = *(in++))) {
64 if (!isxdigit(in[0]) || !isxdigit(in[1]))
69 c = strtol(dec, NULL, 16);
71 } else if (decode_plus && c == '+') {
83 static char hex_digit(char val)
85 val += val > 9 ? 'a' - 10 : '0';
89 void bin_to_hex(char *dest, const void *buf, int len)
91 const uint8_t *data = buf;
94 for (i = 0; i < len; i++) {
95 *(dest++) = hex_digit(data[i] >> 4);
96 *(dest++) = hex_digit(data[i] & 0xf);
101 static void http_create_hash(char *dest, const char * const * str, int n_str)
108 for (i = 0; i < n_str; i++) {
110 md5_hash(":", 1, &md5);
111 md5_hash(str[i], strlen(str[i]), &md5);
114 bin_to_hex(dest, &hash, sizeof(hash));
117 void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password)
119 const char *hash_str[] = {
125 http_create_hash(dest, hash_str, ARRAY_SIZE(hash_str));
128 void http_digest_calculate_response(char *dest, const struct http_digest_data *data)
130 const char *h_a2_strings[] = {
134 const char *resp_strings[] = {
140 dest, /* initialized to H(A2) first */
143 http_create_hash(dest, h_a2_strings, ARRAY_SIZE(h_a2_strings));
144 http_create_hash(dest, resp_strings, ARRAY_SIZE(resp_strings));