1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus framework.
7: *
8: * (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Password;
15:
16: final readonly class Password
17: {
18: /**
19: * Creates a password hash driver.
20: *
21: * @param non-empty-string $algo
22: * @param array{
23: * cost?: int,
24: * iterations?: int,
25: * length?: int,
26: * opslimit?: int,
27: * memlimit?: int,
28: * memory_cost?: int,
29: * threads?: int,
30: * time_cost?: int,
31: * } $options
32: *
33: * @throws HashException
34: */
35: public static function fromAlgorithm(string $algo, array $options = []): HashInterface
36: {
37: $algorithm = Algorithm::tryFrom($algo);
38:
39: return match ($algorithm) {
40: Algorithm::Argon2i => new Hash\Argon2iHash($algorithm, $options),
41: Algorithm::Argon2id => new Hash\Argon2idHash($algorithm, $options),
42: Algorithm::Bcrypt => new Hash\BcryptHash($algorithm, $options),
43: Algorithm::Pbkdf2HmacSha1,
44: Algorithm::Pbkdf2HmacSha256,
45: Algorithm::Pbkdf2HmacSha512 => new Hash\Pbkdf2Hash($algorithm, $options),
46: Algorithm::Sodium => new Hash\SodiumHash($algorithm, $options),
47: default => throw new HashException(\sprintf('Unsupported "%s" algorithm.', $algo)),
48: };
49: }
50: }
51: