Imported Upstream version 2.7
[ossec-hids.git] / src / os_crypto / md5 / md5_op.c
1 /*      $OSSEC, os_crypto/md5_op.c, v0.2, 2005/09/17, Daniel B. Cid$      */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12 /* v0.2 (2005/09/17): char fixes (signal)
13  * v0.1 (2004/08/09)
14  */
15
16 /* OS_crypto/md5 Library.
17  * APIs for many crypto operations.
18  */
19
20
21 #include <stdio.h>
22 #include <string.h>
23 #include "md5.h"
24
25 int OS_MD5_File(char * fname, char * output)
26 {
27     FILE *fp;
28     MD5_CTX ctx;
29     unsigned char buf[1024 +1];
30     unsigned char digest[16];
31     int n;
32
33     memset(output,0, 33);
34     buf[1024] = '\0';
35
36     fp = fopen(fname,"r");
37     if(!fp)
38     {
39         return(-1);
40     }
41
42     MD5Init(&ctx);
43     while((n = fread(buf, 1, sizeof(buf) -1, fp)) > 0)
44     {
45         buf[n] = '\0';
46         MD5Update(&ctx,buf,n);
47     }
48
49     MD5Final(digest, &ctx);
50
51     for(n = 0;n < 16; n++)
52     {
53         snprintf(output, 3, "%02x", digest[n]);
54         output+=2;
55     }
56
57     /* Closing it */
58     fclose(fp);
59
60     return(0);
61 }
62
63 /* EOF */
64 int OS_MD5_Str(char * str, char * output)
65 {
66     unsigned char digest[16];
67
68     int n;
69
70     MD5_CTX ctx;
71
72     MD5Init(&ctx);
73
74     MD5Update(&ctx,(unsigned char *)str,strlen(str));
75
76     MD5Final(digest, &ctx);
77
78     output[32] = '\0';
79     for(n = 0;n < 16;n++)
80     {
81         snprintf(output, 3, "%02x", digest[n]);
82         output+=2;
83     }
84
85     return(0);
86 }
87
88 /* EOF */