30 lines
986 B
Python
30 lines
986 B
Python
|
import base64
|
||
|
|
||
|
|
||
|
def base64_encode(data, output=None):
|
||
|
"""
|
||
|
:param data: Bytes or String or FileStream.
|
||
|
:param output: Output file stream, when the data parameter is the file stream valid.
|
||
|
:return: What type of data entering returns the same data type.
|
||
|
"""
|
||
|
if isinstance(data, bytes):
|
||
|
return base64.b64encode(data)
|
||
|
if isinstance(data, str):
|
||
|
return base64.b64encode(bytes(data, encoding='utf-8')).decode()
|
||
|
else:
|
||
|
return base64.encode(data, output)
|
||
|
|
||
|
|
||
|
def base64_decode(data, output=None):
|
||
|
"""
|
||
|
:param data: Bytes or String or FileStream.
|
||
|
:param output: Output file stream, when the data parameter is the file stream valid.
|
||
|
:return: What type of data entering returns the same data type.
|
||
|
"""
|
||
|
if isinstance(data, bytes):
|
||
|
return base64.b64decode(data)
|
||
|
if isinstance(data, str):
|
||
|
return base64.b64decode(bytes(data, encoding='utf-8')).decode()
|
||
|
else:
|
||
|
return base64.decode(data, output)
|