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