Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bitgetlimited
GitHub Repository: bitgetlimited/v3-bitget-api-sdk
Path: blob/master/bitget-php-sdk-api/src/internal/RsaUtil.php
518 views
1
<?php
2
3
namespace bitget\internal;
4
5
6
/**
7
* rsa加密类
8
* Class Rsa
9
*/
10
class RsaUtil
11
{
12
13
const CHAR_SET = "UTF-8";
14
const BASE_64_FORMAT = "UrlSafeNoPadding";
15
const RSA_ALGORITHM_KEY_TYPE = OPENSSL_KEYTYPE_RSA;
16
const RSA_ALGORITHM_SIGN = OPENSSL_ALGO_SHA256;
17
18
protected $public_key;
19
protected $private_key;
20
protected $key_len;
21
22
public function __construct($pub_key = '', $pri_key = null)
23
{
24
if ($pub_key) {
25
$this->public_key = $pub_key;
26
$pub_id = openssl_pkey_get_public($this->public_key);
27
$this->key_len = openssl_pkey_get_details($pub_id)['bits'];
28
}
29
if ($pri_key) {
30
$this->private_key = $pri_key;
31
$pri_id = openssl_pkey_get_private($this->private_key);
32
print_r($pri_id);
33
34
// print_r("=1=".$pri_key."\n");
35
// print_r("=2=".$this->private_key."\n");
36
// print_r("=3=".$pri_id."\n");
37
38
// $this->key_len = openssl_pkey_get_details($pri_id)['bits'];
39
}
40
}
41
42
/*
43
* 创建密钥对
44
*/
45
public static function createKeys($key_size = 1024)
46
{
47
$config = array(
48
"private_key_bits" => $key_size,
49
"private_key_type" => self::RSA_ALGORITHM_KEY_TYPE,
50
);
51
$res = openssl_pkey_new($config);
52
openssl_pkey_export($res, $private_key);
53
$public_key_detail = openssl_pkey_get_details($res);
54
$public_key = $public_key_detail["key"];
55
56
return array(
57
"public_key" => $public_key,
58
"private_key" => $private_key,
59
);
60
}
61
62
/*
63
* 公钥加密
64
*/
65
public function publicEncrypt($data)
66
{
67
$encrypted = '';
68
$part_len = $this->key_len / 8 - 11;
69
$parts = str_split($data, $part_len);
70
71
foreach ($parts as $part) {
72
$encrypted_temp = '';
73
openssl_public_encrypt($part, $encrypted_temp, $this->public_key);
74
$encrypted .= $encrypted_temp;
75
}
76
77
return base64_encode($encrypted);
78
}
79
80
/*
81
* 私钥解密
82
*/
83
public function privateDecrypt($encrypted)
84
{
85
$decrypted = "";
86
$part_len = $this->key_len / 8;
87
//url 中的get传值默认会吧+号过滤成' ',替换回来就好了
88
str_replace('% ', '+', $encrypted);
89
echo $encrypted;
90
$base64_decoded = base64_decode($encrypted);
91
$parts = str_split($base64_decoded, $part_len);
92
foreach ($parts as $part) {
93
$decrypted_temp = '';
94
openssl_private_decrypt($part, $decrypted_temp, $this->private_key);
95
$decrypted .= $decrypted_temp;
96
}
97
return $decrypted;
98
}
99
100
/*
101
* 私钥加密
102
*/
103
public function privateEncrypt($data)
104
{
105
$encrypted = '';
106
$part_len = $this->key_len / 8 - 11;
107
$parts = str_split($data, $part_len);
108
109
foreach ($parts as $part) {
110
$encrypted_temp = '';
111
openssl_private_encrypt($part, $encrypted_temp, $this->private_key);
112
$encrypted .= $encrypted_temp;
113
}
114
return base64_encode($encrypted);
115
}
116
117
/*
118
* 公钥解密
119
*/
120
public function publicDecrypt($encrypted)
121
{
122
$decrypted = "";
123
$part_len = $this->key_len / 8;
124
$base64_decoded = base64_decode($encrypted);
125
$parts = str_split($base64_decoded, $part_len);
126
127
foreach ($parts as $part) {
128
$decrypted_temp = '';
129
openssl_public_decrypt($part, $decrypted_temp, $this->public_key);
130
$decrypted .= $decrypted_temp;
131
}
132
return $decrypted;
133
}
134
135
/*
136
* 数据加签
137
*/
138
public function sign($data)
139
{
140
// print_r("hehe ".$this->private_key);
141
openssl_sign($data, $sign, $this->private_key, self::RSA_ALGORITHM_SIGN);
142
return base64_encode($sign);
143
}
144
145
/*
146
* 数据签名验证
147
*/
148
public function verify($data, $sign)
149
{
150
$pub_id = openssl_get_publickey($this->public_key);
151
$res = openssl_verify($data, base64_decode($sign), $pub_id, self::RSA_ALGORITHM_SIGN);
152
return $res;
153
}
154
155
}
156
157