整理一些常用的加密解密方法。
import base64
import hashlib
from urllib.parse import quote, unquote
# CryptoJS.SHA256()加密
def CryptoJS_SHA256(text):
if text != "":
algorithm = hashlib.sha256()
algorithm.update(text.encode(encoding='UTF-8'))
return algorithm.hexdigest()
else:
return text
# encodeURI()加密
def encodeURI(text):
if text != "":
text_encodeURI = quote(text, safe=",/?:@&=+$#", encoding="utf-8")
return text_encodeURI
else:
return text
# encodeURIComponent()加密
def encodeURIComponent(text):
if text != "":
text_encodeURI = quote(text, encoding="utf-8")
return text_encodeURI
else:
return text
# decodeURI()解密
def decodeURI(text):
if text != "":
text_decodeURI = unquote(text, encoding='utf-8')
return text_decodeURI
else:
return text
# btoa()加密
def btoa(text):
if text != "":
text_btoa = base64.b64encode(text.encode()).decode()
return text_btoa
else:
return text
# btoa()解密
def atob(text):
if text != "":
text_atob = base64.b64decode(text).decode()
return text_atob
else:
return text