-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_base64.py
More file actions
242 lines (201 loc) · 8.84 KB
/
lib_base64.py
File metadata and controls
242 lines (201 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -*- coding: utf-8 -*-
from __future__ import division
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any, Callable
import base64
from .lib_object import BaseManager
class Base64:
"""
Base64 提供了 base64 相关的内置函数
"""
_manager = BaseManager()
def __init__(self, manager): # type: (BaseManager) -> None
"""初始化并返回一个新的 Base64
Args:
manager (BaseManager):
用于管理引用对象的对象管理器
"""
self._manager = manager
def b16decode(self, string, casefold=False): # type: (str, bool) -> int | str
"""b16decode 解码一个 Base16 编码的字符串
Args:
string (str):
待解码的字符串
casefold (bool, optional):
是否忽略大小写。
默认值为 False
Returns:
int | str:
如果返回了一个整数,则它是一个指向了 bytes 的指针;
否则,返回了一个字符串,表示其在较低 Python 版本中的结果
"""
result = base64.b16decode(string, casefold)
if isinstance(result, str):
return result
return self._manager.ref(result)
def b16encode(self, ptr_or_str): # type: (int | str) -> str
"""b16encode 将给定的对象按 Base16 编码
Args:
ptr_or_str (int | str):
如果提供的是整数,则将从它指向的 bytes 对象编码;
否则,提供的是字符串,则将直接对其进行编码
Returns:
str: 编码所得的 Base16 字符串
"""
if isinstance(ptr_or_str, int):
obj_a = self._manager.deref(ptr_or_str) # type: bytes
return base64.b16encode(obj_a).decode(encoding="utf-8")
else:
temp = ptr_or_str # type: Any
obj_b = temp # type: bytes
return base64.b16encode(obj_b) # type: ignore
def b32decode(self, string, casefold=False): # type: (str, bool) -> int | str
"""b32decode 解码一个 Base32 编码的字符串
Args:
string (str):
待解码的字符串
casefold (bool, optional):
是否忽略大小写。
默认值为 False
Returns:
int | str:
如果返回了一个整数,则它是一个指向了 bytes 的指针;
否则,返回了一个字符串,表示其在较低 Python 版本中的结果
"""
result = base64.b32decode(string, casefold)
if isinstance(result, str):
return result
return self._manager.ref(result)
def b32encode(self, ptr_or_str): # type: (int | str) -> str
"""b32encode 将给定的对象按 Base32 编码
Args:
ptr_or_str (int | str):
如果提供的是整数,则将从它指向的 bytes 对象编码;
否则,提供的是字符串,则将直接对其进行编码
Returns:
str: 编码所得的 Base32 字符串
"""
if isinstance(ptr_or_str, int):
obj_a = self._manager.deref(ptr_or_str) # type: bytes
return base64.b32encode(obj_a).decode(encoding="utf-8")
else:
temp = ptr_or_str # type: Any
obj_b = temp # type: bytes
return base64.b32encode(obj_b) # type: ignore
def b64decode(self, string, altchars=None): # type: (str, str | None) -> int | str
"""b64decode 解码一个 Base64 编码的字符串
Args:
string (str): 待解码的字符串
altchars (str | None, optional):
可选的替代字符,用于替换标准 Base64 字符集中的 '+' 和 '/'。
默认值为 None
Returns:
int | str:
如果返回了一个整数,则它是一个指向了 bytes 的指针;
否则,返回了一个字符串,表示其在较低 Python 版本中的结果
"""
result = base64.b64decode(string, altchars)
if isinstance(result, str):
return result
return self._manager.ref(result)
def b64encode(self, ptr_or_str): # type: (int | str) -> str
"""b64encode 将给定的对象按 Base64 编码
Args:
ptr_or_str (int | str):
如果提供的是整数,则将从它指向的 bytes 对象编码;
否则,提供的是字符串,则将直接对其进行编码
Returns:
str: 编码所得的 Base64 字符串
"""
if isinstance(ptr_or_str, int):
obj_a = self._manager.deref(ptr_or_str) # type: bytes
return base64.b64encode(obj_a).decode(encoding="utf-8")
else:
temp = ptr_or_str # type: Any
obj_b = temp # type: bytes
return base64.b64encode(obj_b) # type: ignore
def standard_b64decode(self, string): # type: (str) -> int | str
"""standard_b64decode 按 Base64 标准解码一个字符串
Args:
string (str): 待解码的字符串
Returns:
int | str:
如果返回了一个整数,则它是一个指向了 bytes 的指针;
否则,返回了一个字符串,表示其在较低 Python 版本中的结果
"""
result = base64.standard_b64decode(string)
if isinstance(result, str):
return result
return self._manager.ref(result)
def standard_b64encode(self, ptr_or_str): # type: (int | str) -> str
"""standard_b64encode 按 Base64 标准进行编码
Args:
ptr_or_str (int | str):
如果提供的是整数,则将从它指向的 bytes 对象编码;
否则,提供的是字符串,则将直接对其进行编码
Returns:
str: 编码所得的 Base64 字符串
"""
if isinstance(ptr_or_str, int):
obj_a = self._manager.deref(ptr_or_str) # type: bytes
return base64.standard_b64encode(obj_a).decode(encoding="utf-8")
else:
temp = ptr_or_str # type: Any
obj_b = temp # type: bytes
return base64.standard_b64encode(obj_b) # type: ignore
def urlsafe_b64decode(self, string): # type: (str) -> int | str
"""urlsafe_b64decode 解码一个 URL 安全的 Base64 编码字符串
Args:
string (str): 待解码的字符串
Returns:
int | str:
如果返回了一个整数,则它是一个指向了 bytes 的指针;
否则,返回了一个字符串,表示其在较低 Python 版本中的结果
"""
result = base64.urlsafe_b64decode(string)
if isinstance(result, str):
return result
return self._manager.ref(result)
def urlsafe_b64encode(self, ptr_or_str): # type: (int | str) -> str
"""
urlsafe_b64encode 编码一个 Base64 字符串,
并确保其对于 URL 是安全的
Args:
ptr_or_str (int | str):
如果提供的是整数,则将从它指向的 bytes 对象编码;
否则,提供的是字符串,则将直接对其进行编码
Returns:
str: 编码所得的 Base64 字符串
"""
if isinstance(ptr_or_str, int):
obj_a = self._manager.deref(ptr_or_str) # type: bytes
return base64.urlsafe_b64encode(obj_a).decode(encoding="utf-8")
else:
temp = ptr_or_str # type: Any
obj_b = temp # type: bytes
return base64.urlsafe_b64encode(obj_b) # type: ignore
def build_func(
self,
origin, # type: dict[str, Callable[..., int | bool | float | str]]
): # type: (...) -> None
"""
build_func 构建 base64 模块的内置函数,
并将构建结果写入到传递的 origin 字典中
Args:
origin (dict[str, Callable[..., int | bool | float | str]]):
用于存放所有内置函数的字典
"""
funcs = {} # type: dict[str, Callable[..., int | bool | float | str]]
funcs["base64.b16decode"] = self.b16decode
funcs["base64.b16encode"] = self.b16encode
funcs["base64.b32decode"] = self.b32decode
funcs["base64.b32encode"] = self.b32encode
funcs["base64.b64decode"] = self.b64decode
funcs["base64.b64encode"] = self.b64encode
funcs["base64.standard_b64decode"] = self.standard_b64decode
funcs["base64.standard_b64encode"] = self.standard_b64encode
funcs["base64.urlsafe_b64decode"] = self.urlsafe_b64decode
funcs["base64.urlsafe_b64encode"] = self.urlsafe_b64encode
for key, value in funcs.items():
origin[key] = value