Deterministic Uuid from arbitrary string


#1

I needed a way to generate a Uuid deterministically from an arbitrary String.

This did the trick and, as far as I can tell, seems to satisfy the RFC-4122 version 3 standard (although it doesn't use any 'namespace' in generation). Thought I'd share it so that (1) it might help someone else, and (2) someone can suggest a more 'authentic' alternative :) .

Uuid generateDeterministicUuid (const String& sourceString)
{
    MD5 checksum (sourceString.toUTF8());
    // Force bits to ensure it meets RFC-4122 v3 (MD5) standard...
    uint8 buffer[16];
    memcpy (buffer, checksum.getChecksumDataArray(), sizeof (buffer));
    buffer[6] = (buffer[6] & 0x0f) | (3 << 4); // version '3' shifted left into top nibble
    buffer[8] = (buffer[8] & 0x3f) | 0x80;
    return Uuid (const_cast<const uint8*>(buffer));
}

Would be nice for something like this to be a static function in Uuid of course - though I understand it might make sense to keep the class as v4 only.


Deterministic UUID generation