Bcrypt vs Hash in Laravel
When designing a secure authentication token or one-time link system, the choice between a cryptographic hash and an encrypted value often comes down to a misunderstanding of what each primitive actually guarantees. The core distinction lies in reversibility and timing requirements.
In your proposed scenarios, Option 1 relies on a deterministic hash function like SHA-256, while Option 2 uses Laravel's Crypt facade (which defaults to AES) for symmetric encryption. To determine which is safer, we must look at how they handle the "secret" and whether timing attacks or brute-force scenarios apply.
The Trap of Deterministic Hashing
Your first approach generates a key by hashing a timestamp combined with a static salt: hash('sha256', date('Y-m-d').$salt). This method is fundamentally flawed for security-sensitive contexts because it is deterministic. If an attacker intercepts the token and knows the algorithm, they can simply iterate through dates to find a match.
The issue isn't just that the salt is hardcoded; it's that SHA-256 is computationally cheap. An attacker could test millions of date combinations per second on their local machine without any significant delay. In Laravel, you might be tempted to use Hash::make(), but even that relies on a random salt stored in the database. If your goal is to verify a token without storing state or if the "salt" is predictable (like a fixed string), you are effectively building an MD5-style rainbow table lookup.
The primary pitfall here is the lack of computational cost. Security protocols often rely on functions that deliberately slow down brute-force attempts. SHA-256 provides no resistance to this; it returns a result in microseconds. If your system expects a valid token only once per day, an attacker could easily replay old tokens or generate new ones if they can guess the date and salt combination.
The Reality of Encryption vs. Hashing
Your second option uses Crypt::encrypt(). This is symmetric encryption, not hashing. You are encrypting the date string with a default key derived from the application's APP_KEY environment variable.
This approach introduces a critical dependency: key availability. In your code snippet, you attempt to decrypt the token on Page 2 using the same logic used on Page 1. This implies that both pages share the exact same APP_KEY. If this is an internal system where both endpoints are controlled by you and share the same environment configuration, encryption works as intended. It provides confidentiality and integrity; an attacker cannot simply guess a date that decrypts to the expected string without knowing the key.
However, there is a subtle flaw in your implementation logic regarding the decryption step:
$pageOneKey = Crypt::decrypt($key);
if ($key == $pageOneKey) { // This comparison is logically incorrect
// Execute some code
}
You are comparing the encrypted ciphertext ($key) against the decrypted plaintext ($pageOneKey). They will never be equal. The correct check compares the original input against the decrypted value:
if (Crypt::decrypt($key) == $expectedDate) {
// Execute some code
}
Why Bcrypt is Not the Right Tool Here
You asked about using bcrypt. It is crucial to clarify that bcrypt is for password hashing, not token verification. Bcrypt is designed to be slow and resource-intensive specifically to prevent brute-force attacks against passwords stored in a database.
Using bcrypt to generate or verify time-based tokens is generally an anti-pattern unless you are implementing a specific protocol like PBKDF2 where the output must be derived from a user secret. If you use Hash::make() (which uses bcrypt) on a timestamp:
$key = Hash::make(date('Y-m-d'));
The resulting hash includes a random salt. When verifying this token, you cannot simply compare two strings; you must store the hash in a database or retrieve it from the original generation point to verify against. You cannot regenerate the exact same bcrypt hash for verification without storing the salt associated with that specific token.
If your goal is a "password" required to access a link (as implied by your title), you are mixing concepts. A standard URL parameter cannot securely hold a bcrypt password because the salt is not transmitted. To use bcrypt for access control, you must store the hash in the database and verify it there, which defeats the purpose of a stateless token unless you embed the salt in the token itself (which makes the token massive and leaks information about the cost factor).
The Verdict
For your specific use case of validating a link:
- If you control both endpoints and they share the same
APP_KEY: Use Encryption (Crypt::encrypt). It is reversible, secure against guessing, and handles the date logic naturally. Just ensure you compare ciphertext to ciphertext, or decrypt then compare plaintexts correctly. - If you need high entropy without a shared key: Use a HMAC. Instead of hashing with SHA-256, use Laravel's
Hash::get()combined with a secret stored in your application config (likeAPP_KEYor a dedicatedTOKEN_SECRET). This ensures that even if the date is known, the attacker cannot generate a valid token without the secret key.// Generate $token = Hash::get(date('Y-m-d'), 'secret-key'); // Verify if (Hash::get($inputDate, 'secret-key') === $receivedToken) { ... } - Avoid Bcrypt: Do not use bcrypt for this unless you are storing the hash in a database table where the salt is persisted alongside the token.
The most secure and standard approach for time-based links in modern Laravel applications is HMAC, as it provides authentication (proof of origin) without requiring shared symmetric keys for every single request, or relying on deterministic hashes that can be brute-forced easily.