From d5221a3d40ed2e16ed687075cf8a309d0ec59601 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 1 Aug 2005 14:48:22 +0000 Subject: [PATCH] Finalize a SoupMD5Context and write out the digest in hex digits. * libsoup/soup-md5-utils.c (soup_md5_final_hex): Finalize a SoupMD5Context and write out the digest in hex digits. * libsoup/soup-auth-digest.c (authenticate, compute_response): * libsoup/soup-server-auth.c (check_digest_passwd): Use that, rather than duplicating the code in both places here. Patch from Wim Lewis. --- ChangeLog | 11 +++++++++++ libsoup/soup-auth-digest.c | 21 +++------------------ libsoup/soup-md5-utils.c | 38 ++++++++++++++++++++++++++++++-------- libsoup/soup-md5-utils.h | 21 ++++++++------------- libsoup/soup-server-auth.c | 20 +++----------------- 5 files changed, 55 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7b97462..6237c37 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2005-08-01 Dan Winship + + * libsoup/soup-md5-utils.c (soup_md5_final_hex): Finalize a + SoupMD5Context and write out the digest in hex digits. + + * libsoup/soup-auth-digest.c (authenticate, compute_response): + * libsoup/soup-server-auth.c (check_digest_passwd): Use that, + rather than duplicating the code in both places here. + + Patch from Wim Lewis. + 2005-07-15 Dan Winship * libsoup/soup-session.c (redirect_handler): Allow relative URIs, diff --git a/libsoup/soup-auth-digest.c b/libsoup/soup-auth-digest.c index 0397a28..593ce27 100644 --- a/libsoup/soup-auth-digest.c +++ b/libsoup/soup-auth-digest.c @@ -243,16 +243,6 @@ get_realm (SoupAuth *auth) } static void -digest_hex (guchar *digest, guchar hex[33]) -{ - guchar *s, *p; - - /* lowercase hexify that bad-boy... */ - for (s = digest, p = hex; p < hex + 32; s++, p += 2) - sprintf (p, "%.2x", *s); -} - -static void authenticate (SoupAuth *auth, const char *username, const char *password) { SoupAuthDigestPrivate *priv = SOUP_AUTH_DIGEST_GET_PRIVATE (auth); @@ -300,8 +290,7 @@ authenticate (SoupAuth *auth, const char *username, const char *password) } /* hexify A1 */ - soup_md5_final (&ctx, d); - digest_hex (d, priv->hex_a1); + soup_md5_final_hex (&ctx, priv->hex_a1); } static gboolean @@ -314,7 +303,6 @@ static char * compute_response (SoupAuthDigestPrivate *priv, SoupMessage *msg) { guchar hex_a2[33], o[33]; - guchar d[16]; SoupMD5Context md5; char *url; const SoupUri *uri; @@ -338,8 +326,7 @@ compute_response (SoupAuthDigestPrivate *priv, SoupMessage *msg) } /* now hexify A2 */ - soup_md5_final (&md5, d); - digest_hex (d, hex_a2); + soup_md5_final_hex (&md5, hex_a2); /* compute KD */ soup_md5_init (&md5); @@ -373,9 +360,7 @@ compute_response (SoupAuthDigestPrivate *priv, SoupMessage *msg) } soup_md5_update (&md5, hex_a2, 32); - soup_md5_final (&md5, d); - - digest_hex (d, o); + soup_md5_final_hex (&md5, o); return g_strdup (o); } diff --git a/libsoup/soup-md5-utils.c b/libsoup/soup-md5-utils.c index b4fc115..d9ca7a2 100644 --- a/libsoup/soup-md5-utils.c +++ b/libsoup/soup-md5-utils.c @@ -16,13 +16,6 @@ * will fill a supplied 16-byte array with the digest. */ -/* parts of this file are : - * Written March 1993 by Branko Lankester - * Modified June 1993 by Colin Plumb for altered md5.c. - * Modified October 1995 by Erik Troan for RPM - */ - - #include "soup-md5-utils.h" #include @@ -131,7 +124,8 @@ soup_md5_update (SoupMD5Context *ctx, const guchar *buf, guint32 len) * @digest: 16 bytes buffer * @ctx: context containing the calculated md5 * - * copy the final md5 hash to a bufer + * Performs the final md5 transformation on the context, and + * then copies the resulting md5 hash to a buffer **/ void soup_md5_final (SoupMD5Context *ctx, guchar digest[16]) @@ -179,6 +173,33 @@ soup_md5_final (SoupMD5Context *ctx, guchar digest[16]) +/** + * soup_md5_final_hex: copy the final md5 hash to a bufer + * @digest: 33 bytes buffer (32 hex digits plus NUL) + * @ctx: context containing the calculated md5 + * + * As soup_md5_final(), but copies the final md5 hash + * to a buffer as a NUL-terminated hexadecimal string + **/ +void +soup_md5_final_hex (SoupMD5Context *ctx, guchar hex_digest[33]) +{ + static const guchar hexdigits[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + int p; + + soup_md5_final (ctx, hex_digest); + + hex_digest[32] = 0; + for (p = 15; p >= 0; p--) { + guchar b = hex_digest[p]; + hex_digest[p * 2 + 1] = hexdigits[ (b & 0x0F ) ]; + hex_digest[p * 2] = hexdigits[ (b & 0xF0 ) >> 4 ]; + } +} + /* The four core functions - F1 is optimized somewhat */ @@ -280,3 +301,4 @@ soup_md5_transform (guint32 buf[4], const guint32 in[16]) buf[2] += c; buf[3] += d; } + diff --git a/libsoup/soup-md5-utils.h b/libsoup/soup-md5-utils.h index 161c0d3..4e1f5ea 100644 --- a/libsoup/soup-md5-utils.h +++ b/libsoup/soup-md5-utils.h @@ -16,13 +16,6 @@ * will fill a supplied 16-byte array with the digest. */ -/* parts of this file are : - * Written March 1993 by Branko Lankester - * Modified June 1993 by Colin Plumb for altered md5.c. - * Modified October 1995 by Erik Troan for RPM - */ - - #ifndef SOUP_MD5_UTILS_H #define SOUP_MD5_UTILS_H @@ -36,12 +29,14 @@ typedef struct { gboolean doByteReverse; } SoupMD5Context; -void soup_md5_init (SoupMD5Context *ctx); -void soup_md5_update (SoupMD5Context *ctx, - const guchar *buf, - guint32 len); -void soup_md5_final (SoupMD5Context *ctx, - guchar digest[16]); +void soup_md5_init (SoupMD5Context *ctx); +void soup_md5_update (SoupMD5Context *ctx, + const guchar *buf, + guint32 len); +void soup_md5_final (SoupMD5Context *ctx, + guchar digest[16]); +void soup_md5_final_hex (SoupMD5Context *ctx, + guchar digest[33]); #endif /* SOUP_MD5_UTILS_H */ diff --git a/libsoup/soup-server-auth.c b/libsoup/soup-server-auth.c index 3225f60..5134886 100644 --- a/libsoup/soup-server-auth.c +++ b/libsoup/soup-server-auth.c @@ -70,16 +70,6 @@ soup_auth_get_strongest_header (guint auth_types, return scheme->type; } -static void -digest_hex (guchar *digest, guchar hex[33]) -{ - guchar *s, *p; - - /* lowercase hexify that bad-boy... */ - for (s = digest, p = hex; p < hex + 32; s++, p += 2) - sprintf (p, "%.2x", *s); -} - static gboolean check_digest_passwd (SoupServerAuthDigest *digest, gchar *passwd) @@ -111,8 +101,7 @@ check_digest_passwd (SoupServerAuthDigest *digest, } /* hexify A1 */ - soup_md5_final (&ctx, d); - digest_hex (d, hex_a1); + soup_md5_final_hex (&ctx, hex_a1); /* compute A2 */ soup_md5_init (&ctx); @@ -129,8 +118,7 @@ check_digest_passwd (SoupServerAuthDigest *digest, } /* hexify A2 */ - soup_md5_final (&ctx, d); - digest_hex (d, hex_a2); + soup_md5_final_hex (&ctx, hex_a2); /* compute KD */ soup_md5_init (&ctx); @@ -156,9 +144,7 @@ check_digest_passwd (SoupServerAuthDigest *digest, soup_md5_update (&ctx, ":", 1); soup_md5_update (&ctx, hex_a2, 32); - soup_md5_final (&ctx, d); - - digest_hex (d, o); + soup_md5_final_hex (&ctx, o); return strcmp (o, digest->digest_response) == 0; } -- 2.7.4