hautelook/phpass
Modernized, namespaced Composer-ready fork of Openwall Phpass (0.3) with minimal stylistic changes and unit tests. Provides PasswordHash to generate and verify password hashes for legacy systems; public domain source.
Installation:
Add to composer.json:
"require": {
"bordoni/phpass": "^0.3.6"
}
Run composer install or composer update.
Autoloading:
Ensure vendor/autoload.php is included (Laravel handles this via composer.json autoloading).
First Use Case: Hash and verify a password in a Laravel controller or service:
use Hautelook\Phpass\PasswordHash;
$hasher = new PasswordHash(8, false); // 8 = cost factor (1-31), false = portable hashes
$hash = $hasher->HashPassword('user_input_password');
Password Hashing:
User model registration or password reset logic:
public function setPasswordAttribute($password) {
$this->attributes['password'] = app(PasswordHash::class)->HashPassword($password);
}
users.password column).Password Verification:
AuthenticatesUsers trait):
public function validateCredentials($request) {
$hasher = app(PasswordHash::class);
return $hasher->CheckPassword(
$request->password,
$this->password
);
}
Service Container Binding (Laravel):
Bind the hasher in AppServiceProvider for dependency injection:
public function register() {
$this->app->singleton(PasswordHash::class, function () {
return new PasswordHash(config('hash.cost'), config('hash.portable'));
});
}
Configuration:
Define cost factor and portability in config/hash.php:
return [
'cost' => 10, // Higher = more secure but slower
'portable' => false, // Set true for cross-language compatibility
];
Migrations:
Update users table to store hashes (e.g., password column as varchar(255)).
Testing:
Mock PasswordHash in unit tests:
$hasher = $this->createMock(PasswordHash::class);
$hasher->method('CheckPassword')->willReturn(true);
Cost Factor:
Hash::needsRehash() pattern).Portability:
portable: true only if hashes must work outside PHP (e.g., Python). Reduces security slightly.PHP 8.1+:
^0.3.6 to avoid intval deprecation warnings (fixed in #5).Database Storage:
varchar(255) to avoid truncation.Hash Mismatches: Verify the exact hash string (whitespace/case-sensitive) when comparing.
// Debug helper
dd($hasher->CheckPassword('input', $storedHash)); // Returns bool
Performance:
High cost factors slow down hashing. Benchmark with php -r 'hash("sha256", str_repeat("a", 60));' for reference.
Custom Hashing Logic:
Extend PasswordHash to add pre/post-processing:
class CustomPasswordHash extends PasswordHash {
public function HashPassword($password) {
$password = strtolower($password); // Force lowercase
return parent::HashPassword($password);
}
}
Laravel Hash Facade:
Create a facade for consistency with Laravel’s Hash facade:
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Facade;
Facade::register('Phpass', function () {
return app(PasswordHash::class);
});
Usage:
$hash = Phpass::HashPassword('secret');
Password Policies:
Combine with Laravel’s Password validator for complexity rules:
$request->validate([
'password' => 'required|min:8|confirmed|string',
]);
How can I help you explore Laravel packages today?