Hi
I have a problem for generating GUID for strings. How can we do that?
Example- GUID g= New GUID("Mehar");
How can i get the guid for "Mehar". I am getting an exception?
Regards mehar
Hi
I have a problem for generating GUID for strings. How can we do that?
Example- GUID g= New GUID("Mehar");
How can i get the guid for "Mehar". I am getting an exception?
Regards mehar
You use the static NewGuid() method to create Guids, I.E., Guid g = Guid.NewGuid();
or string s = Guid.NewGuid().ToString()'
What are you trying to do? Are you trying to generate a unique value based on the string, in which case you want to hash, e.g. http://stackoverflow.com/questions/2112685/how-do-one-way-hash-functions-work
You cannot use GUID that way. The constructor of Guid expects a valid, string representation of a Guid.
What you're looking for is called a Hash function. (for example: MD5)
I think you have a misunderstanding of what a Guid actually is. There is no Guid representation of a string such as "Mehar".
The reason there is a new Guid(String s)
overload is so that you can create a guid from a typical string representation of one, such as "00000000-0000-0000-0000-000000000000".
See the wiki article for more information on what a Guid actually is.
Guids are random, they are not intrinsically assigned to any string or other value.
If you need such linking, store the guids in a Dictionary and check for an existing guid first before creating a new one.
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.
What you are looking for is probably generating version 3 or version 5 UUIDs, which are name based UUIDs. (version 5 is the recommended). I don't think that the .NET framework has build in support for it. See http://en.wikipedia.org/wiki/Universally_Unique_Identifier
I did a few google searches to see if I could find something in the Win32 API, but nothing came up. However, I am sure that the .NET framework has some implementation hidden somewhere, because as far as I know, when generating a COM object in .NET, and you don't supply an explicit GUID, then the .NET framework generates a name based UUID to create a well-defined ClassID and InterfaceID, i.e. UUIDs that don't change every time you recompile (like VB6). But this is probably hidden, so I guess you need to implement the algorithm yourself. Luckily, .NET provides both an MD5 and SHA1 algorithm so I don't think implementing a version3 and version5 UUID should be too difficult.