sign_verify.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import ctypes
  2. import requests
  3. # 根据实际情况修改签名验签库名称
  4. SIGN_LIB_NAME = 'libgmsign.so'
  5. VERIFY_LIB_NAME = 'libgmsign.so'
  6. def get_sign(raw_data: str) -> (str, str):
  7. """
  8. 获取签名值
  9. :param raw_data: 原文字符串
  10. :return: tuple(str,str):(签名值base64字符串,公钥base64字符串)
  11. """
  12. # 加载共享库
  13. lib = ctypes.CDLL(SIGN_LIB_NAME)
  14. # 定义函数的参数和返回类型
  15. # 签名函数签名如下:
  16. # int sign(char *key_value, char *hash_value, char *public_key)
  17. lib.sign.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p]
  18. lib.sign.restype = ctypes.c_int
  19. key_value = raw_data.replace(" ", "").encode('utf-8')
  20. hash_value = ctypes.create_string_buffer(256) # 设置签名值输出缓冲区大小为 256 字节
  21. public_key = ctypes.create_string_buffer(256) # 设置公钥输出缓冲区大小为 256 字节
  22. result = lib.sign(key_value, hash_value, public_key)
  23. if result == 0:
  24. return hash_value.value.decode(), public_key.value.decode()
  25. else:
  26. return None, None
  27. def verify_sign(raw_data: str, sign_data: str, public_key: str) -> bool:
  28. """
  29. 验证签名
  30. :param raw_data: 原文字符串
  31. :param sign_data: 签名值base64字符串
  32. :param public_key: 公钥base64字符串
  33. :return:
  34. """
  35. # 加载共享库
  36. lib = ctypes.CDLL(VERIFY_LIB_NAME)
  37. # 验签函数签名如下:
  38. # int sign_verify(char *content, char *sign_value, char *public_key)
  39. lib.sign_verify.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p]
  40. lib.sign_verify.restype = ctypes.c_int
  41. content = raw_data.replace(" ", "").encode('utf-8')
  42. sign_value = sign_data.encode('utf-8')
  43. public_key = public_key.encode('utf-8')
  44. verify_result = lib.sign_verify(content, sign_value, public_key)
  45. verify_result = True if verify_result == 1 else False
  46. return verify_result
  47. # 远程调用方式的签名验签接口
  48. # SIGN_API_URL = "http://api.example.com/sign"
  49. # VERIFY_API_URL = "http://api.example.com/verify"
  50. # def remote_sign(raw_data: str):
  51. # payload = {"raw_data": raw_data}
  52. # response = requests.post(SIGN_API_URL, json=payload)
  53. # if response.status_code == 200:
  54. # data = response.json()
  55. # return data['sign_data'], data['public_key']
  56. # else:
  57. # print("签名失败:", response.text)
  58. # return None, None
  59. # def remote_verify(raw_data: str, sign_data: str, public_key: str):
  60. # payload = {
  61. # "raw_data": raw_data,
  62. # "sign_data": sign_data,
  63. # "public_key": public_key
  64. # }
  65. # response = requests.post(VERIFY_API_URL, json=payload)
  66. # if response.status_code == 200:
  67. # return response.json().get("verify_result", False)
  68. # else:
  69. # print("验签失败:", response.text)
  70. # return False
  71. if __name__ == '__main__':
  72. raw_data = "hello world test sign"
  73. sign_data, public_key = get_sign(raw_data)
  74. print(f"sign_data: {sign_data}\npublic_key: {public_key}")
  75. verify_result = verify_sign(raw_data, sign_data, public_key)
  76. print(f"verify_result: {verify_result}")