Lanaru
Lanaru

Reputation: 9721

Algorithm to convert string to GUID

I am trying to create a function that takes an arbitrary string as input and creates a GUID based on that string. The GUID's format should be ideally the GUID struct from objbase.h, but it's not that important. What is important is that the function is deterministic but always (or close enough to always) creates a different GUID for different strings.

What kind of algorithm could I use to do that? I've thought about bit operations on the contents of the string but I would appreciate some direction.

Upvotes: 1

Views: 1546

Answers (3)

Sean U
Sean U

Reputation: 6850

What about computing an MD5 hash of the string and treating it as if it were a GUID? GUIDs and MD5 hashes are both 128-bit values.

Upvotes: 2

user1202136
user1202136

Reputation: 11557

Check the specifications for UUID version 3 and 5:

  1. http://en.wikipedia.org/wiki/Uuid#Version_3_.28MD5_hash.29
  2. http://en.wikipedia.org/wiki/Uuid#Version_5_.28SHA-1_hash.29

In essence, you compute MD5 (or SHA-1) of your string and store the results verbatim in a 128-bit UUID. For SHA-1, you need to truncate the result, as SHA-1 returns 160-bits.

Upvotes: 1

Jonathan Paulson
Jonathan Paulson

Reputation: 1058

You can hash the string and then use the first 16 bytes of the hash to create a GUID (http://msdn.microsoft.com/en-us/library/90ck37x3.aspx)

Upvotes: 1

Related Questions