Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Hasher
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
1 / 1
 create
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Crypt\Hashing;
16
17/**
18 * Pop Crypt hasher factory
19 *
20 * @category   Pop
21 * @package    Pop\Crypt
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    4.0.0
26 */
27class Hasher
28{
29
30    /**
31     * Create hasher object with options
32     *
33     * @param  mixed $algorithm
34     * @throws \InvalidArgumentException
35     * @return AbstractHasher
36     */
37    public static function create(mixed $algorithm, array $options = []): AbstractHasher
38    {
39        $hasher = match ($algorithm) {
40            PASSWORD_BCRYPT   => new BcryptHasher(),
41            PASSWORD_ARGON2I  => new Argon2IHasher(),
42            PASSWORD_ARGON2ID => new Argon2IdHasher(),
43            default           => throw new Exception('Error: Invalid hashing algorithm.'),
44        };
45
46        if (!empty($options)) {
47            $hasher->setOptions($options);
48        }
49
50        return $hasher;
51    }
52}