|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The SHA-3 family SHA-224 hashing algorithm class. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\Hashing; |
|
8
|
|
|
|
|
9
|
|
|
use CryptoManana\Core\Abstractions\MessageDigestion\AbstractUnkeyedHashFunction as UnkeyedHashAlgorithm; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ShaThree224 - The SHA-3 family SHA-224 hashing algorithm object. |
|
13
|
|
|
* |
|
14
|
|
|
* @package CryptoManana\Hashing |
|
15
|
|
|
*/ |
|
16
|
|
|
class ShaThree224 extends UnkeyedHashAlgorithm |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The internal name of the algorithm. |
|
20
|
|
|
*/ |
|
21
|
|
|
const ALGORITHM_NAME = 'sha3-224'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Checksum digestion algorithm constructor. |
|
25
|
|
|
*/ |
|
26
|
22 |
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
22 |
|
parent::__construct(); |
|
29
|
|
|
|
|
30
|
22 |
|
$this->useNative = !in_array(static::ALGORITHM_NAME, hash_algos(), true); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Calculates a hash value for the given data. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $data The input string. |
|
37
|
|
|
* |
|
38
|
|
|
* @return string The digest. |
|
39
|
|
|
* @throws \Exception Validation errors. |
|
40
|
|
|
*/ |
|
41
|
12 |
|
public function hashData($data) |
|
42
|
|
|
{ |
|
43
|
12 |
|
if (!is_string($data)) { |
|
44
|
2 |
|
throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
10 |
|
$data = $this->addSaltString($data); |
|
48
|
|
|
|
|
49
|
10 |
|
$rawOutput = ($this->digestFormat === self::DIGEST_OUTPUT_RAW); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@internal Backward compatibility native realization for SHA-3 must be used. }} |
|
53
|
|
|
*/ |
|
54
|
10 |
|
$digest = ($this->useNative) ? |
|
55
|
2 |
|
\CryptoManana\Compatibility\NativeSha3::digest224($data, $rawOutput) |
|
56
|
10 |
|
: |
|
57
|
10 |
|
hash(static::ALGORITHM_NAME, $data, $rawOutput); |
|
58
|
|
|
|
|
59
|
10 |
|
$digest = $this->changeOutputFormat($digest); |
|
60
|
|
|
|
|
61
|
10 |
|
return $digest; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|