]> git.street.me.uk Git - andy/viking.git/blame - src/md5_hash.c
Some spelling fixes in a comment
[andy/viking.git] / src / md5_hash.c
CommitLineData
bd27baa4
RN
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2016 Rob Norris <rw_norris@hotmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#include "md5_hash.h"
22#include <glib.h>
23#include <glib/gstdio.h>
24#include <string.h>
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28#ifdef HAVE_LIBNETTLE
29#include <nettle/md5-compat.h>
30#endif
31
32/**
33 * Get MD5 hash of a string using a library function
34 */
35char *md5_hash(const char *message)
36{
37 char *answer = NULL;
38#ifdef HAVE_LIBNETTLE
39 unsigned char result[16];
40 MD5_CTX ctx;
41 MD5Init ( &ctx );
42 MD5Update ( &ctx, (const unsigned char*)message, strlen(message) );
43 MD5Final ( &result[0], &ctx );
44 /*
45 g_printf ( "%s: of string '%s' is: ", __FUNCTION__, message );
46 for(int i = 0; i < 16; i++)
47 g_printf ( "%02x", result[i]);
48 g_printf("\n");
49 */
50 // Should be nicer way of converting this but I can't think of one ATM
51 answer = g_strdup_printf ( "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
52 result[0],result[1],result[2],result[3],result[4],result[5],result[6],result[7],
53 result[8],result[9],result[10],result[11],result[12],result[13],result[14],result[15]);
54#else
55 // Return something that might vaguely work in case you've disabled MD5 support
56 // but you should know what you've doing and uses of it (such as thumbnail caching) now won't work well
57 guint value = g_str_hash ( message );
58 answer = g_strdup_printf ( "%d", value );
59#endif
60 return answer;
61}