Imported Upstream version 2.7
[ossec-hids.git] / src / os_crypto / sha1 / sha1_op.c
1 /* @(#) $Id: ./src/os_crypto/sha1/sha1_op.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
7  * This program is a free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General Public
9  * License (version 2) as published by the FSF - Free Software
10  * Foundation
11  */
12
13
14 #include <stdio.h>
15 #include <string.h>
16 #include "sha1_op.h"
17
18 /* Openssl sha1
19  * Only use if open ssl is not available.
20 #ifndef USE_OPENSSL
21 #include "sha.h"
22 #include "sha_locl.h"
23 #else
24 #include <openssl/sha.h>
25 #endif
26 */
27
28 #include "sha_locl.h"
29
30
31
32 int OS_SHA1_File(char * fname, char * output)
33 {
34     SHA_CTX c;
35     FILE *fp;
36     unsigned char buf[2048 +2];
37     unsigned char md[SHA_DIGEST_LENGTH];
38     int n;
39
40     memset(output,0, 65);
41     buf[2049] = '\0';
42
43     fp = fopen(fname,"r");
44     if(!fp)
45         return(-1);
46
47     SHA1_Init(&c);
48     while((n = fread(buf, 1, 2048, fp)) > 0)
49     {
50         buf[n] = '\0';
51         SHA1_Update(&c,buf,(unsigned long)n);
52     }
53
54     SHA1_Final(&(md[0]),&c);
55
56     for (n=0; n<SHA_DIGEST_LENGTH; n++)
57     {
58         snprintf(output, 3, "%02x", md[n]);
59         output+=2;
60     }
61
62     /* Closing it */
63     fclose(fp);
64
65     return(0);
66 }
67
68
69 /* EOF */