Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
BcryptHasher
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
7 / 7
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCost
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasCost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 make
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 requiresRehash
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
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 Bcrypt 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 */
26class BcryptHasher extends AbstractHasher
27{
28
29    /**
30     * Cost
31     * @var int
32     */
33    protected int $cost = 12;
34
35    /**
36     * Constructor
37     *
38     * Instantiate the Bcrypt object
39     *
40     * @param int     $cost
41     * @param ?string $salt
42     */
43    public function __construct(int $cost = 12)
44    {
45        $this->setCost($cost);
46    }
47
48    /**
49     * Set cost
50     *
51     * @param  int $cost
52     * @return BcryptHasher
53     */
54    public function setCost(int $cost): BcryptHasher
55    {
56        $this->cost = $cost;
57        return $this;
58    }
59
60    /**
61     * Get cost
62     *
63     * @return int
64     */
65    public function getCost(): int
66    {
67        return $this->cost;
68    }
69
70    /**
71     * Has cost
72     *
73     * @return bool
74     */
75    public function hasCost(): bool
76    {
77        return !empty($this->cost);
78    }
79
80    /**
81     * Make hashed value (based on the hasher class)
82     *
83     * @param  string $value
84     * @return string
85     */
86    public function make(#[\SensitiveParameter] string $value): string
87    {
88        return $this->createHash($value, PASSWORD_BCRYPT,  ['cost' => $this->getCost()]);
89    }
90
91    /**
92     * Determine if the hashed value requires re-hashing (based on the hasher class)
93     *
94     * @param  string $hashedValue
95     * @return bool
96     */
97    public function requiresRehash(string $hashedValue): bool
98    {
99        return $this->needsRehash($hashedValue, PASSWORD_BCRYPT, ['cost' => $this->getCost()]);
100    }
101
102    /**
103     * Set hasher options
104     *
105     * @param  array $options
106     * @return static
107     */
108    public function setOptions(array $options): static
109    {
110        if (isset($options['cost'])) {
111            $this->setCost($options['cost']);
112        }
113        return $this;
114    }
115
116}