Geeks With Blogs
Freestyle Coding Programming in the Real World

You know those seemingly random Guids? Guess what? They're not random.

I'll give your head a moment to recover from that bombshell.

Ok, back with me? Good.

I was digging DEEP in some hardcore specs when I stumbled upon this. I had a situation where I have a Guid as a unique identifier. This identifier had some subsequent child data that needed to refer to the global object but required different handling. Yes, I'm being slightly vague. I do have to respect company proprietary data.

Due to this dilemma, I had a really weird "hash table" to quickly find the identifier from the child object. While this solution worked, and was REALLY fast, it was very messy and complex. I decided I needed to hack something to reduce this complexity.

I know. I just said I wanted to reduce complexity, and that my solution was to hack Guids. The sick humor of this is not wasted on me. You knew what you're getting into when you read my blog.

Turns out, there is a status field in the Guid. I was scouring the Wikipedia article on Guids when I read the Binary Encoding section. Turns out 3 of the bits have a special meaning. More specifically, if you were dealing with the Guid 00000000-0000-0000-x000-000000000000, part of the hex value of x means something. Guids in .NET, generated by the System.Guid structure, are all "standard" Guids. As such, they will always have the bit mask of 00000000-0000-0000-8000-000000000000.

If you don't believe me, go do a select on your largest database column. That hex value will always be either 8, 9, A or B.

If we exploit this, we now have 2 bits in our 128 bit number which we can control. We know those 2 bits being 10 will refer to our master identifier. However, since they are fixed, we can use 00, 01 and 11 as attached, but still unique, child Guids. This child Guids will always point back to the parent Guid by restoring those 2 bits to 10.

Happy Friday, people.

Posted on Friday, May 17, 2013 3:48 PM .NET , C# | Back to top

Comments on this post: Exploiting the Non-Uniqueness of Guids

# re: Exploiting the Non-Uniqueness of Guids
Requesting Gravatar...
This is awesome!! really helpful for me. Thanks for sharing with us. Following links also helped me to complete my task.
Left by Andrew Deniel on May 31, 2013 10:31 AM

Your comment:
 (will show your gravatar)

Copyright © Chris Gardner | Powered by: