How to generate cryptographically secure token in PHP

3 Answers

0 votes
$bytes = random_bytes(32); 

$token = bin2hex($bytes);

echo $token;




/*
run:

e71ceda3b339c142b4167093e9b30d27500f1c84d90f2a48a30c58aadf7315d5

*/

 



answered Dec 1, 2023 by avibootz
0 votes
$token = bin2hex(random_bytes(32));

echo $token;




/*
run:

c8caa25b83cbc08a717f94a0c480a9736db49b905d43a287580eaa95a4a3b05b

*/

 



answered Dec 1, 2023 by avibootz
0 votes
$bytes = openssl_random_pseudo_bytes(32);

$token = bin2hex($bytes);

echo $token;




/*
run:

34c7640680a6a0be586c15539ff564331adb94e154056cb20b67281b0b3b4b8c

*/

 



answered Dec 1, 2023 by avibootz
...