Unlike normal GUIDs these are not unique in the traditional sense of the word so don’t use this code for anything other than unit tests
public static class GuidExtension { public static Guid ToGuid(this string input) { var md5 = System.Security.Cryptography.MD5.Create(); var inputBytes = System.Text.Encoding.ASCII.GetBytes(input); var hash = md5.ComputeHash(inputBytes); var result = new Guid(hash); return result; } }Use the extension method like this
Guid TestGuid = "TestGuid".ToGuid();
This will create the following Guid
'add851a2-b07a-f497-64b3-b8314743c9f1'
No comments:
Post a Comment