← PHP / Laravel · Contents

PHP-023 / Beginner

Generating tokens or secrets with rand()/mt_rand()

A02:2021 Cryptographic Failures · CWE-338 Use of Cryptographically Weak PRNG

Why is this a problem?

Predicting seeds and output patterns enables guessing reset tokens and similar values.

How to fix it

php
$token = bin2hex(random_bytes(32));   // Or Str::random(64)

Further reading