TassApi.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. import requests
  3. # 辅助类
  4. # 调用JAVA编写的密评接口
  5. API_ROOT = "http://127.0.0.1:8052/tass"
  6. # 隐私信息加密
  7. def CipherEncrypt(data: str) -> str:
  8. resp = __post_data(API_ROOT + "/CipherEncrypt", data)
  9. # print("隐私信息加密 >>>", data, resp)
  10. return resp
  11. # 隐私信息解密
  12. def CipherDecrypt(data: str) -> str:
  13. resp = __post_data(API_ROOT + "/CipherDecrypt", data)
  14. # print("隐私信息解密 >>>", data, resp)
  15. return resp
  16. # 敏感信息数据加密
  17. def TransparentEnc(data: str) -> str:
  18. resp = __post_data(API_ROOT + "/TransparentEnc", data)
  19. # print("敏感信息数据加密 >>>", data, resp)
  20. return resp
  21. # 敏感信息数据解密
  22. def TransparentDec(data: str) -> str:
  23. resp = __post_data(API_ROOT + "/TransparentDec", data)
  24. # print("敏感信息数据解密 >>>", data, resp)
  25. return resp
  26. # 计算HMAC
  27. def Hmac(data: str) -> str:
  28. return __post_data(API_ROOT + "/Hmac", data)
  29. # 验证HMAC
  30. def HmacVerify(sign_data: str, sign_mac: str) -> str:
  31. data = {}
  32. data['sign_data'] = sign_data
  33. data['sign_mac'] = sign_mac
  34. headers = {'Content-Type': 'application/json;charset=UTF-8'}
  35. response = requests.post(url=API_ROOT + "/HmacVerify", headers=headers, json=data, timeout=600)
  36. if response.status_code == 200:
  37. result = response.json()
  38. # print(result)
  39. if result['errcode'] == 0:
  40. return result['data'] == "success"
  41. return False
  42. # 完成P7的签名验证
  43. def verifyP7Sign(p7SignData: str, p7SignValue: str) -> any:
  44. data = {}
  45. data['p7SignData'] = p7SignData
  46. data['p7SignValue'] = p7SignValue
  47. headers = {'Content-Type': 'application/json;charset=UTF-8'}
  48. response = requests.post(url=API_ROOT + "/verifyP7Sign", headers=headers, json=data, timeout=600)
  49. if response.status_code == 200:
  50. result = response.json()
  51. print(result)
  52. if result['errcode'] == 0:
  53. return result['data']
  54. return None
  55. # 公用POST方法
  56. def __post_data(api_url: str, data: str):
  57. headers = {'content-type': 'charset=utf8'}
  58. response = requests.post(url=api_url, headers=headers, data=data.encode('UTF-8'), timeout=600)
  59. if response.status_code == 200:
  60. result = response.json()
  61. # print(result)
  62. if result['errcode'] == 0:
  63. return result['data']
  64. return ""