[redland-dev] Digests, Endianness and Bit Order

Dave Beckett dave.beckett at bristol.ac.uk
Sun Apr 17 16:02:15 BST 2005


So, a bit more analysis later, I think there is no bug in Redland.
(tracking bug: http://bugs.librdf.org/mantis/view.php?id=22 )


The original test code was:

    u_int64_t iHash;

    ...
(1) librdf_digest_update(oDig,"foobar", sizeof("foobar"));

     ...

(2)  memcpy(&iHash, (void*) librdf_digest_get_digest(oDig), sizeof(iHash));
     printf("Digest: %llu\n",iHash);

Bug (1) - minor one.  If you use sizeof like that on a string it adds
the \0 into the length, so you can't check the MD5sum is right with

  $ echo -n 'foobar' | md5sum
  3858f62230ac3c915f300c664312c63f  -

Bug (2) - serious.  This is not a portable way to use the first 4
bytes of the digest.  When you memcpy into a u_int64_t, that will not
work across different endian systems to calculate the same value.
Instead, you need to do something like:

  void *ptr=librdf_digest_get_digest(oDig);
  iHash = ptr[0] + (ptr[1]<<8) + (ptr[2]<<16) + (ptr[3]<<24)


I checked the digest unit tests in Redland that each system gets the
same bytes back on PPC and i386 and they still do.

If you add these lines to your test program, you'll
see that the digest bytes really are the same:
    printf("Digest hex: ");
    librdf_digest_print(oDig, stdout);
    fputc('\n', stdout);

Just grabbing the first 4 bytes non-portably.

Dave


More information about the redland-dev mailing list