Installation:
composer require mikemclin/laravel-wp-password
Add the service provider to config/app.php:
MikeMcLin\WpPassword\WpPasswordProvider::class
First Use Case: Hash a password for WordPress compatibility:
use MikeMcLin\WpPassword\Facades\WpPassword;
$hashed = WpPassword::make('user_input_password');
Where to Look First:
MikeMcLin\WpPassword\Facades\WpPassword (for quick usage).MikeMcLin\WpPassword\WpPassword (for direct instantiation)./tests/ for edge cases and validation logic.Password Hashing:
// Basic hashing
$hash = WpPassword::make('plain_password');
// With custom cost (default: 8)
$hash = WpPassword::make('plain_password', 10);
Password Verification:
// Check if plain password matches hashed
if (WpPassword::check('user_input', $stored_hash)) {
// Valid
}
Integration with Laravel Auth:
App\Providers\AuthServiceProvider for custom password validation:
public function boot()
{
Hash::extend('wp', function($app) {
return new MikeMcLin\WpPassword\WpPassword();
});
}
config/auth.php:
'defaults' => [
'password' => 'wp',
],
Migrating Existing Data:
// Update user passwords in a migration
DB::table('users')->update([
'password' => WpPassword::make('new_password')
]);
API Responses:
// Return hashed password in API responses (e.g., user registration)
return response()->json([
'status' => 'success',
'password_hash' => WpPassword::make($request->password)
]);
Custom Cost Logic: Dynamically set hash cost based on user role or security settings:
$cost = auth()->user()->isAdmin() ? 12 : 8;
$hash = WpPassword::make($password, $cost);
Batch Processing: Use in queue jobs for bulk password updates:
class UpdatePasswordsJob implements ShouldQueue
{
public function handle()
{
User::chunk(100, function ($users) {
foreach ($users as $user) {
$user->password = WpPassword::make('new_password');
$user->save();
}
});
}
}
Legacy System Integration: Sync with external systems using WordPress hashes:
$externalHash = $legacySystem->getHash();
if (WpPassword::check('user_input', $externalHash)) {
// Proceed with legacy auth
}
Cost Parameter:
8. Higher values increase security but slow hashing.Facade vs. Class:
WpPassword::make()) is convenient but less flexible than the class:
// Facade (simpler)
$hash = WpPassword::make('password');
// Class (more control)
$wpPassword = new MikeMcLin\WpPassword\WpPassword();
$hash = $wpPassword->make('password', 10);
Thread Safety:
Laravel Hashing Conflicts:
Hash facade, avoid naming collisions:
// Bad: Ambiguous if both packages are loaded
$hash = Hash::make('password');
// Good: Explicitly use WP package
$hash = WpPassword::make('password');
Deprecated Methods:
wp_hash_password() and wp_check_password() signatures. Check the WordPress Codex for updates.Verify Hashes:
Use WordPress’s wp_check_password() in PHP CLI to validate hashes:
require 'vendor/autoload.php';
use MikeMcLin\WpPassword\Facades\WpPassword;
$hash = WpPassword::make('test');
var_dump(WpPassword::check('test', $hash)); // bool(true)
Cost Validation:
Test edge cases for cost values (e.g., 0 or >12):
try {
WpPassword::make('password', 0); // May throw or behave unexpectedly
} catch (\Exception $e) {
// Handle
}
Performance Profiling: Benchmark hashing with different costs:
$start = microtime(true);
WpPassword::make('long_password', 12);
$time = microtime(true) - $start;
Custom Hash Algorithms: Extend the package to support additional algorithms (e.g., Argon2):
class CustomWpPassword extends MikeMcLin\WpPassword\WpPassword
{
public function make($password, $cost = 8)
{
// Custom logic here
return parent::make($password, $cost);
}
}
Event Listeners: Trigger events for password hashing/verification:
// In EventServiceProvider
public function boot()
{
WpPassword::addListener('hashing', function ($hash) {
Log::info("Password hashed: " . substr($hash, 0, 10) . "...");
});
}
Configuration:
Override defaults in config/wp-password.php (if added by the package):
'default_cost' => 10,
'algorithm' => 'wp_standard',
Testing: Mock the package in PHPUnit:
$this->partialMock(MikeMcLin\WpPassword\WpPassword::class, ['make'])
->shouldReceive('make')
->with('password', 8)
->andReturn('$hashed');
How can I help you explore Laravel packages today?