]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Adding procedure cnv_hashmd5
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Wed, 2 Jul 2025 22:21:41 +0000 (18:21 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Wed, 2 Jul 2025 22:21:41 +0000 (18:21 -0400)
lib/subcnv.c
lib/subcnv.h

index a16ffa95a66b1a1beb42cf24c02ff6f4d3a58ad3..d469e013150ca62a11dd22d27a0e74afb42b01dc 100644 (file)
@@ -13,6 +13,7 @@
 #include        <openssl/bio.h>
 #include        <openssl/evp.h>
 #include        <openssl/err.h>
+#include        <openssl/hmac.h>
 #include        <openssl/ssl.h>
 
 #include       "subcnv.h"
@@ -95,11 +96,11 @@ switch(retour=BIO_read(bbio,decoded,strlen(b64))) {
         scan--;
         if (decoded[scan]=='\000') {
           register int taille;
-         char *new_decoded;
+         char *n_deco;
 
           taille=retour+skip+1;
-         if ((new_decoded=(char *)realloc(decoded,taille*sizeof(char)))!=(char *)0) {
-           decoded=new_decoded;
+         if ((n_deco=(char *)realloc(decoded,taille*sizeof(char)))!=(char *)0) {
+           decoded=n_deco;
            (void) memmove(decoded+scan+skip,decoded+scan+1,(retour-scan)+1);
             (void) memmove(decoded+scan,IOBNULL,skip);
            }
@@ -118,4 +119,109 @@ return decoded;
 
 #undef  OPEP
 }
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to convert a plain ASCII sequence to a*/
+/*      B64 ASCII sequence.                             */
+/*                                                      */
+/********************************************************/
+PUBLIC char *cnv_setb64(const char *str)
+
+{
+#define        OPEP    "unicnv.c:tls_setb64,"
 
+register int skip;
+
+char *coded;
+BIO *bmem;
+BIO *b64;
+BUF_MEM *bptr;
+int taille;
+char *buffer;
+char *ptr;
+
+skip=strlen(IOBNULL)-1;
+coded=(char *)0;
+b64=BIO_new(BIO_f_base64());
+BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
+bmem=BIO_new(BIO_s_mem());
+b64=BIO_push(b64,bmem);
+buffer=strdup(str);
+taille=strlen(buffer);
+ptr=buffer;
+while ((ptr=strstr(ptr,IOBNULL))!=(char *)0) {
+  *ptr='\000';
+  ptr++;
+  (void) memmove(ptr,ptr+skip,strlen(ptr+skip)+1);
+  taille-=skip;
+  }
+(void) BIO_write(b64,buffer,taille);
+(void) BIO_flush(b64);
+BIO_get_mem_ptr(b64,&bptr);
+(void) free(buffer);
+coded=(char *)calloc(bptr->length+1,sizeof(char));
+(void) memcpy(coded,bptr->data,bptr->length);
+(void) BIO_free_all(b64);
+return coded;
+
+#undef  OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      procedure to compute a MD5 hashing.             */
+/*                                                      */
+/********************************************************/
+PUBLIC char *cnv_hashmd5(const void *key,unsigned char *seq)
+
+{
+#define OPEP    "subcnv.c:cnv_hashmd5,"
+
+char *hashmd5;          //function result
+unsigned char *d5;      //md5 hash result
+u_int reslen;
+int phase;
+int proceed;
+
+hashmd5=(char *)0;
+d5=(unsigned char *)0;
+reslen=-1;
+phase=1;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //do ve have good parameters
+      if ((key==(const void  *)0)||(seq==(unsigned char *)0)) {
+        (void) fprintf(stderr,"%s key or seq undefined (Bug?)",OPEP);
+        phase=999;
+        }
+      break;
+    case 1      :       //computing sequence
+      d5=HMAC(EVP_md5(),key,strlen(key),seq,strlen((const char *)seq),d5,&reslen);
+      if (d5==(unsigned char *)0) {
+        (void) fprintf(stderr,"%s No MD5 for key=<%s> and seq=<%s> (Bug?)",
+                               OPEP,(char *)key,seq);
+        phase=999;
+        }
+      break;
+    case 2      :       //duplicate md5 result
+      if (reslen>0) {
+        hashmd5=(char *)calloc(reslen+1,sizeof(char));
+        (void) memmove((void *)hashmd5,(void *)d5,reslen);
+        }
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+return hashmd5;
+
+#undef  OPEP
+}
index c200cac50cbb3226221613b0560e19912f68f19e..6d78b542783b94503b15fadd27b43cf40596cea9 100644 (file)
@@ -18,4 +18,8 @@ extern char *cnv_getb64(char *b64);
 //Procedure to convert a plain ASCII sequence
 //to an ASCII B64 sequence
 extern char *cnv_setb64(const char *str);
+
+//Procedure to calculate a MD5 hash function
+extern char *cnv_hashmd5(const void *key,unsigned char *seq);
+
 #endif