I'm wondering if I can safely replace ids generated with mysql's UUID() function with ids generated by java's UUID.randomUUID() function?
I'm looking to share these ids in urls with my users, but the mysql UUID() output looks very similar to each other after a few runs:
3ae2c9c4-47df-11e1-8c2a-a46b34c02a9e
976de634-47e3-11e1-8c2a-a46b34c02a9e
530cc5c6-47e7-11e1-8c2a-a46b34c02a9e
...
I'm not saying they're not unique, but is this how the results of UUID() usually appear? I suppose it's tied to some identifier on my machine.
Java's UUID.randomUUID() "look" more random:
c042437b-298a-41c4-c2b6-0f83552bdb8b
e33d8ab7-d9d3-4ffe-a592-650a125d2a93
ecb12c54-5741-45c8-8b85-1825c19a9cae
...
From my naive understanding of uniqueness, the ids generated by the java method should work as a perfect replacement for mysql's UUID()?
(another bonus for using java's version is that I won't have to re-fetch an inserted record which uses UUID() when I need to provide it in a reply to a client)
Thank you
The randomUUID() method is used to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.
UUID() function in MySQL It is designed as a number that is universally unique. Two UUID values are expected to be distinct, even they are generated on two independent servers. In MySQL, a UUID value is a 128-bit number represented as a utf8 string, and the format in hexadecimal number will be as follows.
A UUID is 36 characters long unique number. It is also known as a Globally Unique Identifier (GUID). A UUID is a class that represents an immutable Universally Unique Identifier (UUID). A UUID represents a 128-bit long value that is unique to all practical purpose.
As it happens, Java's UUID. randomUUID() method does use a cryptographic RNG, so it should be safe in that regard. But the risk then is that developers who later look at your code may not understand that your intent includes secure cryptographic random choice, and not just uniqueness.
Are you using linux or FreeBSD? From mysql's documentation:
The fifth number is an IEEE 802 node number that provides spatial uniqueness. A random number is substituted if the latter is not available (for example, because the host computer has no Ethernet card, or we do not know how to find the hardware address of an interface on your operating system). In this case, spatial uniqueness cannot be guaranteed. Nevertheless, a collision should have very low probability.
Currently, the MAC address of an interface is taken into account only on FreeBSD and Linux. On other operating systems, MySQL uses a randomly generated 48-bit number.
Also note:
Warning:
Although UUID() values are intended to be unique, they are not necessarily unguessable or unpredictable. If unpredictability is required, UUID values should be generated some other way.
EDIT
If you want user ids to appear more random, you can pass them through a hashing function like MD5(UUID())
, which gives a more convincing random string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With