1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LegalThings\DataEnricher\Processor; |
4
|
|
|
|
5
|
|
|
use LegalThings\DataEnricher\Node; |
6
|
|
|
use LegalThings\DataEnricher\Processor; |
7
|
|
|
use StephenHill\Base58; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Hash processor |
11
|
|
|
*/ |
12
|
|
|
class Hash implements Processor |
13
|
|
|
{ |
14
|
|
|
use Processor\Implementation; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Apply processing to a single node |
18
|
|
|
* |
19
|
|
|
* @param Node $node |
20
|
|
|
*/ |
21
|
10 |
|
public function applyToNode(Node $node) |
22
|
|
|
{ |
23
|
10 |
|
$instruction = $node->getInstruction($this); |
24
|
|
|
|
25
|
10 |
|
if (is_array($instruction) || is_object($instruction)) { |
26
|
10 |
|
$instruction = (object)$instruction; |
27
|
10 |
|
} |
28
|
|
|
|
29
|
10 |
|
if (!isset($instruction) || !isset($instruction->input) || !isset($instruction->algo)) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
10 |
|
if (!method_exists($this, $instruction->algo)) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
10 |
|
$hmac = isset($instruction->hmac) ? $instruction->hmac : null; |
38
|
10 |
|
$result = call_user_func_array([$this, $instruction->algo], [$instruction->input, $hmac]); |
39
|
|
|
|
40
|
10 |
|
$node->setResult($result); |
41
|
10 |
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* md5 hash |
46
|
|
|
* |
47
|
|
|
* @param string $input |
48
|
|
|
* @param string $hmac |
49
|
|
|
*/ |
50
|
2 |
|
public function md5($input, $hmac = null) |
51
|
|
|
{ |
52
|
|
|
return $hmac ? |
53
|
2 |
|
hash_hmac('md5', $input, $hmac) : |
54
|
2 |
|
hash('md5', $input); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* sha1 hash |
59
|
|
|
* |
60
|
|
|
* @param string $input |
61
|
|
|
* @param string $hmac |
62
|
|
|
*/ |
63
|
2 |
|
public function sha1($input, $hmac = null) |
64
|
|
|
{ |
65
|
|
|
return $hmac ? |
66
|
2 |
|
hash_hmac('sha1', $input, $hmac) : |
67
|
2 |
|
hash('sha1', $input); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* sha256 hash |
72
|
|
|
* |
73
|
|
|
* @param string $input |
74
|
|
|
* @param string $hmac |
75
|
|
|
*/ |
76
|
2 |
|
public function sha256($input, $hmac = null) |
77
|
|
|
{ |
78
|
|
|
return $hmac ? |
79
|
2 |
|
hash_hmac('sha256', $input, $hmac) : |
80
|
2 |
|
hash('sha256', $input); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* sha512 hash |
85
|
|
|
* |
86
|
|
|
* @param string $input |
87
|
|
|
* @param string $hmac |
88
|
|
|
*/ |
89
|
2 |
|
public function sha512($input, $hmac = null) |
90
|
|
|
{ |
91
|
|
|
return $hmac ? |
92
|
2 |
|
hash_hmac('sha512', $input, $hmac) : |
93
|
2 |
|
hash('sha512', $input); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* crc32 hash |
98
|
|
|
* |
99
|
|
|
* @param string $input |
100
|
|
|
* @param string $hmac |
101
|
|
|
*/ |
102
|
2 |
|
public function crc32($input, $hmac = null) |
103
|
|
|
{ |
104
|
|
|
return $hmac ? |
105
|
2 |
|
hash_hmac('crc32', $input, $hmac) : |
106
|
2 |
|
hash('crc32', $input); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|