Browse Source

added gm lib to project

dev
Jingxun 8 months ago
parent
commit
2af33b28a1
  1. 32
      frontend/utils/gmssl_pack.py

32
frontend/utils/gmssl_pack.py

@ -0,0 +1,32 @@
# encoding: utf-8
"""
@author: Qiancj
@contact: qiancj@risenenergy.com
@file: gmssl_pack
@create-time: 2023-09-21 09:22
@description: The new python script
"""
from gmssl.sm2 import CryptSM2
from gmssl.sm3 import sm3_hash
class GmEncryptDecrypt:
"""
国密sm4加解密
"""
def __init__(self, sm2_private_key=None, sm2_public_key=None, mode=1):
self.__sm2_endecrpt = CryptSM2(private_key=sm2_private_key, public_key=sm2_public_key, mode=mode)
def sm2_encrypt(self, text):
data = text.encode() if isinstance(text, str) else text
return self.__sm2_endecrpt.encrypt(data).hex()
def sm2_decrypt(self, msg):
data = bytes.fromhex(msg)
return self.__sm2_endecrpt.decrypt(data).decode()
@staticmethod
def sm3_encrypt(text):
return sm3_hash(bytearray(text, encoding="utf-8"))
Loading…
Cancel
Save