5 ms·
UUIDs and GUIDs are far too complicated, personally I don't like using them. There are multiple "versions" (really, generation algorithms) of UUID and GUID, eac
by TazeTSchnitzel 11y ago
UUIDs and GUIDs are far too complicated, personally I don't like using them. There are multiple "versions" (really, generation algorithms) of UUID and GUID, each with their own problems:
* Some types of UUID uniquely identify the machine they were generated on (one version contains the MAC address + current time, another contains the POSIX UID/GID + domain name) - this got Microsoft into hot water in the 1990s when Word added GUIDs to documents, which meant you could trace documents Stasi-style
* Some types of UUID are based on insecure hashing algorithms (MD5 and SHA1)
* Some types of UUID are namespaced, because everything needs namespaces, obviously
* There's a specially reserved type for Microsoft to use for special COM objects
There's only one mode you should actually use, which is the random bits.
UUIDs and GUIDs also have a weird spacing of dashes. You'd expect three dashes, splitting it into a sequence of 4-byte chunks, but no: it's split into 4-2-2-2-6, for some reason. And which chunk it is matters, because different chunks have different endianness. Some of them are considered numbers, some of them bytes, even though they all look the same. Some of them have special significance (it contains two different version numbers!), with no especially obvious rhyme or reason to their placement. Oh, and the endianness is implementation-defined: GUIDs are partly "native" endian (usually little-endian, then), partly big-endian, whereas UUIDs are typically big-endian. How do you tell them apart? Well, GUIDs are usually written in capitals, and UUIDs are usually written in lowercase.
I just use 16 random bytes encoded in hexadecimal, separated by three dashes at 4-byte increments. No hashing algorithms, versions, endianness issues, namespacing, severe privacy problems, just random bytes. It's not only simpler, it has more bits of entropy, and is easier to generate.
- JoeAltmaier 11y agoI also treat a UUID as a string of bytes. Go ahead and render it any way you like, but there are no endianness issues. The bytes are random and meaningless. Also be careful of what you think is 'random'. Calling the OS random-number-generator may not be random enough. I'd recommend in every case to use the system library to generate a UUID.
- TazeTSchnitzel 11y ago> I also treat a UUID as a string of bytes. Why not avoid UUID altogether? It is an overly complicated format. It is simpler to just generate 16 random bytes and represent them as you wish. Beware that 16 random bytes, at least currently, is not a valid UUID. You have to include version and variant numbers. > Go ahead and render it any way you like, but there are no endianness issues. The bytes are random and meaningless. Endianness absolutely matters. If you don't encode it and decode it correctly from the binary formats, you will end up with a different identifier. > Also be careful of what you think is 'random'. Calling the OS random-number-generator may not be random enough. If it's just a regular PRNG, sure. What you want is a CSPRNG, like /dev/urandom.
- JoeAltmaier 11y agoNo, endianness need not mean anything, when you are using uuids for your own purposes. Go ahead and identify a resource by this id. When it is returned to you, it will be as you sent it. No matter what (pointless) swapping and unswapping was done by the other participant. They will return the string the same as they got it (or they have a bug). To have endianness implies scalar subfields, which implies meaningful values in those fields. This goes back to when the fields had meaning (mac address, timestamp and so on). Nowadays they are random bytes and the breakdown into scalar parts has no useful purpose. Now, you can print UUIDs any way you like. And if you print them different places with different rendering libraries they can look different, sure. Don't confuse that with the underlying value, which is a random string.
- TazeTSchnitzel 11y ago> No, endianness need not mean anything, when you are using uuids for your own purposes. Go ahead and identify a resource by this id. When it is returned to you, it will be as you sent it. No matter what (pointless) swapping and unswapping was done by the other participant. They will return the string the same as they got it (or they have a bug). If you're passing around the string representation, then endianness isn't an issue, sure. But you need to care about it if you store UUIDs efficiently as bytes. > To have endianness implies scalar subfields, which implies meaningful values in those fields. This goes back to when the fields had meaning (mac address, timestamp and so on). Nowadays they are random bytes and the breakdown into scalar parts has no useful purpose. Even if you're only using random UUIDs, you need to worry about this, because UUIDs always contain a version and variant number. > Now, you can print UUIDs any way you like. And if you print them different places with different rendering libraries they can look different, sure. Don't confuse that with the underlying value, which is a random string. The string is only mostly random. It contains version information.
- lmm 11y agoThe great advantage of being standardized is that other people have thought about these issues and dealt with them in libraries. Yes there are many bad ways to generate them, but your library should offer the right way to generate them. Yes the string format is weird, but why would you write that by hand? Let the library print it, let the library parse it. Want to store it in a database? There's a column type for that. Want to send it over the network? There's probably a datatype for it in whatever protocol you're using (whereas with your approach you immediately have endianness issues).
- jovdg 11y agoI can't look-up the details right now, but we ran into some issues when we wanted to use system uuids (via dmidecode) to identify servers and pxeboot them. Apparently, HP server uuids are (subtly) different through dmidecode and via pxeboot... And it could be endianness actually - I did not check why it we different...
- jovdg 11y agoOK just found an experience from someone else: https://ocdnix.wordpress.com/2013/02/26/fuckin-uuids/ https://ocdnix.wordpress.com/2013/02/26/fuckin-uuids/
- TazeTSchnitzel 11y ago> The great advantage of being standardized is that other people have thought about these issues and dealt with them in libraries. Issues that don't exist if you don't use a hideously complex format like UUID in the first place. > Yes there are many bad ways to generate them, but your library should offer the right way to generate them. Yes, a UUID library will have a way to generate the random kind of UUID, but it's telling that you need a library for it. You can generate 16 random bytes without a library, by using fopen() and fread(). > Yes the string format is weird, but why would you write that by hand? Let the library print it, let the library parse it. You shouldn't need a library to handle your identifiers. If you do, your identifiers are too complicated. > Want to store it in a database? There's a column type for that. Want to send it over the network? There's probably a datatype for it in whatever protocol you're using (whereas with your approach you immediately have endianness issues). With the approach of random bytes, there's at least a consistent endianness throughout. And you can solve the endianness issue by treating it as a byte string. Job done.
- rikkus 11y ago> There's only one mode you should actually use, which is the random bits. Although if you were using GUIDs in indexes in SQL Server, it made sense for them to have some similarity and allow ordering [1] Last time I benchmarked this, however, I couldn't detect any difference. [1] http://blogs.msdn.com/b/sqlserverfaq/archive/2010/05/27/guid-vs-int-debate.aspx http://blogs.msdn.com/b/sqlserverfaq/archive/2010/05/27/guid...
- codeulike 11y agoUnless everyone starts with the same seed.
- je42 11y agoActually, i started to value UUID 5. Really neat properties, to generate from a name and a UUID a new one.
- colanderman 11y agoYes, the UUID 5 (SHA1 + namespace) is fantastically useful because it is deterministic. They can be a function of your own internal IDs, rather than trying to synchronize clocks or random seeds across systems, and still won't conflict with UUIDs from other vendors using a similar system. MD5 and SHA-1 being broken only matter if you let users choose your keys, to which the answer is don't do that.
- TazeTSchnitzel 11y agoDeterminism is a good point, that would be useful in some applications.