|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Drivers\Pasargad\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Shetabit\Multipay\Drivers\Pasargad\Utils\RSA; |
|
6
|
|
|
|
|
7
|
|
|
class RSAProcessor |
|
8
|
|
|
{ |
|
9
|
|
|
public const KEY_TYPE_XML_FILE = 'xml_file'; |
|
10
|
|
|
public const KEY_TYPE_XML_STRING = 'xml_string'; |
|
11
|
|
|
|
|
12
|
|
|
private $publicKey = null; |
|
13
|
|
|
private $privateKey = null; |
|
14
|
|
|
private $modulus = null; |
|
15
|
|
|
private $keyLength = "1024"; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($key, $keyType = null) |
|
18
|
|
|
{ |
|
19
|
|
|
$xmlObject = null; |
|
20
|
|
|
$keyType = is_null($keyType) ? null : strtolower($keyType); |
|
21
|
|
|
|
|
22
|
|
|
if ($keyType == null || $keyType == self::KEY_TYPE_XML_STRING) { |
|
|
|
|
|
|
23
|
|
|
$xmlObject = simplexml_load_string($key); |
|
24
|
|
|
} elseif ($keyType == self::KEY_TYPE_XML_FILE) { |
|
25
|
|
|
$xmlObject = simplexml_load_file($key); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->modulus = RSA::binaryToNumber(base64_decode($xmlObject->Modulus)); |
|
29
|
|
|
$this->publicKey = RSA::binaryToNumber(base64_decode($xmlObject->Exponent)); |
|
30
|
|
|
$this->privateKey = RSA::binaryToNumber(base64_decode($xmlObject->D)); |
|
31
|
|
|
$this->keyLength = strlen(base64_decode($xmlObject->Modulus)) * 8; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Retrieve public key |
|
36
|
|
|
* |
|
37
|
|
|
* @return string|null |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getPublicKey() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->publicKey; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retrieve private key |
|
46
|
|
|
* |
|
47
|
|
|
* @return string|null |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getPrivateKey() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->privateKey; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Retrieve key length |
|
56
|
|
|
* |
|
57
|
|
|
* @return integer |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getKeyLength() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->keyLength; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Retrieve modulus |
|
66
|
|
|
* |
|
67
|
|
|
* @return string|null |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getModulus() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->modulus; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Encrypt given data |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $data |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function encrypt($data) |
|
82
|
|
|
{ |
|
83
|
|
|
return base64_encode(RSA::rsaEncrypt($data, $this->publicKey, $this->modulus, $this->keyLength)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Decrypt given data |
|
88
|
|
|
* |
|
89
|
|
|
* @param $data |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function decrypt($data) |
|
94
|
|
|
{ |
|
95
|
|
|
return RSA::rsaDecrypt($data, $this->privateKey, $this->modulus, $this->keyLength); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Sign given data |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $data |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function sign($data) |
|
106
|
|
|
{ |
|
107
|
|
|
return RSA::rsaSign($data, $this->privateKey, $this->modulus, $this->keyLength); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Verify RSA data |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $data |
|
114
|
|
|
* |
|
115
|
|
|
* @return boolean |
|
116
|
|
|
*/ |
|
117
|
|
|
public function verify($data) |
|
118
|
|
|
{ |
|
119
|
|
|
return RSA::rsaVerify($data, $this->publicKey, $this->modulus, $this->keyLength); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|