1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Omnipay\Twispay; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Helper |
7
|
|
|
* @package ByTIC\Omnipay\Twispay |
8
|
|
|
*/ |
9
|
|
|
class Helper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param array $data |
13
|
|
|
* @param string $key |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
3 |
|
public static function generateChecksum(array $data, string $key) |
17
|
|
|
{ |
18
|
3 |
|
unset($data['checksum']); |
19
|
3 |
|
self::recursiveKeySort($data); |
20
|
3 |
|
$query = http_build_query($data); |
21
|
3 |
|
$encoded = hash_hmac('sha512', $query, $key, true); |
22
|
3 |
|
return base64_encode($encoded); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $data |
27
|
|
|
*/ |
28
|
3 |
|
private static function recursiveKeySort(array &$data) |
29
|
|
|
{ |
30
|
3 |
|
ksort($data, SORT_STRING); |
31
|
3 |
|
foreach ($data as $key => $value) { |
32
|
3 |
|
if (is_array($value)) { |
33
|
3 |
|
self::recursiveKeySort($data[$key]); |
34
|
|
|
} |
35
|
|
|
} |
36
|
3 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $encrypted |
40
|
|
|
* @param $key |
41
|
|
|
* @return null|string |
42
|
|
|
* @throws \Exception |
43
|
|
|
*/ |
44
|
|
|
public static function decrypt(string $encrypted, $key) |
45
|
|
|
{ |
46
|
|
|
if (strpos($encrypted, ',') !== false) { |
47
|
|
|
$encryptedParts = explode(',', $encrypted, 2); |
48
|
|
|
$iv = base64_decode($encryptedParts[0]); |
49
|
|
|
if ($iv === false) { |
50
|
|
|
throw new \Exception('Invalid encryption iv'); |
51
|
|
|
} |
52
|
|
|
$encrypted = base64_decode($encryptedParts[1]); |
53
|
|
|
if ($encrypted === false) { |
54
|
|
|
throw new \Exception('Invalid encrypted data'); |
55
|
|
|
} |
56
|
|
|
$decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); |
|
|
|
|
57
|
|
|
if ($decrypted === false) { |
58
|
|
|
throw new \Exception('Cannot decrypt data'); |
59
|
|
|
} |
60
|
|
|
return $decrypted; |
61
|
|
|
} |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|