2022-08-02 10:31:08 +00:00
|
|
|
from gmssl import sm3, func
|
|
|
|
|
|
|
|
from django.contrib.auth.hashers import PBKDF2PasswordHasher
|
|
|
|
|
|
|
|
|
|
|
|
class Hasher:
|
|
|
|
name = 'sm3'
|
2022-10-14 10:59:28 +00:00
|
|
|
block_size = 64
|
|
|
|
digest_size = 32
|
2022-08-02 10:31:08 +00:00
|
|
|
|
2022-10-14 11:40:51 +00:00
|
|
|
def __init__(self, data):
|
|
|
|
self.__data = data
|
2022-08-02 10:31:08 +00:00
|
|
|
|
|
|
|
def hexdigest(self):
|
2022-10-14 11:40:51 +00:00
|
|
|
return sm3.sm3_hash(func.bytes_to_list(self.__data))
|
2022-08-02 10:31:08 +00:00
|
|
|
|
2022-10-14 10:59:28 +00:00
|
|
|
def digest(self):
|
|
|
|
return bytes.fromhex(self.hexdigest())
|
|
|
|
|
2022-08-02 10:31:08 +00:00
|
|
|
@staticmethod
|
2022-10-14 10:59:28 +00:00
|
|
|
def hash(msg=b''):
|
2022-08-02 10:31:08 +00:00
|
|
|
return Hasher(msg)
|
|
|
|
|
2022-10-14 11:40:51 +00:00
|
|
|
def update(self, data):
|
|
|
|
self.__data += data
|
2022-10-14 10:59:28 +00:00
|
|
|
|
|
|
|
def copy(self):
|
2022-10-14 11:40:51 +00:00
|
|
|
return Hasher(self.__data)
|
2022-10-14 10:59:28 +00:00
|
|
|
|
2022-08-02 10:31:08 +00:00
|
|
|
|
|
|
|
class PBKDF2SM3PasswordHasher(PBKDF2PasswordHasher):
|
|
|
|
algorithm = "pbkdf2_sm3"
|
|
|
|
digest = Hasher.hash
|
|
|
|
|