mad_md5.c

Go to the documentation of this file.
00001 
00033 #include <stdio.h>
00034 #include <memory.h>
00035 #include <string.h>
00036 #include <sys/stat.h>
00037 #include <errno.h>
00038 
00039 #include "mad_md5.h"
00040 
00041 #ifdef USE_OPENSSL
00042 
00043 #include <openssl/md5.h>
00044 #include <openssl/bio.h>
00045 #include <openssl/evp.h>
00046 
00047 char* file_md5(const char *filename) {
00048 
00049 #ifdef _MSC_VER
00050         struct __stat64 file_stats;
00051 #else
00052     struct stat64 file_stats;
00053 #endif
00054 
00055         int nbytes;
00056         FILE *fp;
00057         char *md5 = NULL;
00058 
00059         char b64_digest[MD5_DIGEST_LENGTH*2];
00060         BIO *bio, *b64, *mem;
00061 
00062         unsigned char md5_digest[MD5_DIGEST_LENGTH];
00063         MD5_CTX ctx;                            
00064         int i;
00065         char zBuf[10240]; /*10240*/
00066 
00067         memset(md5_digest, 0, MD5_DIGEST_LENGTH);
00068         memset(b64_digest, 0, MD5_DIGEST_LENGTH*2);
00069 
00070         MD5_Init(&ctx);
00071 
00072 #ifdef _MSC_VER
00073         fp = fopen(filename, "rb");
00074 #else
00075         fp = fopen64(filename, "rb");
00076 #endif
00077 
00078         if(fp == NULL) {
00079           printf("mad_md5.c: fopen error\n");
00080           fflush(stdout);
00081           return NULL;
00082         }
00083 
00084 #ifdef _MSC_VER
00085         if(_stat64(filename, &file_stats) == -1) {
00086 #else
00087     if(stat64(filename, &file_stats) == -1) {
00088 #endif
00089                 printf("Error: %s is not valid file name\n", filename);
00090                 fflush(stdout);
00091         return NULL;
00092         }
00093 
00094         for(;;) {
00095                 nbytes = fread(zBuf, 1, sizeof(zBuf), fp);
00096         
00097                 if(nbytes <= 0) {
00098                         break;
00099                 }
00100                 
00101                 MD5_Update(&ctx, (unsigned char*)zBuf, (unsigned)nbytes);
00102     }
00103 
00104         MD5_Final(md5_digest, &ctx); 
00105 
00106         b64 = BIO_new(BIO_f_base64());
00107         mem = BIO_new(BIO_s_mem());
00108         bio = BIO_push(b64, mem);
00109         BIO_write(bio, md5_digest, MD5_DIGEST_LENGTH);
00110         BIO_flush(bio);
00111         BIO_gets(mem, b64_digest, MD5_DIGEST_LENGTH*2);
00112         BIO_free_all(bio);
00113 
00114         for(i = 0; i < MD5_DIGEST_LENGTH*2; i++) {
00115                 if(b64_digest[i] <= ' ') {
00116                         b64_digest[i] = '\0';
00117         }
00118         }
00119 
00120         if(!(md5 = (char*)calloc((strlen(b64_digest) + 1), sizeof(char)))) {
00121                 printf("Could not alloc memory for md5 buffer!\n");
00122                 fflush(stdout);
00123         return NULL;
00124         } 
00125 
00126         memcpy(md5, b64_digest, strlen(b64_digest));
00127         
00128         if(fclose(fp) != 0) {
00129                 printf("fclose failed, errno: %i\n", errno);
00130         }
00131 
00132         return md5;
00133 }
00134 
00135 char* buffer_md5(char *buffer, unsigned long long length) {
00136         
00137         char b64_digest[MD5_DIGEST_LENGTH*2];
00138         BIO *bio, *b64, *mem;
00139 
00140         unsigned char md5_digest[MD5_DIGEST_LENGTH];
00141         MD5_CTX ctx;                            
00142         int i;
00143         char *md5 = NULL;
00144 
00145         memset(md5_digest, 0, MD5_DIGEST_LENGTH);
00146         memset(b64_digest, 0, MD5_DIGEST_LENGTH*2);
00147 
00148         MD5_Init(&ctx);
00149 
00150         MD5_Update(&ctx, buffer, (unsigned int)length);
00151         MD5_Final(md5_digest, &ctx); 
00152 
00153         b64 = BIO_new(BIO_f_base64());
00154         mem = BIO_new(BIO_s_mem());
00155         bio = BIO_push(b64, mem);
00156         BIO_write(bio, md5_digest, MD5_DIGEST_LENGTH);
00157         BIO_flush(bio);
00158         BIO_gets(mem, b64_digest, MD5_DIGEST_LENGTH*2);
00159         BIO_free_all(bio);
00160 
00161         for(i = 0; i < MD5_DIGEST_LENGTH*2; i++) {
00162                 if(b64_digest[i] <= ' ') {
00163                         b64_digest[i] = '\0';
00164         }
00165         }
00166 
00167         if(!(md5 = (char*)calloc((strlen(b64_digest) + 1), sizeof(char)))) {
00168                 printf("Could not alloc memory for md5 buffer!\n");
00169                 fflush(stdout);
00170         return NULL;
00171         } 
00172 
00173         memcpy(md5, b64_digest, strlen(b64_digest));
00174 
00175         return md5;
00176 }
00177 
00178 #endif
00179 

Generated on Fri Mar 9 19:59:53 2007 for MAD-FCL by  doxygen 1.5.0