Add GChecksum, a generic wrapper around various hashing algorithms. At the

2007-12-04  Emmanuele Bassi  <ebassi@gnome.org>

	* glib/gchecksum.[ch]: Add GChecksum, a generic wrapper around
	various hashing algorithms. At the moment, the MD5, SHA-1 and
	SHA-256 algorithms are supported. (#443648)

	* glib/glib.h:
	* glib/Makefile.am:
	* glib/glib.symbols: Build glue for GChecksum

	* tests/Makefile.am
	* tests/checksum-test.c: Add test suite for GChecksum.

svn path=/trunk/; revision=6042
This commit is contained in:
Emmanuele Bassi 2007-12-04 16:31:51 +00:00 committed by Emmanuele Bassi
parent 450c704f3e
commit f17db34652
8 changed files with 1669 additions and 0 deletions

View File

@ -1,3 +1,16 @@
2007-12-04 Emmanuele Bassi <ebassi@gnome.org>
* glib/gchecksum.[ch]: Add GChecksum, a generic wrapper around
various hashing algorithms. At the moment, the MD5, SHA-1 and
SHA-256 algorithms are supported. (#443648)
* glib/glib.h:
* glib/Makefile.am:
* glib/glib.symbols: Build glue for GChecksum
* tests/Makefile.am
* tests/checksum-test.c: Add test suite for GChecksum.
2007-12-03 Ryan Lortie <desrt@desrt.ca>
* glib/ghash.c: no code changes; add comments to document the internal

View File

@ -103,6 +103,7 @@ libglib_2_0_la_SOURCES = \
gbookmarkfile.c \
gbsearcharray.h \
gcache.c \
gchecksum.c \
gcompletion.c \
gconvert.c \
gdataset.c \
@ -185,6 +186,7 @@ glibsubinclude_HEADERS = \
gbase64.h \
gbookmarkfile.h \
gcache.h \
gchecksum.h \
gcompletion.h \
gconvert.h \
gdataset.h \

1398
glib/gchecksum.c Normal file

File diff suppressed because it is too large Load Diff

70
glib/gchecksum.h Normal file
View File

@ -0,0 +1,70 @@
/* gchecksum.h - data hashing functions
*
* Copyright (C) 2007 Emmanuele Bassi <ebassi@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __G_CHECKSUM_H__
#define __G_CHECKSUM_H__
#include <glib/gtypes.h>
G_BEGIN_DECLS
/**
* GChecksumType:
* @G_CHECKSUM_MD5: Use the MD5 hashing algorithm
* @G_CHECKSUM_SHA1: Use the SHA-1 hashing algorithm
* @G_CHECKSUM_SHA256: Use the SHA-256 hashing algorithm
*
* The hashing algorithm to be used by #GChecksum when performing the
* digest of some data.
*
* The #GChecksumType enumeration can be extended at later date to include
* new hashing algorithm types.
*
* Since: 2.16
*/
typedef enum {
G_CHECKSUM_MD5,
G_CHECKSUM_SHA1,
G_CHECKSUM_SHA256
} GChecksumType;
typedef struct _GChecksum GChecksum;
GChecksum * g_checksum_new (GChecksumType checksum_type);
GChecksum * g_checksum_copy (const GChecksum *checksum);
void g_checksum_free (GChecksum *checksum);
void g_checksum_update (GChecksum *checksum,
const guchar *data,
gsize length);
G_CONST_RETURN gchar *g_checksum_get_string (GChecksum *checksum);
void g_checksum_get_digest (GChecksum *checksum,
guint8 **digest,
gsize *digest_len);
gchar *g_compute_checksum_for_data (GChecksumType checksum_type,
const guchar *data,
gsize length);
gchar *g_compute_checksum_for_string (GChecksumType checksum_type,
const gchar *str,
gsize length);
G_END_DECLS
#endif /* __G_CHECKSUM_H__ */

View File

@ -35,6 +35,7 @@
#include <glib/gbase64.h>
#include <glib/gbookmarkfile.h>
#include <glib/gcache.h>
#include <glib/gchecksum.h>
#include <glib/gcompletion.h>
#include <glib/gconvert.h>
#include <glib/gdataset.h>

View File

@ -171,6 +171,19 @@ g_cache_value_foreach
#endif
#endif
#if IN_HEADER(__G_CHECKSUM_H__)
#if IN_FILE(__G_CHECKSUM_C__)
g_checksum_new
g_checksum_copy
g_checksum_free
g_checksum_update
g_checksum_get_string
g_checksum_get_digest
g_compute_checksum_for_data
g_compute_checksum_for_string
#endif
#endif
#if IN_HEADER(__G_COMPLETION_H__)
#if IN_FILE(__G_COMPLETION_C__)
g_completion_add_items

View File

@ -91,6 +91,7 @@ test_programs = \
base64-test \
bit-test \
$(CXX_TEST) \
checksum-test \
child-test \
completion-test \
convert-test \
@ -163,6 +164,7 @@ array_test_LDADD = $(progs_ldadd)
base64_test_LDADD = $(progs_ldadd)
bit_test_LDADD = $(progs_ldadd)
bookmarkfile_test_LDADD = $(progs_ldadd)
checksum_test_LDADD = $(progs_ldadd)
child_test_LDADD = $(thread_ldadd)
completion_test_LDADD = $(progs_ldadd)
convert_test_LDADD = $(progs_ldadd)

170
tests/checksum-test.c Normal file
View File

@ -0,0 +1,170 @@
#include "config.h"
#include <glib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>
#define FIXED_STR "The quick brown fox jumps over the lazy dog"
#define FIXED_LEN (strlen (FIXED_STR))
#define MD5_FIXED_SUM "9e107d9d372bb6826bd81d3542a419d6"
#define SHA1_FIXED_SUM "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
#define SHA256_FIXED_SUM "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
#define BLOCK_SIZE 256
static gchar *
digest_to_string (guint8 *digest,
gsize digest_len)
{
static const gchar hex_digits[] = "0123456789abcdef";
gint len = digest_len * 2;
gchar *retval;
gint i;
retval = g_new (gchar, len + 1);
for (i = 0; i < digest_len; i++)
{
retval[2 * i] = hex_digits[digest[i] >> 4];
retval[2 * i + 1] = hex_digits[digest[i] & 0xf];
}
retval[len] = 0;
return retval;
}
static void
test_checksum (GChecksumType checksum_type,
const gchar *type,
const gchar *sum,
const gchar *filename)
{
GChecksum *checksum0, *checksum1, *checksum2;
gchar *data;
guchar *p;
gsize data_len;
guint8 *digest1, *digest2;
gsize len1, len2;
gchar *digest_str1, *digest_str2;
checksum0 = g_checksum_new (checksum_type);
g_checksum_update (checksum0, (const guchar *) FIXED_STR, FIXED_LEN);
if (strcmp (g_checksum_get_string (checksum0), sum) != 0)
{
g_print ("Invalid %s checksum for `%s': %s (expecting: %s)\n",
type,
FIXED_STR,
g_checksum_get_string (checksum0),
sum);
exit (1);
}
g_checksum_free (checksum0);
checksum1 = g_checksum_new (checksum_type);
checksum2 = g_checksum_new (checksum_type);
if (!g_file_get_contents (filename, &data, &data_len, NULL))
{
g_print ("Could not load `%s' contents\n", filename);
exit (1);
}
g_checksum_update (checksum1, (const guchar *) data, data_len);
p = (guchar *) data;
do
{
if (data_len > BLOCK_SIZE)
{
g_checksum_update (checksum2, p, BLOCK_SIZE);
data_len -= BLOCK_SIZE;
p += BLOCK_SIZE;
}
else
{
g_checksum_update (checksum2, p, data_len);
break;
}
}
while (*p != '\0');
g_free (data);
digest1 = NULL;
g_checksum_get_digest (checksum1, &digest1, &len1);
if (!digest1)
{
g_print ("No %s digest found for checksum1\n", type);
exit (1);
}
digest2 = NULL;
g_checksum_get_digest (checksum2, &digest2, &len2);
if (!digest2)
{
g_print ("No %s digest found for checksum2\n", type);
exit (1);
}
digest_str1 = digest_to_string (digest1, len1);
digest_str2 = digest_to_string (digest2, len2);
if (strcmp (digest_str1, digest_str2) != 0)
{
g_print ("Wrong %s digest `%s' (expecting: %s)\n",
type,
digest_str1,
digest_str2);
exit (1);
}
g_free (digest_str1);
g_free (digest_str2);
g_free (digest1);
g_free (digest2);
digest_str1 = g_strdup (g_checksum_get_string (checksum1));
digest_str2 = g_strdup (g_checksum_get_string (checksum2));
if (!digest_str1 || !digest_str2)
{
g_print ("No %s digest string found\n", type);
exit (1);
}
if (strcmp (digest_str1, digest_str2) != 0)
{
g_print ("Wrong %s digest string `%s' (expecting: %s)\n",
type,
digest_str1,
digest_str2);
exit (1);
}
g_free (digest_str1);
g_free (digest_str2);
g_checksum_free (checksum1);
g_checksum_free (checksum2);
}
#define test(type) test_checksum (G_CHECKSUM_##type, \
#type, \
type##_FIXED_SUM, \
__FILE__)
int
main (int argc, char *argv[])
{
test (MD5);
test (SHA1);
test (SHA256);
return EXIT_SUCCESS;
}