mirror of https://github.com/dnomd343/ProxyC
dnomd343
2 years ago
3 changed files with 37 additions and 30 deletions
@ -0,0 +1,32 @@ |
|||||
|
#!/usr/bin/env python3 |
||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
import base64 |
||||
|
import urllib.parse |
||||
|
|
||||
|
def urlEncode(content: str) -> str: # url encode (RFC3986) |
||||
|
return urllib.parse.quote(content, encoding = 'utf-8') |
||||
|
|
||||
|
|
||||
|
def urlDecode(content: str) -> str: # url decode (RFC3986) |
||||
|
return urllib.parse.unquote(content, encoding = 'utf-8') |
||||
|
|
||||
|
|
||||
|
def base64Encode(content: str, urlSafe: bool = True, padding: bool = False) -> str: # base64 encode |
||||
|
content = base64.b64encode(content.encode(encoding = 'utf-8')).decode(encoding = 'utf-8') |
||||
|
if urlSafe: |
||||
|
content = content.replace('+', '-') # `+` => `-` |
||||
|
content = content.replace('/', '_') # `/` => `_` |
||||
|
if padding: |
||||
|
return content |
||||
|
return content.replace('=', '') # remove `=` padding |
||||
|
|
||||
|
|
||||
|
def base64Decode(content: str) -> str: # base64 decode |
||||
|
try: |
||||
|
content = content.replace('-', '+').replace('_', '/') # compatible urlSafe |
||||
|
if len(content) % 4 in range(2, 4): # remainder -> 2 or 3 |
||||
|
content = content.ljust((len(content) // 4 + 1) * 4, '=') # increase to 4n |
||||
|
return base64.b64decode(content).decode(encoding = 'utf-8') |
||||
|
except: |
||||
|
raise RuntimeError('Invalid base64 encode') |
@ -0,0 +1,5 @@ |
|||||
|
#!/usr/bin/env python3 |
||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
from Utils.Common.Coding import urlEncode, urlDecode |
||||
|
from Utils.Common.Coding import base64Encode, base64Decode |
Loading…
Reference in new issue