tags:

views:

85

answers:

2

What is a good way to convert a file path (URI) into a System.Guid? I'd like to minimize the possibility of a collision, but I'm happy with a reasonably unique hashing (probably never more than a few dozen/hundred items in the database)

+6  A: 

There is no need. Hash URI with md5 or sha1 and that's all. GUIDs are not for hashing things. They meant to be created unique independently of everything else.

FractalizeR
Appreciate the spanking, but I've got to retrofit these into a system which already depends on Guids. EDIT: ah, I see md5 creates a 128bit number, which equals a Guid. I can go from here I think.
David Rutten
I am not sure, but I think there may be a collision with "natively generated" GUIDs this way if you generate a real lot of them
FractalizeR
hi, I am interested in knowing what program OR how you would convert the URI to md5 hash? is there a utility somewhere in VS? thanks!
VoodooChild
@VoodooChild, have a look at System.Security.Cryptography.MD5CryptoServiceProvider
David Rutten
Look at the implementations of the v3 (MD5) and v5 (SHA-1) UUIDs in RFC 4122, http://www.ietf.org/rfc/rfc4122.txt uuid_create_md5_from_name and in Python, http://svn.python.org/view/python/trunk/Lib/uuid.py?view=markup
George V. Reilly
If someone is asking how to hash something in .NET that's really useless to send him to RFCs...
FractalizeR
+2  A: 

If you have the option of a centralized registry/database, you could maintain a GUID <-> URL resolver and generate a new GUID for each URL you need. This would use GUIDs the way they are intended, minimizing probability of collision with natively-generated GUIDs.

Michael E
Michael, excellent suggestion. I'll keep this in mind in case I run into issues with the md5 encryption.
David Rutten