159c3cf5f9ebfc4febf7df8c078c36d541a019af
[ossec-hids.git] / src / os_crypto / sha1 / sha1_op.c
1 /* @(#) $Id$ */
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
13 #include <stdio.h>
14 #include <string.h>
15 #include "sha1_op.h"
16
17 /* Openssl sha1 
18  * Only use if open ssl is not available.
19 #ifndef USE_OPENSSL
20 #include "sha.h"
21 #include "sha_locl.h"
22 #else
23 #include <openssl/sha.h>  
24 #endif
25 */
26
27 #include "sha_locl.h"
28
29
30  
31 int OS_SHA1_File(char * fname, char * output)
32 {
33     SHA_CTX c;
34     FILE *fp;
35     unsigned char buf[2048 +2];
36     unsigned char md[SHA_DIGEST_LENGTH];
37     int n;
38     
39     memset(output,0, 65);
40     buf[2049] = '\0';
41     
42     fp = fopen(fname,"r");
43     if(!fp)
44         return(-1);
45     
46     SHA1_Init(&c);
47     while((n = fread(buf, 1, 2048, fp)) > 0)
48     {
49         buf[n] = '\0';
50         SHA1_Update(&c,buf,(unsigned long)n);
51     }
52     
53     SHA1_Final(&(md[0]),&c);
54     
55     for (n=0; n<SHA_DIGEST_LENGTH; n++)
56     {
57         snprintf(output, 3, "%02x", md[n]);
58         output+=2;
59     }
60                 
61     /* Closing it */
62     fclose(fp);
63         
64     return(0);
65 }
66
67
68 /* EOF */