Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a GUID for a string?

Tags:

c#

I am having a problem generating a GUID for a string - for example:

Guid g = New Guid("Mehar"); 

How can I compute a GUID for "Mehar"? I am getting an exception.

like image 228
Mehar Avatar asked Feb 03 '10 09:02

Mehar


People also ask

How do I create a GUID?

Open Visual Studio->Tools->Create GUID->Registry Format->New GUID. It will create a new GUID every time you click New GUID.

How does a GUID get generated?

Basically, a a GUID is generated using a combination of: The MAC address of the machine used to generate the GUID (so GUIDs generated on different machines are unique unless MAC addresses are re-used) Timestamp (so GUIDs generated at different times on the same machine are unique)

What is a GUID string?

Guid(String) Initializes a new instance of the Guid structure by using the value represented by the specified string. Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Initializes a new instance of the Guid structure by using the specified unsigned integers and bytes.

Is GUID a string or number?

A GUID is a 16-byte (128-bit) number, typically represented by a 32-character hexadecimal string.


2 Answers

Quite old this thread but this is how we solved this problem:

Since Guid's from the .NET framework are arbitrary 16bytes, or respectively 128bits, you can calculate a Guid from arbitrary strings by applying any hash function to the string that generates a 16 byte hash and subsequently pass the result into the Guid constructor.

We decided to use the MD5 hash function and an example code could look like this:

string input = "asdfasdf"; using (MD5 md5 = MD5.Create()) {     byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(input));     Guid result = new Guid(hash); } 

Please note that this Guid generation has a few flaws by itself as it depends on the quality of the hash function! If your hash function generates equal hashes for lots of string you use, it's going to impact the behaviour of your software.

Here is a list of the most popular hash functions that produce a digest of 128bit:

  • RIPEMD (probability of collision: 2^18)
  • MD4 (probability of collision: for sure)
  • MD5 (probability of collision: 2^20.96)

Please note that one can use also other hash functions that produce larger digests and simply truncate those. Therefore it may be smart to use a newer hash function. To list some:

  • SHA-1
  • SHA-2
  • SHA-3

Today (Aug 2013) the 160bit SHA1 hash can be considered being a good choice.

like image 142
Nachbars Lumpi Avatar answered Sep 22 '22 03:09

Nachbars Lumpi


I'm fairly sure you've confused System.Guid with wanting a hash (say, SHA-256) of a given string.

Note that, when selecting a cryptographically-secure hashing algorithm, MD5, SHA0 and SHA1 are all generally considered dead. SHA2 and up are still usable.

like image 31
Noon Silk Avatar answered Sep 20 '22 03:09

Noon Silk