1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The Bcrypt hashing algorithm class. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Hashing; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\MessageDigestion\AbstractHardwareResistantDerivation as StrongDerivationAlgorithm; |
10
|
|
|
use \CryptoManana\Core\Interfaces\MessageDigestion\AlgorithmicCostInterface as AlgorithmicCostTuning; |
11
|
|
|
use \CryptoManana\Core\Traits\MessageDigestion\AlgorithmicCostTrait as AlgorithmicCostTuningCapabilities; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Bcrypt - The Bcrypt hashing algorithm object. |
15
|
|
|
* |
16
|
|
|
* @package CryptoManana\Hashing |
17
|
|
|
* |
18
|
|
|
* @mixin AlgorithmicCostTuningCapabilities |
19
|
|
|
*/ |
20
|
|
|
class Bcrypt extends StrongDerivationAlgorithm implements AlgorithmicCostTuning |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Algorithmic cost tuning capabilities. |
24
|
|
|
* |
25
|
|
|
* {@internal Reusable implementation of `AlgorithmicCostInterface`. }} |
26
|
|
|
*/ |
27
|
|
|
use AlgorithmicCostTuningCapabilities; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The internal name of the algorithm. |
31
|
|
|
*/ |
32
|
|
|
const ALGORITHM_NAME = 'bcrypt'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The internal maximum length in bytes of the raw output digest for the algorithm. |
36
|
|
|
* |
37
|
|
|
* @internal For the current algorithm: `72` |
38
|
|
|
*/ |
39
|
|
|
const ALGORITHM_MAXIMUM_OUTPUT = 72; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The digestion internal computational cost property storage. |
43
|
|
|
* |
44
|
|
|
* @var int The algorithmic cost value. |
45
|
|
|
*/ |
46
|
|
|
protected $computationalCost = PASSWORD_BCRYPT_DEFAULT_COST; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Fetch the correctly formatted internal variation for digestion. |
50
|
|
|
* |
51
|
|
|
* @return int|string The chosen variation for password hashing. |
52
|
|
|
*/ |
53
|
|
|
protected function fetchAlgorithmVariation() |
54
|
|
|
{ |
55
|
|
|
return PASSWORD_BCRYPT; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Fetch the correctly formatted internal parameters for digestion. |
60
|
|
|
* |
61
|
|
|
* @return array The chosen parameters for password hashing. |
62
|
|
|
*/ |
63
|
|
|
protected function fetchAlgorithmParameters() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
'cost' => $this->computationalCost |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Password-based key derivation algorithm constructor. |
72
|
|
|
*/ |
73
|
|
|
public function __construct() |
74
|
|
|
{ |
75
|
|
|
parent::__construct(); |
76
|
|
|
|
77
|
|
|
// @codeCoverageIgnoreStart |
78
|
|
|
if (!in_array(PASSWORD_BCRYPT, password_algos(), true)) { |
79
|
|
|
throw new \RuntimeException( |
80
|
|
|
'The Bcrypt algorithm is not supported under the current system configuration.' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
// @codeCoverageIgnoreEnd |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|