Jingxun
8 months ago
1 changed files with 32 additions and 0 deletions
@ -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…
Reference in new issue