views:

122

answers:

3

I'm developing an ASP .Net MVC application. One of my actions requires id as a parameter. For example:

public actionresult Detail(Guid id){
    return View();
}

As you can see, I'm using Guid instead of Int. The issue is more cosmetic. The url can be very long, such as localhost/Detail/0c157b42-379d-41d5-b9ba-83e9df9985b2.

Is it safe to take only parts of the Guid like localhost/Detail/0c157b42?

+2  A: 

No, it's not safe.

You can calculate a SHA-2 hash of it though, and take the first few characters of that.

Noon Silk
That would not be unique!
pihentagy
+1  A: 

No, you need the entire GUID since there is a possibility that a subset may not be unique.

For example:

0c157b42-379d-41d5-b9ba-83e9df9985b2

0c157b42-379d-41d5-b9ba-83e9df9985b3

Notice, only the last number is different. The beginnings are both the same. You can't use the trailing end of the GUID either since there's no way to predict what part of the GUID will change when its created.

Soviut
+2  A: 

GUID is designed in such a way that it is intended to be unique, but any part of it is not. See this blog post for details. If you need to shorten the GUID take a good hash of it - like SHA-1 or (if you don't have security concerns) MD5.

sharptooth
Shortening? You mean, you take a 36 char long GUID (if the example in the question is valid), and __shorten__ it to 32 characters? (__if__ you use md5, which will result in 32 chars, and not SHA1, which will result in 48 chars)
pihentagy
@pihentagy: The point is that after hashing you can take part of the hash and it will be random enough, but you can't take a part of the GUID itself.
sharptooth