| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Support;
- use Illuminate\Contracts\Auth\Authenticatable;
- use Illuminate\Support\Facades\Hash;
- /**
- * Magento 1.x password hashes: md5(password) or md5(salt + password).':'.salt
- */
- class Magento1Password
- {
- public static function verify(string $plain, string $hash): bool
- {
- if ($plain === '' || $hash === '') {
- return false;
- }
- $parts = explode(':', $hash);
- $computed = match (count($parts)) {
- 1 => md5($plain),
- 2 => md5($parts[1].$plain),
- default => null,
- };
- if ($computed === null) {
- return false;
- }
- return hash_equals($parts[0], $computed);
- }
- public static function upgradeToBcrypt(Authenticatable $customer, string $plain): void
- {
- $customer->password = Hash::make($plain);
- $customer->legacy_password = null;
- $customer->save();
- }
- public static function attempt(Authenticatable $customer, string $plain): bool
- {
- $stored = (string) $customer->getAuthPassword();
- try {
- if ($stored !== '' && Hash::check($plain, $stored)) {
- return true;
- }
- } catch (\Throwable) {
- // Magento hashes are not bcrypt; continue with the legacy verifier.
- }
- $legacy = (string) ($customer->legacy_password ?? '');
- if ($legacy !== '' && self::verify($plain, $legacy)) {
- self::upgradeToBcrypt($customer, $plain);
- return true;
- }
- return false;
- }
- }
|