0

I generate keys for my software as:

Guid.NewGuid().ToString();

that returns something like: 15c6bd70-8d3c-42d0-bb24-40da6e08ed9d

anyways everytime someone purchases a new software I generate a new key. can it be possible that I generate the same key twice? Everytime someone purchase the software I call the method: Guid.NewGuid().ToString() Should I append a counter at the end of each guid to be 100% sure that there cannot be duplicates?

Edit

A constructor of the Guid class takes a byte array of 16 bytes as a parameter. If you serialize the current date (8 bytes) then append another 8 random bytes to the constructor of the GUID will that be 100% secure? I am just asking for learning based on your answers I will probably just have Guid.NewGuid()

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • 7
    It's possible, but *very* unlikely. – David Sep 17 '13 at 19:42
  • Unless you're planning on selling quintillions of copies, you'll be fine. – Dmitry Brant Sep 17 '13 at 19:44
  • 1
    @DmitryBrant: "Quintillions" is close enough to "zero" in the calculation that even that many is statistically irrelevant. – David Sep 17 '13 at 19:45
  • @David I meant to say "zillions" – Dmitry Brant Sep 17 '13 at 19:46
  • 2
    Quintillions, zillions, bajillions... You guys are entering a semanthyc labyrinth. What matters is that the chances of two GUID's colliding are smaller than Valve ever releasing Half Life 3, or smaller than the odds of Opera becoming the most popular browser in the market. – Geeky Guy Sep 17 '13 at 19:48
  • `"If you serialize the current date (8 bytes) then append another 8 random bytes to the constructor of the GUID will that be 100% secure?"` - That will be less random than a GUID. Honestly, you're *probably* not going to develop a better GUID-generating algorithm than the standard that's already in place. There's a lot of proven math behind it. – David Sep 17 '13 at 19:56
  • If you add more random bytes then you reduce the probability of collision by some amount more. The probability is already low enough that it's effectively zero, but if you want something just a tad closer to zero, go ahead. – Servy Sep 17 '13 at 19:56
  • Please use search before asking duplicates. – tnw Sep 17 '13 at 19:59
  • What about the Scott Guid? http://weblogs.asp.net/leftslipper/archive/2010/04/01/last-guid-used-up-new-scottguid-unique-id-to-replace-it.aspx – Matthew Steeples Sep 17 '13 at 20:22

3 Answers3

4

An excerpt from one of the best blog series ever written about the Guid:

There are a number of possible strategies for making a unique GUID, and in fact information about the strategy used is encoded in the first four bits of the third "group"; almost every GUID you see will be of the form {xxxxxxxx-xxxx-1xxx-xxxx-xxxxxxxxxxxx} or {xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx}.

If there is a one in that place then the algorithm used to guarantee uniqueness is essentially a variation on the ISBN strategy. The GUID is guaranteed to be unique "in space" by choosing some of the bits as the MAC address of the network card in the machine. (The tricky problem of ensuring that no two network cards in the world have the same MAC address is solved somehow by someone else; how that problem is solved, we don't particularly care. The cost of solving that problem is passed on to you, the consumer, in the purchase cost of the network card.)

In short, it's very unlikely that they would ever collide.

Community
  • 1
  • 1
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • 3
    `And using the .NET implementation, you're leveraging the MAC address implementation` Not anymore, unless you're using a pretty old version. – Servy Sep 17 '13 at 19:47
  • @Servy, ah that's news to me. I'll drop that off. – Mike Perrenoud Sep 17 '13 at 19:48
  • MAC addresses are not actually unique. They are supposed to be, but in reality collisions do happen. – Guffa Sep 17 '13 at 20:23
  • @Guffa, yeah I think in the part I grabbed that excerpt from Eric had an update that stated there are in fact literal collisions even with MAC addresses, and further they can of course be spoofed. I would say the point, as you stated in your answer, is that it's just about impossible. – Mike Perrenoud Sep 17 '13 at 20:41
4

Yes, it's possible, but extremely unlikely. The probability for a GUID collision is about as likely as a bit in the GUID changing spontaneously in memory, and that kind of thing is not something that you normally worry about.

You can already be 100% sure, that is of course if you dont mean that you need to be 100.000000000000000000000000000000000000% sure.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • `"The probability for a GUID collision is about as likely as a bit in the GUID changing spontaneously in memory"` - I think I'm going to look up the statistics on that just so I can use it when explaining GUIDs to clients. I bet it's even less likely than the bit change, possibly by orders of magnitude, which makes for a great comparison. – David Sep 17 '13 at 19:53
  • +1 for actually counting out the proper number of zeros for the precision of your 100%. – Servy Sep 17 '13 at 19:55
0

Just use the Guid.. no need to append anything. Unless you expect to sell more copies than there are atoms in the universe (unlikely).

  • 8
    Please don't guess. Number of possible guids: 10^36 (http://en.wikipedia.org/wiki/Globally_unique_identifier). Number of atoms in the universe: 10^78 to 10^82 (http://www.universetoday.com/36302/atoms-in-the-universe/). – Bob Kaufman Sep 17 '13 at 19:48