1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
13 #include "md5_sha1_op.h"
14 #include "../md5/md5.h"
15 #include "../sha1/sha.h"
16 #include "headers/defs.h"
19 int OS_MD5_SHA1_File(const char *fname, const char *prefilter_cmd, os_md5 md5output, os_sha1 sha1output, int mode)
23 unsigned char buf[2048 + 2];
24 unsigned char sha1_digest[SHA_DIGEST_LENGTH];
25 unsigned char md5_digest[16];
30 /* Clear the memory */
35 /* Use prefilter_cmd if set */
36 if (prefilter_cmd == NULL) {
37 fp = fopen(fname, mode == OS_BINARY ? "rb" : "r");
43 size_t target_length = strlen(prefilter_cmd) + 1 + strlen(fname);
44 int res = snprintf(cmd, sizeof(cmd), "%s %s", prefilter_cmd, fname);
45 if (res < 0 || (unsigned int)res != target_length) {
54 /* Initialize both hashes */
58 /* Update for each one */
59 while ((n = fread(buf, 1, 2048, fp)) > 0) {
61 SHA1_Update(&sha1_ctx, buf, n);
62 MD5Update(&md5_ctx, buf, (unsigned)n);
65 SHA1_Final(&(sha1_digest[0]), &sha1_ctx);
66 MD5Final(md5_digest, &md5_ctx);
68 /* Set output for MD5 */
69 for (n = 0; n < 16; n++) {
70 snprintf(md5output, 3, "%02x", md5_digest[n]);
74 /* Set output for SHA-1 */
75 for (n = 0; n < SHA_DIGEST_LENGTH; n++) {
76 snprintf(sha1output, 3, "%02x", sha1_digest[n]);
81 if (prefilter_cmd == NULL) {