DEV Community

Discussion on: Eloquent UUIDs.

Collapse
 
jenschude profile image
Jens

Please don't use mt_rand for generating UUIDs. As the PHP documentation notes this function does not generate cryptographically secure values and you will end up with a lot of collisions.

Better use random_bytes which is crypthographically secure. Example:

function uuidv4()
    {
        return implode('-', [
            bin2hex(random_bytes(4)),
            bin2hex(random_bytes(2)),
            bin2hex(chr((ord(random_bytes(1)) & 0x0F) | 0x40)) . bin2hex(random_bytes(1)),
            bin2hex(chr((ord(random_bytes(1)) & 0x3F) | 0x80)) . bin2hex(random_bytes(1)),
            bin2hex(random_bytes(6))
        ]);
    }

Kindly taken from paragonie.com/b/JvICXzh_jhLyt4y3

Besides that as noted in the different comments use some library which is able to generate the uuids in binary form and store them also as binary in the database.