You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

#!/usr/bin/python
# -*- coding:utf-8 -*-
import base64
import urllib.parse
def urlEncode(content: str):
try:
return urllib.parse.urlencode(content)
except:
return None
def urlDecode(content: str):
try:
return urllib.parse.unquote(content)
except:
return None
def base64Encode(content: str, urlSafe: bool = False, isPadding: bool = True):
try:
content = base64.b64encode(content.encode()).decode()
if urlSafe:
content = content.replace('+', '-')
content = content.replace('/', '_')
if not isPadding:
content = content.replace('=', '')
return content
except:
return None
def base64Decode(content: str):
content = content.replace('-', '+').replace('_', '/')
if len(content) % 4 in range(2, 4): # remainder -> 2 or 3
content = content.ljust((len(content) // 4 + 1) * 4, '=') # increase to 4n
try:
return base64.b64decode(content).decode()
except:
return None