Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractArgon2Hasher
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
11 / 11
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setMemoryCost
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMemoryCost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasMemoryCost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTimeCost
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTimeCost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasTimeCost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setThreads
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getThreads
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasThreads
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOptions
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
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 Abstract Argon2 class
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 */
27abstract class AbstractArgon2Hasher extends AbstractHasher
28{
29
30    /**
31     * Memory cost
32     * @var int
33     */
34    protected int $memoryCost = PASSWORD_ARGON2_DEFAULT_MEMORY_COST; // 65536
35
36    /**
37     * Time cost
38     * @var int
39     */
40    protected int $timeCost = PASSWORD_ARGON2_DEFAULT_TIME_COST; // 4
41
42    /**
43     * Threads
44     * @var int
45     */
46    protected int $threads = PASSWORD_ARGON2_DEFAULT_THREADS; // 1
47
48    /**
49     * Constructor
50     *
51     * Instantiate the Argon2 object
52     *
53     * @param int $memoryCost
54     * @param int $timeCost
55     * @param int $threads
56     */
57    public function __construct(
58        int $memoryCost = PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
59        int $timeCost = PASSWORD_ARGON2_DEFAULT_TIME_COST,
60        int $threads = PASSWORD_ARGON2_DEFAULT_THREADS
61    )
62    {
63        $this->setMemoryCost($memoryCost);
64        $this->setTimeCost($timeCost);
65        $this->setThreads($threads);
66    }
67
68    /**
69     * Set memory cost
70     *
71     * @param  int $memoryCost
72     * @return static
73     */
74    public function setMemoryCost(int $memoryCost): static
75    {
76        $this->memoryCost = $memoryCost;
77        return $this;
78    }
79
80    /**
81     * Get memory cost
82     *
83     * @return int
84     */
85    public function getMemoryCost(): int
86    {
87        return $this->memoryCost;
88    }
89
90    /**
91     * Has memory cost
92     *
93     * @return bool
94     */
95    public function hasMemoryCost(): bool
96    {
97        return !empty($this->memoryCost);
98    }
99
100    /**
101     * Set time cost
102     *
103     * @param  int $timeCost
104     * @return static
105     */
106    public function setTimeCost(int $timeCost): static
107    {
108        $this->timeCost = $timeCost;
109        return $this;
110    }
111
112    /**
113     * Get time cost
114     *
115     * @return int
116     */
117    public function getTimeCost(): int
118    {
119        return $this->timeCost;
120    }
121
122    /**
123     * Has time cost
124     *
125     * @return bool
126     */
127    public function hasTimeCost(): bool
128    {
129        return !empty($this->timeCost);
130    }
131
132    /**
133     * Set threads
134     *
135     * @param  int $threads
136     * @return static
137     */
138    public function setThreads(int $threads): static
139    {
140        $this->threads = $threads;
141        return $this;
142    }
143
144    /**
145     * Get threads
146     *
147     * @return int
148     */
149    public function getThreads(): int
150    {
151        return $this->threads;
152    }
153
154    /**
155     * Has threads
156     *
157     * @return bool
158     */
159    public function hasThreads(): bool
160    {
161        return !empty($this->threads);
162    }
163
164    /**
165     * Set hasher options
166     *
167     * @param  array $options
168     * @return static
169     */
170    public function setOptions(array $options): static
171    {
172        if (isset($options['memory_cost'])) {
173            $this->setMemoryCost($options['memory_cost']);
174        }
175        if (isset($options['time_cost'])) {
176            $this->setTimeCost($options['time_cost']);
177        }
178        if (isset($options['threads'])) {
179            $this->setThreads($options['threads']);
180        }
181        return $this;
182    }
183
184}