md5.c
changeset 45 7197576fedcf
child 77 49e0babccb23
equal deleted inserted replaced
44:bb6586b1c547 45:7197576fedcf
       
     1 /**
       
     2  * test/demos/md5.c
       
     3  * Copyright (C) 2008 Markus Broeker
       
     4  */
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 #include <string.h>
       
     9 #include <openssl/md5.h>
       
    10 
       
    11 typedef unsigned char u_char;
       
    12 
       
    13 int main (int argc, char **argv)
       
    14 {
       
    15     char buffer[80];
       
    16     char text[80];
       
    17 
       
    18     unsigned char *md5_hash;
       
    19     char result[33];
       
    20     char byte[3];
       
    21 
       
    22     int i, j;
       
    23 
       
    24     if (argc != 2) {
       
    25         printf ("Usage: %s <text>\n", argv[0]);
       
    26         return EXIT_FAILURE;
       
    27     }
       
    28 
       
    29     /*
       
    30      * MD5 alters the input buffer
       
    31      */
       
    32     strcpy (buffer, argv[1]);
       
    33     strcpy (text, argv[1]);
       
    34 
       
    35     md5_hash = MD5 ((u_char *) buffer, strlen (buffer), NULL);
       
    36 
       
    37     for (i = 0, j = 0; i < 16; i++) {
       
    38         sprintf (byte, "%02x", (md5_hash[i] & 0xFF));
       
    39         result[j++] = byte[0];
       
    40         result[j++] = byte[1];
       
    41     }
       
    42 
       
    43     result[j] = 0;
       
    44     printf ("%s: %s\n", text, result);
       
    45 
       
    46     return EXIT_SUCCESS;
       
    47 }