Generate a GUID from a String

A GUID is a 128-bit integer (16 bytes).
To generate a GUID from a string, you need to apply a hash function to the string to generate a 16 byte hash. Then pass this hash result into the Guid constructor.

In this example I used the MD5 hash function:

Guid guid;
String text = "test";
using (MD5 md5 = MD5.Create())
{
	byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text));
	guid = new Guid(hash);
}
Advertisements

Comments are closed.

%d bloggers like this: