1 | <?php |
||
16 | class Security |
||
17 | { |
||
18 | /** @var string */ |
||
19 | const VERSION = 'HMAC_SHA256_V1'; |
||
20 | |||
21 | /** |
||
22 | * Encode merchant parameters |
||
23 | * |
||
24 | * @param array $data The parameters to encode |
||
25 | * |
||
26 | * @return string Encoded data |
||
27 | */ |
||
28 | 1 | public function encodeMerchantParameters($data) |
|
29 | { |
||
30 | 1 | return base64_encode(json_encode($data)); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * Decode merchant parameters |
||
35 | * |
||
36 | * @param string $data The encoded string of parameters |
||
37 | * |
||
38 | * @return array Decoded data |
||
39 | */ |
||
40 | 9 | public function decodeMerchantParameters($data) |
|
41 | { |
||
42 | 9 | return (array)json_decode(base64_decode(strtr($data, '-_', '+/'))); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Encrypt message with given key and default IV |
||
47 | * |
||
48 | * @param string $message The message to encrypt |
||
49 | * @param string $key The key used to encrypt the message |
||
50 | * |
||
51 | * @return string Encrypted message |
||
52 | * |
||
53 | * @throws RuntimeException |
||
54 | */ |
||
55 | 11 | protected function encryptMessage($message, $key) |
|
56 | { |
||
57 | 11 | $iv = implode(array_map('chr', array(0, 0, 0, 0, 0, 0, 0, 0))); |
|
58 | |||
59 | 11 | if ($this->hasValidEncryptionMethod()) { |
|
60 | 10 | $ciphertext = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv); |
|
61 | 10 | } else { |
|
62 | 1 | throw new RuntimeException('No valid encryption extension installed'); |
|
63 | } |
||
64 | |||
65 | 10 | return $ciphertext; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Check if the system has a valid encryption method available |
||
70 | * |
||
71 | * @todo add or switch to extension_loaded()? |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 9 | public function hasValidEncryptionMethod() |
|
79 | |||
80 | /** |
||
81 | * Create signature hash used to verify messages |
||
82 | * |
||
83 | * @todo Add if-check on algorithm to match against signature version as new param? |
||
84 | * |
||
85 | * @param string $message The message to encrypt |
||
86 | * @param string $salt Unique salt used to generate the ciphertext |
||
87 | * @param string $key The key used to encrypt the message |
||
88 | * |
||
89 | * @return string Generated signature |
||
90 | */ |
||
91 | 9 | public function createSignature($message, $salt, $key) |
|
96 | |||
97 | 8 | public function createReturnSignature($message, $salt, $key) |
|
101 | } |
||
102 |