1 /* @(#) $Id: ./src/os_crypto/md5_sha1/md5_sha1_op.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
16 #include "md5_sha1_op.h"
18 #include "../md5/md5.h"
19 #include "../sha1/sha.h"
20 #include "headers/defs.h"
24 int OS_MD5_SHA1_File(char *fname, char *prefilter_cmd, char *md5output, char *sha1output)
28 unsigned char buf[2048 +2];
29 unsigned char sha1_digest[SHA_DIGEST_LENGTH];
30 unsigned char md5_digest[16];
38 /* Clearing the memory. */
43 /* Use prefilter_cmd if set */
44 if (prefilter_cmd == NULL) {
45 fp = fopen(fname,"r");
49 strncpy(cmd, prefilter_cmd, sizeof(cmd) - 1);
51 strncat(cmd, fname, sizeof(cmd) - strlen(cmd) - 1);
57 /* Initializing both hashes */
62 /* Updating for each one. */
63 while((n = fread(buf, 1, 2048, fp)) > 0)
66 SHA1_Update(&sha1_ctx, buf, (unsigned long)n);
67 MD5Update(&md5_ctx, buf, n);
70 SHA1_Final(&(sha1_digest[0]), &sha1_ctx);
71 MD5Final(md5_digest, &md5_ctx);
74 /* Setting output for md5. */
75 for(n = 0;n < 16; n++)
77 snprintf(md5output, 3, "%02x", md5_digest[n]);
81 /* Setting output for sha1. */
82 for (n = 0; n<SHA_DIGEST_LENGTH; n++)
84 snprintf(sha1output, 3, "%02x", sha1_digest[n]);
90 if (prefilter_cmd == NULL) {