Failed Conditions
Pull Request — master (#32)
by Moesjarraf
06:49
created

Hash::sha512()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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
    public function applyToNode(Node $node)
22
    {
23
        $instruction = $node->getInstruction($this);
24
        
25
        if (is_array($instruction) || is_object($instruction)) {
26
            $instruction = (object)$instruction;
27
        }
28
        
29
        if (!isset($instruction) || !isset($instruction->input) || !isset($instruction->algo)) {
30
            return;
31
        }
32
        
33
        if (!method_exists($this, $instruction->algo)) {
34
            return;
35
        }
36
        
37
        $hmac = isset($instruction->hmac) ? $instruction->hmac : null;
38
        $result = call_user_func_array([$this, $instruction->algo], [$instruction->input, $hmac]);
39
        
40
        $node->setResult($result);
41
    }
42
    
43
    
44
    /**
45
     * md5 hash
46
     * 
47
     * @param string $input
48
     * @param string $hmac
49
     */
50
    public function md5($input, $hmac = null)
51
    {
52
        return $hmac ?
53
            hash_hmac('md5', $input, $hmac) :
54
            hash('md5', $input);
55
    }
56
    
57
    /**
58
     * sha1 hash
59
     * 
60
     * @param string $input
61
     * @param string $hmac
62
     */
63
    public function sha1($input, $hmac = null)
64
    {
65
        return $hmac ?
66
            hash_hmac('sha1', $input, $hmac) :
67
            hash('sha1', $input);
68
    }
69
    
70
    /**
71
     * sha256 hash
72
     * 
73
     * @param string $input
74
     * @param string $hmac
75
     */
76
    public function sha256($input, $hmac = null)
77
    {
78
        return $hmac ?
79
            hash_hmac('sha256', $input, $hmac) :
80
            hash('sha256', $input);
81
    }
82
    
83
    /**
84
     * sha512 hash
85
     * 
86
     * @param string $input
87
     * @param string $hmac
88
     */
89
    public function sha512($input, $hmac = null)
90
    {
91
        return $hmac ?
92
            hash_hmac('sha512', $input, $hmac) :
93
            hash('sha512', $input);
94
    }
95
    
96
    /**
97
     * crc32 hash
98
     * 
99
     * @param string $input
100
     * @param string $hmac
101
     */
102
    public function crc32($input, $hmac = null)
0 ignored issues
show
Unused Code introduced by
The parameter $hmac is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        // doesn't support hmac variant
105
        return crc32($input);
106
    }
107
}
108