Generating strong repeatable GUIDs
In recent weeks I've had the problem of wanting to generate GUIDs that are distinct, but are repeatable -- in particular for things like COM class UIDs, and for installer component IDs, for a project using a lot of generated code.
So this is what I did in part of my build scripts (note, this is not totally RFC 4122 compliant, but it suffices in practice)
def GuidFromHash(bigstring): | |
hash = MD5.Create() | |
data = hash.ComputeHash(Encoding.UTF8.GetBytes(bigstring)) | |
hash.Clear() | |
return System.Guid(data).ToString().ToUpperInvariant() |
The input is the individual name of the thing I'm generating, concatenated with a constant GUID string used as a salt. The ToUpperInvariant
is there to permit WiX to be run in pedantic mode.
Yes, it's exactly as vulnerable to hash collisions as MD5 -- but then my input is not maliciously aiming at such.
Later:-- or you could just use
import uuid | |
import clr | |
def GuidFromHash(explicit_salt_guid, tag): | |
return str(uuid.uuid3(explicit_salt_guid, tag)).ToUpperInvariant() |
but that makes the RFC 4122 guid-for-salt explicit for you.
No comments :
Post a Comment