Secure hash and salt for PHP passwords
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Password hashing is a crucial security practice for protecting user credentials. In the context of web development, particularly with PHP, it is essential to understand how to properly hash passwords and why adding a salt increases security. This article provides a deep dive into secure hashing and salting for PHP passwords.
Understanding Hashing
A hash function is a way to take an input (or "message") and return a fixed-size string of bytes. The output, usually looking like a random string of characters, is typically a "digest" that uniquely corresponds to the input. Crucial properties of a cryptographic hash function include:
- Determinism: the same input always results in the same output.
- Infeasibility to generate the original input by knowing the hash value.
- Sensitivity to changes: even a small change in input significantly changes the output.
In PHP, the most common way to hash passwords is by using the password_hash() function, which simplifies secure hash generation using a strong one-way hashing algorithm.
Why Salt Is Necessary
A "salt" is a random value added to the input of a hash function to ensure unique outputs for different inputs. In the context of password storage, salts prevent attackers from using precomputed dictionaries (like rainbow tables) to crack passwords. By salting passwords, you ensure that even if two users have the same password, their stored password hashes will be different.
PHP's password_hash() and password_verify() Functions
PHP provides two essential functions for password security:
password_hash(): This function generates a password hash using a strong one-way hashing algorithm. It automatically handles salting and can be configured to use different hashing algorithms.password_verify(): This function verifies a password against a hash. It is crucial for checking user login attempts where the plain text password entered by the user needs to be verified against the stored hash.
Here is a simple example of using these functions:
Best Practices for Password Hashing in PHP
While password_hash() and password_verify() offer robust tools for password management, it's essential to follow best practices:
- Always use the
PASSWORD_DEFAULTalgorithm inpassword_hash(), which will automatically use the currently considered strong algorithm. (As of PHP 7.4, this is Bcrypt). - Do not create your own salts. The
password_hash()function handles this for you, providing a cryptographically secure salt. - Store the full hash string returned by
password_hash()in your database, as it contains information about the algorithm used, the salt, and the hash itself. - Regularly update your hashing algorithm as newer, more secure options are developed. PHP’s
PASSWORD_DEFAULThelps in this by always adapting to the best current algorithm.
Comparison of Hashing Algorithms
PHP supports a variety of hashing algorithms. Here's a comparison of the most commonly used ones:
| Algorithm | Recommended | Notes |
| Bcrypt | Yes | Strong and adaptive; resists brute-force. |
| Argon2 | Yes | Winner of the Password Hashing Competition, available since PHP 7.2. |
| MD5 | No | Fast but not secure, susceptible to brute-force attacks. |
| SHA-1 | No | Faster but not recommended for secure hashing due to vulnerabilities. |
Conclusion
Implementing secure password hashing using PHP’s password_hash() and password_verify() functions are straightforward and highly effective. These functions handle the complexities of secure hashing, including the generation of cryptographic salts, making it easier to implement secure authentication mechanisms in your applications. Always stay updated with the latest security practices and PHP versions to leverage enhancements in password hashing technology.
Related reading
- Securing access to REST API of Kafka Connect
- Securing data at rest in Kafka
- Securing REST API using custom tokens stateless, no UI, no cookies, no basic authentication, no OAuth, no login page
- Securing Spring Boot API with API key and secret
- security / codesign in Sierra Keychain ignores access control settings and UI-prompts for permission
- Security Group and Subnet Belongs to different networks
- Security Yaml Bomb user can restart kube-api by sending configmap
- securityContext.privileged Forbidden disallowed by cluster policy

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.