lsflib/src/md5sum.c
changeset 6 c3dc3eb3b541
child 9 c3fecc82ade6
equal deleted inserted replaced
5:d752cbe8208e 6:c3dc3eb3b541
       
     1 /*
       
     2  *  $Id: md5sum.c 94 2008-04-05 01:27:30Z mbroeker $
       
     3  * $URL: http://localhost/svn/c/lsflib/trunk/src/md5sum.c $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <string.h>
       
     9 #include <openssl/evp.h>
       
    10 
       
    11 #include <lsf.h>
       
    12 
       
    13 unsigned char *md5sum (char *fname)
       
    14 {
       
    15     EVP_MD_CTX mdctx;
       
    16     const EVP_MD *md;
       
    17     char line[81];
       
    18     unsigned char md_value[EVP_MAX_MD_SIZE];
       
    19     int md_len, i;
       
    20     FILE *f;
       
    21     static unsigned char *value = NULL;
       
    22 
       
    23     OpenSSL_add_all_digests ();
       
    24 
       
    25     if ((f = fopen (fname, "rb")) == NULL) {
       
    26         perror ("FOPEN");
       
    27         value = malloc (1);
       
    28         value = 0;
       
    29         return value;
       
    30     }
       
    31 
       
    32     if ((md = EVP_get_digestbyname ("md5")) == NULL) {
       
    33         value = malloc (1);
       
    34         value = 0;
       
    35         return value;
       
    36     }
       
    37 
       
    38     EVP_MD_CTX_init (&mdctx);
       
    39     EVP_DigestInit_ex (&mdctx, md, NULL);
       
    40 
       
    41     *line = 0;
       
    42     while ((i = fread (line, 1, 80, f)) != 0) {
       
    43         EVP_DigestUpdate (&mdctx, line, i);
       
    44     }
       
    45 
       
    46     fclose (f);
       
    47 
       
    48     EVP_DigestFinal_ex (&mdctx, md_value, (unsigned int *)&md_len);
       
    49     EVP_MD_CTX_cleanup (&mdctx);
       
    50 
       
    51     value = malloc (md_len + 1);
       
    52 
       
    53     for (i = 0; i < md_len; i++)
       
    54         value[i] = md_value[i];
       
    55     value[i] = 0;
       
    56     return value;
       
    57 }