[vlc-devel] [PATCH v2 05/15] src: http_auth: use new md5 API

Marvin Scholz epirat07 at gmail.com
Wed Apr 8 00:04:21 CEST 2020


---
 src/network/http_auth.c | 98 +++++++++++++++++++----------------------
 1 file changed, 46 insertions(+), 52 deletions(-)

diff --git a/src/network/http_auth.c b/src/network/http_auth.c
index ff1796c25d..c7ef6c4576 100644
--- a/src/network/http_auth.c
+++ b/src/network/http_auth.c
@@ -32,7 +32,7 @@
 
 #include <vlc_common.h>
 #include <vlc_http.h>
-#include <vlc_md5.h>
+#include <vlc_hash.h>
 #include <vlc_rand.h>
 #include <vlc_strings.h>
 
@@ -87,15 +87,14 @@ static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param
 static char *GenerateCnonce()
 {
     char ps_random[32];
-    struct md5_s md5;
+    vlc_hash_md5_t md5;
 
     vlc_rand_bytes( ps_random, sizeof( ps_random ) );
 
-    InitMD5( &md5 );
-    AddMD5( &md5, ps_random, sizeof( ps_random ) );
-    EndMD5( &md5 );
+    vlc_hash_md5_Init( &md5 );
+    vlc_hash_md5_Update( &md5, ps_random, sizeof( ps_random ) );
 
-    return psz_md5_hash( &md5 );
+    return vlc_hash_md5_FinishHex( &md5 );
 }
 
 static char *AuthDigest( vlc_object_t *p_this, vlc_http_auth_t *p_auth,
@@ -107,8 +106,8 @@ static char *AuthDigest( vlc_object_t *p_this, vlc_http_auth_t *p_auth,
     char *psz_ent = NULL;
     char *psz_result = NULL;
     char psz_inonce[9];
-    struct md5_s md5;
-    struct md5_s ent;
+    vlc_hash_md5_t md5;
+    vlc_hash_md5_t ent;
 
     if ( p_auth->psz_realm == NULL )
     {
@@ -126,32 +125,30 @@ static char *AuthDigest( vlc_object_t *p_this, vlc_http_auth_t *p_auth,
     }
     else
     {
-        InitMD5( &md5 );
-        AddMD5( &md5, psz_username, strlen( psz_username ) );
-        AddMD5( &md5, ":", 1 );
-        AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
-        AddMD5( &md5, ":", 1 );
-        AddMD5( &md5, psz_password, strlen( psz_password ) );
-        EndMD5( &md5 );
-
-        psz_HA1 = psz_md5_hash( &md5 );
+        vlc_hash_md5_Init( &md5 );
+        vlc_hash_md5_Update( &md5, psz_username, strlen( psz_username ) );
+        vlc_hash_md5_Update( &md5, ":", 1 );
+        vlc_hash_md5_Update( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
+        vlc_hash_md5_Update( &md5, ":", 1 );
+        vlc_hash_md5_Update( &md5, psz_password, strlen( psz_password ) );
+
+        psz_HA1 = vlc_hash_md5_FinishHex( &md5 );
         if ( psz_HA1 == NULL )
             goto error;
 
         if ( p_auth->psz_algorithm &&
              strcmp( p_auth->psz_algorithm, "MD5-sess" ) == 0 )
         {
-            InitMD5( &md5 );
-            AddMD5( &md5, psz_HA1, 32 );
-            AddMD5( &md5, ":", 1 );
-            AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
-            AddMD5( &md5, ":", 1 );
-            AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
-            EndMD5( &md5 );
+            vlc_hash_md5_Init( &md5 );
+            vlc_hash_md5_Update( &md5, psz_HA1, 32 );
+            vlc_hash_md5_Update( &md5, ":", 1 );
+            vlc_hash_md5_Update( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
+            vlc_hash_md5_Update( &md5, ":", 1 );
+            vlc_hash_md5_Update( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
 
             free( psz_HA1 );
 
-            psz_HA1 = psz_md5_hash( &md5 );
+            psz_HA1 = vlc_hash_md5_FinishHex( &md5 );
             if ( psz_HA1 == NULL )
                 goto error;
 
@@ -162,56 +159,53 @@ static char *AuthDigest( vlc_object_t *p_this, vlc_http_auth_t *p_auth,
     }
 
     /* H(A2) */
-    InitMD5( &md5 );
+    vlc_hash_md5_Init( &md5 );
     if ( *psz_method )
-        AddMD5( &md5, psz_method, strlen( psz_method ) );
-    AddMD5( &md5, ":", 1 );
+        vlc_hash_md5_Update( &md5, psz_method, strlen( psz_method ) );
+    vlc_hash_md5_Update( &md5, ":", 1 );
     if ( psz_path )
-        AddMD5( &md5, psz_path, strlen( psz_path ) );
+        vlc_hash_md5_Update( &md5, psz_path, strlen( psz_path ) );
     else
-        AddMD5( &md5, "/", 1 );
+        vlc_hash_md5_Update( &md5, "/", 1 );
     if ( p_auth->psz_qop && strcmp( p_auth->psz_qop, "auth-int" ) == 0 )
     {
-        InitMD5( &ent );
+        vlc_hash_md5_Init( &ent );
         /* TODO: Support for "qop=auth-int" */
-        AddMD5( &ent, "", 0 );
-        EndMD5( &ent );
+        vlc_hash_md5_Update( &ent, "", 0 );
 
-        psz_ent = psz_md5_hash( &ent );
+        psz_ent = vlc_hash_md5_FinishHex( &ent );
         if ( psz_ent == NULL )
             goto error;
 
-        AddMD5( &md5, ":", 1 );
-        AddMD5( &md5, psz_ent, 32 );
+        vlc_hash_md5_Update( &md5, ":", 1 );
+        vlc_hash_md5_Update( &md5, psz_ent, 32 );
     }
-    EndMD5( &md5 );
 
-    psz_HA2 = psz_md5_hash( &md5 );
+    psz_HA2 = vlc_hash_md5_FinishHex( &md5 );
     if ( psz_HA2 == NULL )
         goto error;
 
     /* Request digest */
-    InitMD5( &md5 );
-    AddMD5( &md5, psz_HA1, 32 );
-    AddMD5( &md5, ":", 1 );
-    AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
-    AddMD5( &md5, ":", 1 );
+    vlc_hash_md5_Init( &md5 );
+    vlc_hash_md5_Update( &md5, psz_HA1, 32 );
+    vlc_hash_md5_Update( &md5, ":", 1 );
+    vlc_hash_md5_Update( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
+    vlc_hash_md5_Update( &md5, ":", 1 );
     if ( p_auth->psz_qop &&
          ( strcmp( p_auth->psz_qop, "auth" ) == 0 ||
            strcmp( p_auth->psz_qop, "auth-int" ) == 0 ) )
     {
         snprintf( psz_inonce, sizeof( psz_inonce ), "%08x", p_auth->i_nonce );
-        AddMD5( &md5, psz_inonce, 8 );
-        AddMD5( &md5, ":", 1 );
-        AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
-        AddMD5( &md5, ":", 1 );
-        AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
-        AddMD5( &md5, ":", 1 );
+        vlc_hash_md5_Update( &md5, psz_inonce, 8 );
+        vlc_hash_md5_Update( &md5, ":", 1 );
+        vlc_hash_md5_Update( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
+        vlc_hash_md5_Update( &md5, ":", 1 );
+        vlc_hash_md5_Update( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
+        vlc_hash_md5_Update( &md5, ":", 1 );
     }
-    AddMD5( &md5, psz_HA2, 32 );
-    EndMD5( &md5 );
+    vlc_hash_md5_Update( &md5, psz_HA2, 32 );
 
-    psz_result = psz_md5_hash( &md5 );
+    psz_result = vlc_hash_md5_FinishHex( &md5 );
 
 error:
     free( psz_HA1 );
-- 
2.24.1 (Apple Git-126)



More information about the vlc-devel mailing list