title: How securely unguessable are GUIDs?
body: <p>A while ago I worked on a web application where users could buy tickets. Due to the way our client's processes worked, what you effectively got as a result of your purchase was a URL with the ticket number in it.</p>
  <p>These were tickets to buy property in the Middle East, and each ticket was potentially worth around $3,000,000. Clearly dishing out sequential integers would have been a bad idea. We used GUIDs as they're basically unguessable, but my question is: are they secure enough?</p>
  <p>As I understand it, the GUIDs .NET produces are totally pseudo-random (except for a few non-varying bits). However, I don't know what algorithm is used to generate them.</p>
  <p>The MSDN documentation tells us that <a href="http://msdn.microsoft.com/en-us/library/system.random.aspx" rel="nofollow noreferrer"><code>Random</code></a> is fast and insecure, and <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx" rel="nofollow noreferrer"><code>RNGCryptoServiceProvider</code></a> is slow and secure. That is, it's reasonable to assume someone could put in enough effort to predict the outcome of <code>Random</code>, but not of <code>RNGCryptoServiceProvider</code>.</p>
  <p>If you saw a long enough sequence of GUIDs, would it be possible to predict futures ones? If so, how many would you need to see?</p>
  <p>[In our particular case there were physical security checks later on - you had to present the passport you used to buy your ticket - so it wouldn't have been <em>too</em> bad if someone had guessed someone else's GUID, so we didn't sweat it at the time. The convenience of using the GUID as a database key made it a useful datatype to use.]</p>
  <hr />
  <p><strong>Edit:</strong></p>
  <p>So the answer is &quot;not enough&quot;.</p>
  <p>Using <a href="https://stackoverflow.com/users/40347/0xa3">0xA3</a>'s answer below, and following links from the <a href="https://stackoverflow.com/questions/2621563/how-random-is-system-guid-newguid-take-two">question</a> he linked to, the following code will generate a cryptographically random GUID that's valid by <a href="https://www.rfc-editor.org/rfc/rfc4122#section-4.4" rel="nofollow noreferrer">Section 4.4 of RFC 4122</a>:</p>
  <pre><code>static Guid MakeCryptoGuid()
  {
      // Get 16 cryptographically random bytes
      RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
      byte[] data = new byte[16];
      rng.GetBytes(data);
  
      // Mark it as a version 4 GUID
      data[7] = (byte)((data[7] | (byte)0x40) &amp; (byte)0x4f);
      data[8] = (byte)((data[8] | (byte)0x80) &amp; (byte)0xbf);
  
      return new Guid(data);
  }
  </code></pre>
  <p>This produces GUIDs much more slowly than <code>Guid.NewGuid()</code>, but with 122 bits of &quot;very random&quot; data, they are safely unpredictable.</p>
  <p>Of course, any cryptographically random text would have done for a ticket number, but GUIDs are pretty handy. :-)</p>
  <p>As with other version 4 GUIDs there's no absolute guarantee of uniqueness, but the odds are impressive. So long as you have fewer than 326,915,130,069,135,865 (i.e. <a href="http://www.wolframalpha.com/input/?i=sqrt(-2*2%5E122*ln(0.99))" rel="nofollow noreferrer">sqrt(-2<em>2^122</em>ln(0.99))</a>) GUIDs in play simultaneously, you can be more than 99% sure there are no collisions. Put another way: if like mine your application will have overflow errors all over the place if you have more than <code>int.MaxValue</code> of pretty much anything, you can be more than 99.9999999999999999% sure of no collisions (i.e. <a href="http://www.wolframalpha.com/input/?i=e%5E-(((2%5E31-1)%5E2)/(2*2%5E122))" rel="nofollow noreferrer">e^-(((2^31-1)^2)/(2*2^122))</a>). This is about a thousand times more sure than you can be that a meteorite won't wipe out most of life on Earth within one second of the application going live (i.e. <a href="http://en.wikipedia.org/wiki/Impact_event#Mass_extinctions_and_impacts" rel="nofollow noreferrer">one per 100 million years</a>).</p>