best practice to generate random token for forgot password

best practice to generate random token for forgot password

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Generating Secure Reset Tokens: Moving Beyond Timestamps

When implementing a "forgot password" flow, generating a unique identifier for the reset token is critical. This token acts as a one-time credential that allows a user to recover access. The specific question often arises regarding whether combining a Unix timestamp with a random number generator like mt_rand() is sufficient. While this approach might seem clever, relying on it introduces significant security and reliability risks that can compromise the entire authentication system.

The primary flaw in using a timestamp as part of the token logic is predictability. An attacker who knows your algorithm can easily guess the time component. If an application generates a token based on time() plus a random string, an adversary only needs to know the approximate time window and the length of the random suffix to brute-force potential tokens. This transforms a security mechanism into a guessing game. Furthermore, timestamps are not inherently unique across different servers or high-concurrency environments without careful synchronization. In distributed systems, relying on wall-clock time can lead to collisions if two requests arrive within the same second, causing race conditions that might overwrite valid sessions or create duplicate links sent to users.

For custom-length tokens where uniqueness and entropy are paramount, cryptographic functions are the industry standard. The most robust approach involves using a cryptographically secure pseudo-random number generator (CSPRNG) to produce a string of random characters. In PHP, this means utilizing random_bytes() rather than rand() or mt_rand(). The random_bytes() function leverages the operating system's entropy pool, making it statistically impossible for an attacker to predict the output even if they know the length of the token.

Here is a practical implementation pattern using PHP 7.0+ that ensures high entropy:

function generateResetToken(int $length = 64): string {
    // Ensure we request enough bytes to cover the desired length after encoding
    $bytesLength = ($length / 4) + 1; 
    
    return bin2hex(random_bytes($bytesLength));
}

This method generates a hexadecimal string. A 64-character hex string (32 bytes) provides approximately 128 bits of entropy, which is considered secure for session tokens and password resets. You can easily adapt this to generate Base64 strings if you need a specific character set or shorter length, though Base64 introduces non-alphanumeric characters that might be annoying in URL paths.

When storing these tokens, the database column should be treated as a string, not an integer or UUID, unless your schema strictly requires it. The storage mechanism itself doesn't change much, but the retrieval logic must handle expiration rigorously. A token is useless if it isn't time-bound or usage-bound. You should store the created_at timestamp alongside the token hash and enforce a strict TTL (Time To Live). For example, you might set tokens to expire after 60 minutes from creation.

One common pitfall involves database indexing. If you plan to look up tokens by their value, ensuring the column is indexed prevents slow queries during the high-volume login attempts typical of brute-force attacks on reset links. However, consider the trade-off: if the token is stored in plain text (even hashed), an attacker gaining read access to your database can still attempt offline verification. Ideally, you should hash the generated token before storing it using a salted hash function like bcrypt or Argon2id. This adds a layer of obscurity; even if the database is leaked, the attacker doesn't have the raw token to guess against.

If you are working within the Laravel ecosystem, utilizing the built-in Str::random() helper method simplifies this process significantly. It handles the CSPRNG logic internally and returns a string of random characters based on a configurable length. This approach aligns with Laravel's security best practices and removes the burden of implementing raw cryptographic functions manually.

Ultimately, the goal is to create a token that is unpredictable, unique, and short-lived. Avoiding timestamp-based generation eliminates predictability vectors, while using random_bytes() or framework helpers ensures you have sufficient entropy to withstand modern brute-force attacks.

Our network