Skip to content

Commit 6175ff5

Browse files
committed
perf(Base): 🎨 移除Base62依赖使用
1 parent 8abb0c1 commit 6175ff5

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

qsnctf/base.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import base64
55
import qsnctf.plugin.python.python3base92
66
import struct
7-
import base62
87
import base58
98

109

@@ -153,12 +152,32 @@ def base64_decode_custom(source_text, custom_table, encoding="utf-8", decoding="
153152
return str(e)
154153

155154

156-
def base62_encode(ints):
157-
return base62.encode(ints)
158-
159-
160-
def base62_decode(text):
161-
return base62.decode(text)
155+
def base62_encode(n):
156+
"""Encode an integer to a base62 string."""
157+
CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
158+
BASE = 62
159+
if not isinstance(n, int):
160+
raise TypeError("Expected int object, not {}".format(type(n).__name__))
161+
if n == 0:
162+
return CHARSET[0]
163+
result = []
164+
while n > 0:
165+
n, rem = divmod(n, BASE)
166+
result.append(CHARSET[rem])
167+
return ''.join(reversed(result))
168+
169+
def base62_decode(s):
170+
"""Decode a base62 string to an integer."""
171+
CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
172+
BASE = 62
173+
if not isinstance(s, str):
174+
raise TypeError("Expected str object, not {}".format(type(s).__name__))
175+
n = 0
176+
for char in s:
177+
if char not in CHARSET:
178+
raise ValueError(f"Invalid character '{char}' in base62 string")
179+
n = n * BASE + CHARSET.index(char)
180+
return n
162181

163182

164183
def base58_encode(text, encoding="utf-8", decoding="utf-8"):

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
PyExecJS2
2-
pybase62
32
base58
43
requests
54
bs4

test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from qsnctf import *
22

3-
a = Chinese_socialism_encode("1234")
3+
a = base62_decode("tWq")
44
print(a)

0 commit comments

Comments
 (0)