|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Hashing; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use RuntimeException; |
| 7 | +use Illuminate\Contracts\Hashing\Hasher as HasherContract; |
| 8 | + |
| 9 | +class Argon2Hasher implements HasherContract |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Hash the given value. |
| 13 | + * |
| 14 | + * @param string $value |
| 15 | + * @param array $options |
| 16 | + * @return string |
| 17 | + * |
| 18 | + * @throws \RuntimeException |
| 19 | + */ |
| 20 | + public function make($value, array $options = []): string |
| 21 | + { |
| 22 | + if (extension_loaded('sodium')) { |
| 23 | + return sodium_crypto_pwhash_str( |
| 24 | + $value, |
| 25 | + $options['time_cost'] ?? SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, |
| 26 | + $options['memory_cost'] ?? SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + throw new RuntimeException('Argon2i hashing not supported.'); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Check a plain text value against a hashed value. |
| 35 | + * |
| 36 | + * @param string $value |
| 37 | + * @param string $hashedValue |
| 38 | + * @param array $options |
| 39 | + * @return bool |
| 40 | + * |
| 41 | + * @throws \RuntimeException |
| 42 | + */ |
| 43 | + public function check($value, $hashedValue, array $options = []): bool |
| 44 | + { |
| 45 | + if (extension_loaded('sodium')) { |
| 46 | + $valid = sodium_crypto_pwhash_str_verify($hashedValue, $value); |
| 47 | + sodium_memzero($value); |
| 48 | + return $valid; |
| 49 | + } |
| 50 | + |
| 51 | + throw new RuntimeException('Argon2i hashing not supported.'); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Check if the given hash has been hashed using the given options. |
| 56 | + * |
| 57 | + * @param string $hashedValue |
| 58 | + * @param array $options |
| 59 | + * @return bool |
| 60 | + * |
| 61 | + * @throws \RuntimeException |
| 62 | + */ |
| 63 | + public function needsRehash($hashedValue, array $options = []): bool |
| 64 | + { |
| 65 | + // Extract options from the hashed value |
| 66 | + list($memoryCost, $timeCost) = sscanf($hashedValue, '$%*[argon2id]$v=%*ld$m=%d,t=%d'); |
| 67 | + $hashOptions = ['memory_cost' => $memoryCost, 'time_cost' => $timeCost]; |
| 68 | + |
| 69 | + if (empty(array_filter($hashOptions))) { |
| 70 | + throw new InvalidArgumentException('Supplied hash is not a valid Argon2 hash'); |
| 71 | + } |
| 72 | + |
| 73 | + // Filter unknown options from the options array |
| 74 | + $options = array_filter($options, function ($key) use ($hashOptions) { |
| 75 | + return isset($hashOptions[$key]); |
| 76 | + }, ARRAY_FILTER_USE_KEY); |
| 77 | + |
| 78 | + return ! empty(array_diff_assoc($options, $hashOptions)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Determine if the system supports Argon2i hashing. |
| 83 | + * |
| 84 | + * @return bool |
| 85 | + */ |
| 86 | + public function isSupported(): bool |
| 87 | + { |
| 88 | + return extension_loaded('sodium'); |
| 89 | + } |
| 90 | +} |
0 commit comments