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
10 /* OS_crypto/md5 Library
11 * APIs for many crypto operations
19 #include "headers/defs.h"
22 int OS_MD5_File(const char *fname, os_md5 output, int mode)
26 unsigned char buf[1024 + 1];
27 unsigned char digest[16];
30 memset(output, 0, 33);
33 fp = fopen(fname, mode == OS_BINARY ? "rb" : "r");
39 while ((n = fread(buf, 1, sizeof(buf) - 1, fp)) > 0) {
41 MD5Update(&ctx, buf, (unsigned)n);
44 MD5Final(digest, &ctx);
46 for (n = 0; n < 16; n++) {
47 snprintf(output, 3, "%02x", digest[n]);
56 int OS_MD5_Str(const char *str, os_md5 output)
58 unsigned char digest[16];
64 MD5Update(&ctx, (const unsigned char *)str, (unsigned)strlen(str));
65 MD5Final(digest, &ctx);
68 for (n = 0; n < 16; n++) {
69 snprintf(output, 3, "%02x", digest[n]);