Re: GChecksum assertion failed



On Thu, 2008-08-07 at 11:08 +0200, Luka Napotnik wrote:
Hello.

I have this small example where I create a GChecksum and apply some
data. The problem is when I request the digest, the program prints out
the following error:

(process:8422): GLib-CRITICAL **: g_checksum_get_digest: assertion
`*digest_len >= len' failed

My GChecksum code is:
------------------------------------------------------------
char data[] = "Hello, World";
int len;
GChecksum *checksum;
guint8 *b_digest;
gsize b_digest_len;
 
checksum = g_checksum_new(G_CHECKSUM_MD5);
len = strlen(data) + 1;
b_digest_len = len;
g_checksum_update(checksum, data, len);
g_checksum_get_digest(checksum, b_digest, &b_digest_len);
------------------------------------------------------------

I initialize the digest len with the length of data. Am I doing
something wrong?

yes, you're not reading the documentation:

  @digest_len:

  The caller initializes it to the size of @buffer. After the call it
  contains the length of the digest.

the buffer is a pointer to an array of bytes, since
g_checksum_get_digest() doesn't handle the string you pass to
g_checksum_update(). as GChecksum handles different hashing algorithms,
the array size must be at least big enough to fit the actual size of the
digest. the size of the digest for a particular checksum algorithm is
obtained using:

  gsize digest_len = g_checksum_type_get_length (G_CHECKSUM_MD5);

which can then be used to allocate an array of bytes:

  guint8 *digest = g_new (guint8, digest_len);

and then you can get the digest:

  g_checksum_get_digest (checksum, digest, &digest_len);

you then obviously need to free the digest when you're done using it:

  g_free (digest);

this obviously implies you want the digest as a raw binary vector. if
you want the hexadecimal version in string you're much better off using
g_compute_checksum_for_string() instead.

ciao,
 Emmanuele.

-- 
Emmanuele Bassi,
W: http://www.emmanuelebassi.net
B: http://log.emmanuelebassi.net




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]