In order to generate a 32 character token for access to our API we currently use:
$token = md5(uniqid(mt_rand(), true));
I have read that this method is not cryptographically secure as it's based on the system clock, and that openssl_random_pseudo_bytes would be a better solution as it would be harder to predict.
If this is the case, what would the equivalent code look like?
I presume something like this, but I don't know if this is right...
$token = md5(openssl_random_pseudo_bytes(32));
Also what length makes sense that I should pass to the function?
md5()generates a 32 character string, but has only 128bits worth of data in it.openssl_random_pseudo_bytes()returns true binary data, so has 32*8 = 256 bits of randomness. By stuffing your 32-byte random string through md5, you're effectively cutting its uniqueness by a massive amount. - Marc B$token = bin2hex(openssl_random_pseudo_bytes(16));sufficient or do I need a loop of 16 iterations passing 1 as the length and appending in hex to a string? - fireRandom::alphanumericString($length), which fits about 2 billion times as much “entropy” into those 16 characters. - caw