1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
+------------------------------------------------------------------------+ |
4
|
|
|
| Plinker-RPC PHP | |
5
|
|
|
+------------------------------------------------------------------------+ |
6
|
|
|
| Copyright (c)2017-2018 (https://github.com/plinker-rpc/core) | |
7
|
|
|
+------------------------------------------------------------------------+ |
8
|
|
|
| This source file is subject to MIT License | |
9
|
|
|
| that is bundled with this package in the file LICENSE. | |
10
|
|
|
| | |
11
|
|
|
| If you did not receive a copy of the license and are unable to | |
12
|
|
|
| obtain it through the world-wide-web, please send an email | |
13
|
|
|
| to [email protected] so we can send you a copy immediately. | |
14
|
|
|
+------------------------------------------------------------------------+ |
15
|
|
|
| Authors: Lawrence Cherone <[email protected]> | |
16
|
|
|
+------------------------------------------------------------------------+ |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Plinker\Core\Lib; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Plinker\Core\Lib\Signer |
23
|
|
|
*/ |
24
|
|
|
final class Signer |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var |
28
|
|
|
*/ |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class construct |
33
|
|
|
* |
34
|
|
|
* @param array $config - config array which holds object configuration |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct($config = []) |
38
|
|
|
{ |
39
|
|
|
// |
40
|
1 |
|
$this->config = array_merge([ |
41
|
1 |
|
"secret" => null |
42
|
1 |
|
], $config); |
43
|
|
|
|
44
|
|
|
// hash secret |
45
|
1 |
|
if (isset($this->config["secret"])) { |
46
|
1 |
|
$this->config["secret"] = hash("sha256", gmdate("h").$this->config["secret"]); |
47
|
|
|
} |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
1 |
|
private function encrypt($plaintext, $password) |
54
|
|
|
{ |
55
|
1 |
|
$method = "AES-256-CBC"; |
56
|
1 |
|
$key = (string) hash("sha256", $password, true); |
57
|
1 |
|
$iv = (string) openssl_random_pseudo_bytes(16); |
58
|
1 |
|
$ciphertext = (string) openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv); |
59
|
|
|
|
60
|
1 |
|
$hash = (string) hash_hmac("sha256", $ciphertext, $key, true); |
61
|
|
|
|
62
|
1 |
|
return base64_encode($iv . $hash . $ciphertext); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
private function decrypt($ciphertext, $password) |
69
|
|
|
{ |
70
|
|
|
$ciphertext = base64_decode($ciphertext); |
71
|
|
|
|
72
|
|
|
$method = "AES-256-CBC"; |
73
|
|
|
$iv = substr($ciphertext, 0, 16); |
74
|
|
|
$hash = substr($ciphertext, 16, 32); |
75
|
|
|
$ciphertext = substr($ciphertext, 48); |
76
|
|
|
$key = (string) hash("sha256", $password, true); |
77
|
|
|
|
78
|
|
|
if (hash_hmac("sha256", $ciphertext, $key, true) !== $hash) { |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return openssl_decrypt($ciphertext, $method, $key, OPENSSL_RAW_DATA, $iv); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sign and encrypt into payload array. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
1 |
|
public function encode($data) |
91
|
|
|
{ |
92
|
1 |
|
$data = serialize($data); |
93
|
|
|
|
94
|
|
|
return [ |
95
|
1 |
|
"data" => $this->encrypt($data, $this->config["secret"]), |
96
|
1 |
|
"token" => hash_hmac( |
97
|
1 |
|
"sha256", |
98
|
1 |
|
$data, |
99
|
1 |
|
$this->config["secret"] |
100
|
|
|
) |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Decrypt, verify and unserialize payload. |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function decode($data) |
110
|
|
|
{ |
111
|
|
|
$data["data"] = $this->decrypt($data["data"], $this->config["secret"]); |
112
|
|
|
|
113
|
|
|
if (hash_hmac( |
114
|
|
|
"sha256", |
115
|
|
|
$data["data"], |
116
|
|
|
$this->config["secret"] |
117
|
|
|
) == $data["token"]) { |
118
|
|
|
return unserialize($data["data"]); |
119
|
|
|
} else { |
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|