1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait implementation of the hashing output formats for digestion algorithms. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\MessageDigestion; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface as DigestionFormatsSpecification; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait DigestionFormatsTrait - Reusable implementation of `DigestionFormatsInterface`. |
13
|
|
|
* |
14
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface The abstract specification. |
15
|
|
|
* |
16
|
|
|
* @package CryptoManana\Core\Traits\MessageDigestion |
17
|
|
|
* |
18
|
|
|
* @property int $digestFormat The digest output format property storage. |
19
|
|
|
* |
20
|
|
|
* @mixin DigestionFormatsSpecification |
21
|
|
|
*/ |
22
|
|
|
trait DigestionFormatsTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Internal method for converting the digest's output format representation via the chosen format. |
26
|
|
|
* |
27
|
|
|
* @param string $digest The output digest. |
28
|
|
|
* |
29
|
|
|
* @return string The input data with proper salting. |
30
|
|
|
*/ |
31
|
176 |
|
protected function changeOutputFormat($digest) |
32
|
|
|
{ |
33
|
176 |
|
switch ($this->getDigestFormat()) { |
34
|
176 |
|
case self::DIGEST_OUTPUT_HEX_LOWER: |
|
|
|
|
35
|
66 |
|
break; // Comes already in lower, can be skipped |
36
|
132 |
|
case self::DIGEST_OUTPUT_HEX_UPPER: |
|
|
|
|
37
|
110 |
|
$digest = strtoupper($digest); |
38
|
110 |
|
break; |
39
|
44 |
|
case self::DIGEST_OUTPUT_BASE_64: |
|
|
|
|
40
|
22 |
|
$digest = base64_encode(pack('H*', $digest)); |
41
|
22 |
|
break; |
42
|
44 |
|
case self::DIGEST_OUTPUT_BASE_64_URL: |
|
|
|
|
43
|
22 |
|
$digest = base64_encode(pack('H*', $digest)); |
44
|
22 |
|
$digest = str_replace(['+', '/', '='], ['-', '_', ''], $digest); |
45
|
22 |
|
break; |
46
|
|
|
default: // case self::DIGEST_OUTPUT_RAW: |
47
|
44 |
|
break; |
48
|
|
|
} |
49
|
|
|
|
50
|
176 |
|
return $digest; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Setter for the digest format code property. |
55
|
|
|
* |
56
|
|
|
* @param int $digestFormat The digest format code. |
57
|
|
|
* |
58
|
|
|
* @return $this The hash algorithm object. |
59
|
|
|
* @throw \Exception Validation errors. |
60
|
|
|
*/ |
61
|
110 |
|
public function setDigestFormat($digestFormat) |
62
|
|
|
{ |
63
|
110 |
|
$digestFormat = filter_var( |
64
|
110 |
|
$digestFormat, |
65
|
110 |
|
FILTER_VALIDATE_INT, |
66
|
|
|
[ |
67
|
|
|
"options" => [ |
68
|
110 |
|
"min_range" => self::DIGEST_OUTPUT_RAW, |
|
|
|
|
69
|
110 |
|
"max_range" => self::DIGEST_OUTPUT_BASE_64_URL, |
|
|
|
|
70
|
|
|
], |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
|
74
|
110 |
|
if ($digestFormat === false) { |
75
|
22 |
|
throw new \InvalidArgumentException('Digest output format mode must be an integer between -1 and 3.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
88 |
|
$this->digestFormat = $digestFormat; |
79
|
|
|
|
80
|
88 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Getter for the digest format code property. |
85
|
|
|
* |
86
|
|
|
* @return int The digest format code. |
87
|
|
|
*/ |
88
|
176 |
|
public function getDigestFormat() |
89
|
|
|
{ |
90
|
176 |
|
return $this->digestFormat; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|