Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractHasher
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 make
n/a
0 / 0
n/a
0 / 0
0
 requiresRehash
n/a
0 / 0
n/a
0 / 0
0
 setOptions
n/a
0 / 0
n/a
0 / 0
0
 create
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 createHash
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAlgorithms
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 needsRehash
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 verify
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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 abstract hasher
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 */
26abstract class AbstractHasher
27{
28
29    /**
30     * Make hashed value (based on the hasher class)
31     *
32     * @param  string $value
33     * @return string
34     */
35    abstract public function make(#[\SensitiveParameter] string $value): string;
36
37    /**
38     * Determine if the hashed value requires re-hashing (based on the hasher class)
39     *
40     * @param  string $hashedValue
41     * @return bool
42     */
43    abstract public function requiresRehash(string $hashedValue): bool;
44
45    /**
46     * Set hasher options
47     *
48     * @param  array $options
49     * @return static
50     */
51    abstract public function setOptions(array $options): static;
52
53    /**
54     * Create hasher object with options
55     *
56     * @return static
57     */
58    public static function create(array $options = []): static
59    {
60        $hasher = new static();
61        $hasher->setOptions($options);
62        return $hasher;
63    }
64
65    /**
66     * Create hashed value
67     *
68     * @param  string          $value
69     * @param  string|int|null $algorithm
70     * @param  array           $options
71     * @return string
72     */
73    public function createHash(#[\SensitiveParameter] string $value, string|int|null $algorithm, array $options = []): string
74    {
75        return password_hash($value, $algorithm, $options);
76    }
77
78    /**
79     * Get info from hashed value
80     *
81     * @param  string $hashedValue
82     * @return array
83     */
84    public function getInfo(string $hashedValue): array
85    {
86        return password_get_info($hashedValue);
87    }
88
89    /**
90     * Get available hashing algorithms
91     *
92     * @return array
93     */
94    public function getAlgorithms(): array
95    {
96        return password_algos();
97    }
98
99    /**
100     * Determine if the hashed value needs to be re-hashed
101     *
102     * @param  string          $hashedValue
103     * @param  string|int|null $algorithm
104     * @param  array           $options
105     * @return bool
106     */
107    public function needsRehash(string $hashedValue, string|int|null $algorithm, array $options = []): bool
108    {
109        return password_needs_rehash($hashedValue, $algorithm, $options);
110    }
111
112    /**
113     * Verify hash
114     *
115     * @param  string $value
116     * @param  string $hashedValue
117     * @return bool
118     */
119    public function verify(#[\SensitiveParameter] string $value, string $hashedValue): bool
120    {
121        return password_verify($value, $hashedValue);
122    }
123
124}