How are .NET 4 GUIDs generated?

后端 未结 2 2048
小鲜肉
小鲜肉 2020-11-27 17:34

I am aware of the multitude of questions here as well as Raymond\'s excellent (as usual) post. However, since the algorithm to create GUIDs was changed apparently, I found i

相关标签:
2条回答
  • 2020-11-27 17:55

    Since Windows 2000 Microsoft uses a version 4 algorithm:

    With Windows 2000, Microsoft switched to version 4 GUIDs, since embedding the MAC address was viewed as a security risk. 1

    You can see that as well from a GUID generated in .NET (from Wikipedia):

    Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx with any hexadecimal digits for x but only one of 8, 9, A, or B for y. e.g. f47ac10b-58cc-4372-a567-0e02b2c3d479.

    A version 4 UUID consist of 122 significant bits, giving 2^122 distinct values which is a very large number. Given a set of H values, the expected number of values we have to choose before finding the first random collision with a 50% chance can be calculated as follows (see Birthday Attack on Wikipedia):

    alt text

    The result (birthday bound) for 2^122 different values is approximately 2,89e+18. This assumes that the generated values are randomly distributed. Obviously, if the values are distributed unevenly, a random collision can be found faster. For further details also see Random UUID probability of duplicates.

    1As a matter of fact, the author of the Melissa worm could be tracked down due to a GUID generated using a version 1 algorithm.

    0 讨论(0)
  • 2020-11-27 18:08

    Yes, there was a change in .NET 4.0, Guid.NewGuid() directly calls CoCreateGuid(), a small wrapper around UuidCreate(). Previous versions of .NET called a helper function in the CLR, GuidNative::CompleteGuid(). Which calls CoCreateGuid. Not sure why this change was made, smells like nothing more than a minor optimization.

    At any rate, the exact same Windows function generates the Guid, the algorithm has been the same for the past 10 years, it is as reliable as it ever was.

    0 讨论(0)
提交回复
热议问题