55

I know this may sounds like a pointless question, but hear me out...

I basically want to know if I can trust the GUID to generate a value which will be unique 100% of the time and impossible to predict.

I'm basically rolling my on login system for a website and want to know if the GUID is secure enough for session cookies.

Any background on how the GUID is generated would be much appreciated in evaluating the answers.

Thanks for the links to duplicate questions, however, my question is specific to the .Net framework.

GateKiller
  • 74,180
  • 73
  • 171
  • 204
  • Dupe: http://stackoverflow.com/questions/184869/are-guid-collisions-possible – John Sheehan Jan 21 '09 at 22:25
  • And another: http://stackoverflow.com/questions/39771/is-a-guid-unique-100-of-the-time – John Sheehan Jan 21 '09 at 22:25
  • And a third: http://stackoverflow.com/questions/462219/xkcd-random-number (ok, not really, but somewhat applicable) – Shog9 Jan 21 '09 at 22:29
  • No, this is not a duplicate. The OP asks about randomness and predictability, not uniqueness – Dirk Vollmar Jan 21 '09 at 22:32
  • BTW: it's not the question you asked, but i suspect it's the question you wanted to ask: http://stackoverflow.com/questions/22880/what-is-the-best-way-to-prevent-session-hijacking – Shog9 Jan 21 '09 at 22:32
  • Not a duplicate. He's asking something new here: whether GUIDs are impossible to predict, and whether they are secure (both vaguely related to each other). – Jason S Jan 21 '09 at 22:34
  • AFAICT none of the replies or comments to this question actually answers it. fortunately another question http://stackoverflow.com/questions/2621563/how-random-is-system-guid-newguid-take-two does have the answer (which is basically it's not cryptographically random) – Andy Oct 27 '15 at 11:45
  • Version 4 is cryptographically secure random: https://security.stackexchange.com/a/7945/89421 – Matty Feb 08 '19 at 10:21

8 Answers8

30

Here's an excellent breakdown by Raymond Chen

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • 1
    @John: Is this the algorithm used in .NET? – GateKiller Jan 21 '09 at 22:29
  • I have accepted this answer on the assumption that this is how GUID's are generated in the .NET framework. Thanks John :) – GateKiller Jan 21 '09 at 22:42
  • I couldn't find proof, but Raymond is a Win32 genius and if you use Reflector to check out System.Guid, it eventually calls out of .NET (I think, I am not a Reflector Ninja). – John Sheehan Jan 21 '09 at 22:43
  • @John: you're correct. Eventually, it'll end up calling CoCreateGuid() – Shog9 Jan 21 '09 at 22:52
  • 5
    So in conclusion, they are *not* very random. That is, consecutive guids from the same server will have a lot in common. – Michael Haren Mar 04 '10 at 19:47
  • 11
    This is not the algorithm used in .NET . This describes Version 1. .NET uses version 4 (see http://en.wikipedia.org/wiki/Universally_Unique_Identifier#Version_4_.28random.29 for a brief description of version 4) – awe Jul 27 '10 at 10:10
  • 12
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – SuperBiasedMan Nov 11 '15 at 14:55
  • 3
    How does the article even answer the question? What does the article say about the randomness and predictability of GUID generation? I think http://blogs.msdn.com/b/oldnewthing/archive/2012/05/23/10309199.aspx answers the question much better which is that GUID generation is practically unique but not random and also predictable. – Bochu Nov 18 '15 at 20:45
  • Agree with Bochu -- the essential point is that *unique* and *random* are different. Even if you use the whole GUID (unique!), it is not suitable for security – Mark Sowul Feb 27 '17 at 17:12
  • I think that article is not very relevant. .NET GUIDs are effectively crypto-random to 122 bits; see my comment on Marc Gravell's answer below. – Jordan Rieger May 29 '18 at 00:32
  • Oh God! I've been using the first part of guid to save forms and when a user clicks his/her result the site might display another saved form data. Now, it's clear why... – Nime Cloud Oct 21 '22 at 11:29
13

No fixed-length value can ever guarantee to be 100% unique (just call it enough times, give or take the universe ending ;-p) - but it can be very, very, very unlikely to duplicate.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    According to https://msdn.microsoft.com/en-us/library/bb417a2c-7a58-404f-84dd-6b494ecf0d13#id11, since Windows 2000 back in 1999, "the random bits for all version 4 GUIDs built in Windows are obtained via the Windows CryptGenRandom cryptographic API or the equivalent, the same source that is used for generation of cryptographic keys". So I'd consider them cryptographically secure -- at least to the extent of the 122 bits of entropy they provide. – Jordan Rieger May 29 '18 at 00:32
5

I can't speak to the predictability of sequential numbers but it will be unique. I think you'd be better off using a random number generator from System.Security.Cryptography, though. Tie a random number with a monotonically increasing value (time) to generate your unique key and you can be sure that it is unique and not predictable.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
4

I basically want to know if I can trust the GUID to generate a value which will be unique 100% of the time and impossible to predict.

I'm basically rolling my on login system for a website and want to know if the GUID is secure enough for session cookies.

Short answer: not at all. It's important to note that unique and random are completely different. If you had a universal counter (like taking a number at a deli), those numbers are unique, but completely predictable.

As Bochu notes above, Raymond's post here talks about it: https://devblogs.microsoft.com/oldnewthing/20120523-00/?p=7553

The GUID generation algorithm was designed for uniqueness. It was not designed for randomness or for unpredictability. Indeed, if you look at an earlier discussion, you can see that so-called Algorithm 1 is non-random and totally predictable. ... Even the Version 4 GUID algorithm (which basically says "set the version to 4 and fill everything else with random or pseudo-random numbers") is not guaranteed to be unpredictable, because the algorithm does not specify the quality of the random number generator.

For secure random numbers, you need a crytographically secure random number generator.

By the way, "rolling my own login system" is a security red flag -- I would be remiss if I didn't point that out.

Pang
  • 9,564
  • 146
  • 81
  • 122
Mark Sowul
  • 10,244
  • 1
  • 45
  • 51
4

The documentation for System.Guid.NewGuid() makes no guarantees for randomness, so while the current implementation is based on a random number generator (it's version 4 of the algorithm, which was devised after privacy concerns arose from version 1 which used the MAC address; other system's like Apple's OS X still use version 1 of the algorithm).

So while you have a very high probabilty of System.Guid.NewGuid() generating a unique value, you can't make any assumptions about its predictability because that's not specified by the documentation.

James Williams
  • 1,861
  • 1
  • 15
  • 21
  • Just for reference: A short description on how Version 4 is built up: http://en.wikipedia.org/wiki/Universally_Unique_Identifier#Version_4_.28random.29 – awe Jul 27 '10 at 10:21
3

I dunno about .NET, but the UUID algorithm is defined fairly precisely.

edit: if you look at the appropriate bits (see wikipedia entry), that should explain which version of UUID is being used.

edit 2: a red flag for your use of the word "secure", which tells me you're better off using a well-defined cryptographic method. For example, when generating session IDs on a server, why just not do something simple like apply an MD5 hash to the concatenation of an appropriate subset of the following: {client machine IP address, sequentially incremented counter, fixed secret constant of your choice, output from random number generator of your choice, etc.} ?

Jason S
  • 184,598
  • 164
  • 608
  • 970
3

Assuming that System.Guid.NewGuid uses CoCreateGuid, it is not random at all. Historically, the algorithm used for creating guids was to incorporate the MAC address from a network adapter, in addition to some other things like the time. I'm not sure if the algorithm has changed. While it certainly is not random, it is guaranteed to be unique.

Tron
  • 1,397
  • 10
  • 11
1

GUIDs are, by definition, unique in all regards. There were, once upon a time, some GUID0-generation routines that were generating sequential GUIDs, but those were problems in... Win98, I think, and were hotfixed by Microsoft.

You should be able to trust a generated GUID to be unique and never repeated or regenerated.

(EDIT: Having said that, we all understand that a string of alphanumeric characters has a fixed number of permutations, if the string is fixed in length. But in the case of a GUID the number of permutations is economical*.)

(* Dammit, where's that XKCD where the proposes "astronomic" numbers aren't large enough?)

JMD
  • 7,331
  • 3
  • 29
  • 39
  • How about "27" and, er, "27". Even when generated sensibly, they aren't really *guaranteed* to be unique - just that repeats are suitable unlikely enough. – Marc Gravell Jan 21 '09 at 22:32