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
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 Bcrypt hasher
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 BcryptHasher extends AbstractHasher
28{
29
30    /**
31     * Cost
32     * @var int
33     */
34    protected int $cost = 12;
35
36    /**
37     * Constructor
38     *
39     * Instantiate the Bcrypt object
40     *
41     * @param int $cost
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}