1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The SHA-3 family PBKDF2-SHA-384 hashing algorithm class. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Hashing; |
8
|
|
|
|
9
|
|
|
use CryptoManana\Core\Abstractions\MessageDigestion\AbstractIterativeSlowDerivation as SlowDerivationAlgorithm; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Pbkdf2ShaThree384 - The SHA-3 family PBKDF2-SHA-384 hashing algorithm object. |
13
|
|
|
* |
14
|
|
|
* @package CryptoManana\Hashing |
15
|
|
|
*/ |
16
|
|
|
class Pbkdf2ShaThree384 extends SlowDerivationAlgorithm |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The internal name of the algorithm. |
20
|
|
|
*/ |
21
|
|
|
const ALGORITHM_NAME = 'sha3-384'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The internal maximum length in bytes of the output digest for the algorithm. |
25
|
|
|
* |
26
|
|
|
* @note For the current algorithm: `PHP_INT_MAX` |
27
|
|
|
*/ |
28
|
|
|
const ALGORITHM_MAXIMUM_OUTPUT = PHP_INT_MAX; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The derivation output digest size in bytes length property storage. |
32
|
|
|
* |
33
|
|
|
* @var int The derivation output digest size in bytes length value. |
34
|
|
|
* |
35
|
|
|
* @note The default output size in bytes for this algorithm. |
36
|
|
|
*/ |
37
|
|
|
protected $outputLength = 48; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Password-based key derivation algorithm constructor. |
41
|
|
|
*/ |
42
|
21 |
|
public function __construct() |
43
|
|
|
{ |
44
|
21 |
|
parent::__construct(); |
45
|
|
|
|
46
|
21 |
|
$this->useNative = !in_array(static::ALGORITHM_NAME, hash_algos(), true); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Calculates a hash value for the given data. |
51
|
|
|
* |
52
|
|
|
* @param string $data The input string. |
53
|
|
|
* |
54
|
|
|
* @return string The digest. |
55
|
|
|
* @throws \Exception Validation errors. |
56
|
|
|
*/ |
57
|
10 |
|
public function hashData($data) |
58
|
|
|
{ |
59
|
10 |
|
if (!is_string($data)) { |
60
|
1 |
|
throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
9 |
|
$data = $this->addSaltString($data); |
64
|
|
|
|
65
|
|
|
// @codeCoverageIgnoreStart |
66
|
|
|
/** |
67
|
|
|
* {@internal Backward compatibility native realization for SHA-3 must be used. }} |
68
|
|
|
*/ |
69
|
|
|
$digest = ($this->useNative) ? |
70
|
|
|
\CryptoManana\Compatibility\NativePbkdf2Sha3::digest384( |
71
|
|
|
$data, |
72
|
|
|
$this->derivationSalt, |
73
|
|
|
$this->numberOfIterations, |
74
|
|
|
$this->outputLength, |
75
|
|
|
true |
76
|
|
|
) |
77
|
|
|
: |
78
|
|
|
hash_pbkdf2( |
79
|
|
|
static::ALGORITHM_NAME, |
80
|
|
|
$data, |
81
|
|
|
$this->derivationSalt, |
82
|
|
|
$this->numberOfIterations, |
83
|
|
|
$this->outputLength, |
84
|
|
|
true |
85
|
|
|
); |
86
|
|
|
// @codeCoverageIgnoreEnd |
87
|
|
|
|
88
|
9 |
|
if ($this->digestFormat !== self::DIGEST_OUTPUT_RAW) { |
89
|
9 |
|
$digest = bin2hex($digest); |
90
|
|
|
} |
91
|
|
|
|
92
|
9 |
|
$digest = $this->changeOutputFormat($digest); |
93
|
|
|
|
94
|
9 |
|
return $digest; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|