We would like to generate a Guid based on some random string. The goal would be that the Guid is always the same for a given string.
Just to be clear, my string is not formated as a guid, it could be "Toto" as "asdfblkajsdflknasldknalkvkndlskfj".
I know that this would generate some Guid that is as unique as my input string, but it's not the question here.
Since both a Guid
and a MD5 hash are 128 bits integers, you could use the MD5 of a given string as the Guid
. It will reproduce the same value consistently:
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
Guid g = new Guid(hashBytes);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With