81

I have this code:

String uuid = UUID.randomUUID().toString().replace("-", "");

How safe is it to remove the "-" in the generated UUID? Would removing it defeat the purpose of it being globally unique and make the generated UUID prone to collisions?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
quarks
  • 33,478
  • 73
  • 290
  • 513
  • 66
    The UUID is a 128-bit number. The hyphens are only part of the display format for human consumption, they are not part of the UUID itself. – Jim Garrison Aug 13 '18 at 21:32
  • 9
    It is safe, since the `-` is always in the same positions. See [UUID Format](https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) on Wikipedia. – Andreas Aug 13 '18 at 21:32
  • 4
    https://stackoverflow.com/questions/1155008/how-unique-is-uuid – shmosel Aug 13 '18 at 21:32
  • 3
    @xybrek WHY do you want to remove the hyphens? That seems like a pretty odd request. – Marie Aug 14 '18 at 19:52
  • 3
    Also, I downvoted since this question lacks basic research. A quick look at generating a few would have rapidly shown they're always in the same spot, and a look into what UUIDs are and how they're formatted would have revealed this equally quickly. Please [do more researching on your own](https://meta.stackoverflow.com/a/261593/1394393) and clarifying your understanding of the technologies you're working with before posting a question. – jpmc26 Aug 14 '18 at 22:45
  • @Marie I've removed the hyphens from UUIDs before because they look *vastly* better in a URL without them. – jpmc26 Aug 15 '18 at 00:44

6 Answers6

129

how safe if is to remove the "-" in the generated UUID

It's 100% safe since the dashes aren't part of the value. The String UUID is a hex representation of a 128 bit value. The dashes are there just for display purposes so UUIDs will be a bit easier on the eyes.

Just be careful when passing UUIDs in String form to external systems such as external APIs, databases, and things of that nature. They might be expecting the dashes to be there.

Malt
  • 28,965
  • 9
  • 65
  • 105
  • I've removed the separate question about uniqueness, as multiple questions in a single post are forbidden. You may wish to update your answer. – jpmc26 Aug 14 '18 at 22:39
53

Let’s say I want to call the White House. Their phone number is (202) 456-1111. If I delete all the dashes and parentheses from that phone number, I’m left with 2024561111. I didn’t lose any information in the course of doing this - I just changed the formatting in a way that makes it harder to read. If I punch this number into my phone, it’ll still make the call properly because the phone system still knows that the first three digits are the area code and the next seven are the main number.

In the same way, the dashes in a UUID are like the extra punctuation in a phone number - they’re included so that it’s easier for a human to read some underlying large number. In UUIDs, that number is 128 bits long and is written in hexadecimal, so unlike a phone number it’s less “obviously” a number, but the basic principle is the same. Deleting the dashes won’t change the number and thus won’t impact security.

Now, what might happen is that doing so breaks formatting compatibility across platforms. Let’s go back to the phone number analogy. Some websites I’ve used won’t let me type in 2024561111 as a phone number. They’ll insist that I put in spaces, dashes, and parentheses, as in (202) 456-1111. (I’m not a fan of sites like that, but that’s another story.) So removing the dashes from your UUID could potentially be an issue if you need to pass a string representation of the UUID into some other process or service that’s expecting the full formatting, including the commas.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
12

The dashes in a properly formed UUID are not randomly placed in the string - it's a specific format detailed in the RFCs - http://www.ietf.org/rfc/rfc4122.txt

So, removing the dashes won't affect the uniqueness of the UUID.

However, it may cause issues with libraries that expect the dashes as part of a UUID to validate it as a UUID.

Why do you want to remove them?

HorusKol
  • 8,375
  • 10
  • 51
  • 92
9

You can check how the string is created by reading the javadoc:

UUID                   = <time_low> "-" <time_mid> "-"
                      <time_high_and_version> "-"
                      <variant_and_sequence> "-"
                      <node>
time_low               = 4*<hexOctet>
time_mid               = 2*<hexOctet>
time_high_and_version  = 2*<hexOctet>
variant_and_sequence   = 2*<hexOctet>
node                   = 6*<hexOctet>

So removing the - is fine, you can reinsert them at the correct position later on if you want, or recreate a UUID object containing the same information.

Regarding uniqueness: How unique is UUID?

assylias
  • 321,522
  • 82
  • 660
  • 783
8

The UUID is a 128-bit number.

The format in hexadecimal with hyphens is only a display rendering for human consumption. It is one of several possible display renderings, and the display format, with or without hyphens, is NOT the UUID itself.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
1

The UUID is a 128-bit number. The four bits of digit M indicate the UUID version, and the one to three most significant bits of digit N indicate the UUID variant. The binary encoding of UUIDs varies between systems. Many systems encode the UUID entirely in a big-endian format.

Stefano
  • 209
  • 2
  • 10
  • 34