luisinder / blowfish-lib
Simple helper library for hashing and verifying passwords using Blowfish (bcrypt).
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.6
README
Simple helper library for hashing and verifying passwords using the Blowfish (bcrypt) algorithm in PHP.
โจ Features
- Secure password hashing via
password_hash()
(bcrypt) - Password verification helper
- Configurable cost factor
- Backwards-compatible method names (
crypt_blowfish
,checkPassword
) retained
๐ฆ Installation
Via Composer (recommended):
composer require luisinder/blowfish-lib
Or clone/download and include the file manually:
require_once __DIR__ . '/src/blowFish.php';
โ Requirements
- PHP >= 7.4
๐ Usage
require_once __DIR__ . '/vendor/autoload.php'; $bf = new BlowFish(12); // cost factor 12 $hash = $bf->hashPassword('My$ecretP@ss'); if ($bf->verifyPassword('My$ecretP@ss', $hash)) { echo "Password valid"; } else { echo "Invalid password"; }
Backwards Compatibility
Older code using:
$bf->crypt_blowfish($password); $bf->checkPassword($plain, $hash);
โฆwill still function, but you should migrate to:
$bf->hashPassword($password); $bf->verifyPassword($plain, $hash);
An internal class_alias
keeps the legacy lowercase blowFish
class name available.
Rehashing Strategy
You can detect when to upgrade stored hashes to a higher cost:
$bf = new BlowFish(10); $hash = $bf->hashPassword('secret'); // Later increase cost $bfStronger = new BlowFish(12); if ($bfStronger->needsRehash($hash)) { $hash = $bfStronger->hashPassword('secret'); }
โ๏ธ Cost Factor Guidance
Typical values: 10โ14. Higher = more secure but slower. Benchmark in your environment aiming for ~100ms or less per hash for interactive logins.
๐ Security Notes
- Do NOT store plain passwords.
- Always use
password_verify()
(wrapped byverifyPassword
). - Rotate the cost upwards over time; you can rehash existing hashes with
password_needs_rehash()
externally if desired (not included to keep this helper minimal).
๐ License
MIT. See LICENSE.
๐งพ Changelog
See CHANGELOG.md for notable changes.
๐ค Contributing
Issues and pull requests are welcome. Please include a clear description and if possible a test snippet to reproduce or validate the change.
๐งช Example Script
Run the included example after install:
composer run example
๐งช Tests
Install dev dependencies and run PHPUnit:
composer install
composer test
๐ Continuous Integration
GitHub Actions workflow runs the test suite on PHP 7.4โ8.3. See .github/workflows/ci.yml
.
Author
Luis Cajigas
Enjoy and stay secure!