File: md5utils.c

package info (click to toggle)
opensips 2.2.2-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 31,060 kB
  • ctags: 37,342
  • sloc: ansic: 334,318; xml: 91,231; perl: 6,659; sh: 5,148; sql: 4,175; makefile: 3,152; yacc: 2,499; python: 1,197; cpp: 611; php: 573
file content (113 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2007 1&1 Internet AG
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of opensips, a free SIP server.
 *
 * opensips is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * opensips is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

/*!
 * \file
 * \brief OpenSIPS MD5 handling functions
 */


#include <stdio.h>
#include <sys/stat.h>

#include "md5global.h"
#include "md5.h"
#include "md5utils.h"
#include "dprint.h"
#include "ut.h"


/*! \brief
  * Calculate a MD5 digests over a string array and stores
  * the result in the destination char array.
  * This function assumes 32 bytes in the destination buffer.
  * \param dest destination
  * \param src string input array
  * \param size elements in the input array
  */
void MD5StringArray(char *dest, str src[], unsigned int size)
{
	MD5_CTX context;
	unsigned char digest[16];
	int i, len;
	char *tmp;

	MD5Init (&context);
	for (i=0; i < size; i++) {
		trim_len(len, tmp, src[i]);
		MD5Update(&context, tmp, len);
	}
	MD5Final(digest, &context);

	string2hex(digest, 16, dest);
	LM_DBG("MD5 calculated: %.*s\n", MD5_LEN, dest);
}

/*! \brief
  * Calculate a MD5 digest over a file.
  * This function assumes 32 bytes in the destination buffer.
  * \param dest destination
  * \param file_name file for that the digest should be calculated
  * \return zero on success, negative on errors
  */
int MD5File(char *dest, const char *file_name)
{
	if (!dest || !file_name) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	MD5_CTX context;
	FILE *input;
	unsigned char buffer[32768];
	unsigned char hash[16];
	unsigned int counter, size;

	struct stat stats;
    if (stat(file_name, &stats) != 0) {
		LM_ERR("could not stat file %s\n", file_name);
		return -1;
	}
	size = stats.st_size;

	MD5Init(&context);
	if((input = fopen(file_name, "rb")) == NULL) {
		LM_ERR("could not open file %s\n", file_name);
		return -1;
	}

	while(size) {
		counter = (size > sizeof(buffer)) ? sizeof(buffer) : size;
		if ((counter = fread(buffer, 1, counter, input)) <= 0) {
			fclose(input);
			return -1;
		}
		MD5Update(&context, buffer, counter);
		size -= counter;
	}
	fclose(input);
	MD5Final(hash, &context);

	string2hex(hash, 16, dest);
	LM_DBG("MD5 calculated: %.*s for file %s\n", MD5_LEN, dest, file_name);

	return 0;
}