cipher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 98
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A padDecode() 0 6 2
A encode() 0 3 1
A decode() 0 5 1
A jsonEncode() 0 3 1
A hash() 0 3 1
A jsonDecode() 0 3 1
A toBase64() 0 3 1
A hashCompare() 0 12 3
1
<?php
2
/**
3
 * ==================================
4
 * Responsible PHP API
5
 * ==================================
6
 *
7
 * @link Git https://github.com/vince-scarpa/responsibleAPI.git
8
 *
9
 * @api Responible API
10
 * @package responsible\core\encoder
11
 *
12
 * @author Vince scarpa <[email protected]>
13
 *
14
 */
15
namespace responsible\core\encoder;
16
17
class cipher
18
{
19
    /**
20
     * [jsonEncode]
21
     * @param  array $stdArray
22
     *         Array to encode
23
     * @return string
24
     */
25
    public function jsonEncode(array $stdArray)
26
    {
27
        return json_encode($stdArray);
28
    }
29
30
    /**
31
     * [jsonDecode]
32
     * @param  string $jsonString
33
     *         String to decode
34
     * @return array
35
     */
36
    public function jsonDecode($jsonString)
37
    {
38
        return json_decode($jsonString, true, 512, JSON_BIGINT_AS_STRING);
39
    }
40
41
    /**
42
     * [encode]
43
     * @param  string $stdString
44
     * @return string
45
     */
46
    public function encode($stdString)
47
    {
48
        return $this->toBase64(base64_encode($stdString));
49
    }
50
51
    /**
52
     * [decode]
53
     * @param  string $toDecode
54
     * @return string
55
     */
56
    public function decode($toDecode)
57
    {
58
        return (string) base64_decode(
59
            $this->padDecode($toDecode),
60
            true
61
        );
62
    }
63
64
    /**
65
     * [toBase64Url]
66
     * @param  string $base64
67
     * @return string
68
     */
69
    private function toBase64($base64)
70
    {
71
        return str_replace(['+', '/', '='], ['-', '_', ''], $base64);
72
    }
73
74
    /**
75
     * [hash]
76
     * @return string
77
     */
78
    public function hash($algo, $stdString, $secret)
79
    {
80
        return hash_hmac($algo, $stdString, $secret, true);
81
    }
82
83
    /**
84
     * [hash_compare Compare two hashed strings]
85
     * @param  string $a
86
     *         Known user hash
87
     * @param  string $b 
88
     *         Unknown user hash
89
     * @return boolean
90
     */
91
    public function hashCompare($a, $b)
92
    {
93
        $len = strlen($a);
94
        if ($len !== strlen($b)) {
95
            return false;
96
        }
97
98
        $status = 0;
99
        for ($i = 0; $i < $len; $i++) {
100
            $status |= ord($a[$i]) ^ ord($b[$i]);
101
        }
102
        return $status === 0;
103
    }
104
105
    /**
106
     * [padDecode]
107
     * @param string $base64String
108
     */
109
    private function padDecode($base64String)
110
    {
111
        if (strlen($base64String) % 4 !== 0) {
112
            return $this->padDecode($base64String . '=');
113
        }
114
        return $base64String;
115
    }
116
}
117