# -*- coding: utf-8 -*- import requests # 辅助类 # 调用JAVA编写的密评接口 API_ROOT = "http://127.0.0.1:8052/tass" # 隐私信息加密 def CipherEncrypt(data: str) -> str: resp = __post_data(API_ROOT + "/CipherEncrypt", data) # print("隐私信息加密 >>>", data, resp) return resp # 隐私信息解密 def CipherDecrypt(data: str) -> str: resp = __post_data(API_ROOT + "/CipherDecrypt", data) # print("隐私信息解密 >>>", data, resp) return resp # 敏感信息数据加密 def TransparentEnc(data: str) -> str: resp = __post_data(API_ROOT + "/TransparentEnc", data) # print("敏感信息数据加密 >>>", data, resp) return resp # 敏感信息数据解密 def TransparentDec(data: str) -> str: resp = __post_data(API_ROOT + "/TransparentDec", data) # print("敏感信息数据解密 >>>", data, resp) return resp # 计算HMAC def Hmac(data: str) -> str: return __post_data(API_ROOT + "/Hmac", data) # 验证HMAC def HmacVerify(sign_data: str, sign_mac: str) -> str: data = {} data['sign_data'] = sign_data data['sign_mac'] = sign_mac headers = {'Content-Type': 'application/json;charset=UTF-8'} response = requests.post(url=API_ROOT + "/HmacVerify", headers=headers, json=data, timeout=600) if response.status_code == 200: result = response.json() # print(result) if result['errcode'] == 0: return result['data'] == "success" return False # 完成P7的签名验证 def verifyP7Sign(p7SignData: str, p7SignValue: str) -> any: data = {} data['p7SignData'] = p7SignData data['p7SignValue'] = p7SignValue headers = {'Content-Type': 'application/json;charset=UTF-8'} response = requests.post(url=API_ROOT + "/verifyP7Sign", headers=headers, json=data, timeout=600) if response.status_code == 200: result = response.json() print(result) if result['errcode'] == 0: return result['data'] return None # 公用POST方法 def __post_data(api_url: str, data: str): headers = {'content-type': 'charset=utf8'} response = requests.post(url=api_url, headers=headers, data=data.encode('UTF-8'), timeout=600) if response.status_code == 200: result = response.json() # print(result) if result['errcode'] == 0: return result['data'] return ""