python 实现sm3国密算法

from gmssl import sm3
import hmac


def encrypt(src: str):
    """
    国密SM3加密
    :param src: 原始字符串
    :return: 哈希值
    """
    return sm3_hash(src)


def sm3_hash(src: str):
    msg_list = [i for i in bytes(src.encode('utf-8'))]
    return sm3.sm3_hash(msg_list).upper()


def verify(src: str, sm3_hex_str: str):
    new_hex_str = sm3_hash(src)
    return new_hex_str == sm3_hex_str


def encrypt_with_key(src: str, key: str):
    """
    SM3+HMAC加密
    :param src: 原始字符串
    :param key: HMAC密钥
    :return: 哈希值
    """
    return hmac_hash(src, key, 'SM3')


def hmac_hash(src: str, key: str, mod: str):
    return hmac.new(key.encode('utf-8'), src.encode('utf-8'), mod).hexdigest().upper()


def verify_with_key(src: str, sm3_hex_str: str, key: str):
    new_hex_str = hmac_hash(src, key, 'SM3')
    return new_hex_str == sm3_hex_str


if __name__ == "__main__":
    src = "测试字符串"
    key = "ABCDEF"

    hash_str = encrypt(src)
    flag = verify(src, hash_str)
    print(hash_str)
    print(flag)

    hash_str = encrypt_with_key(src, key)
    flag = verify_with_key(src, hash_str, key)
    print(hash_str)
    print(flag)

以下是Python实现sm3算法的代码示例: ```python import struct import hashlib class SM3Hash: def __init__(self): self.iv = [0x7380166F, 0x4914B2B9, 0x172442D7, 0xDA8A0600, 0xA96F30BC, 0x163138AA, 0xE38DEE4D, 0xB0FB0E4E] self.tj = [] for i in range(0, 16): self.tj.append(0x79CC4519) for i in range(16, 64): self.tj.append(0x7A879D8A) self.A, self.B, self.C, self.D, self.E, self.F, self.G, self.H = self.iv[0], self.iv[1], self.iv[2], self.iv[3], self.iv[4], self.iv[5], self.iv[6], self.iv[7] def _CF(self, X): W = [] for i in range(0, 16): W.append(X[i]) for i in range(16, 68): W.append(self._P1(W[i-16] ^ W[i-9] ^ (self._ROTATE_LEFT(W[i-3], 15))) ^ (self._ROTATE_LEFT(W[i-13], 7)) ^ W[i-6]) for i in range(68, 64): W.append(self._P1(W[i-16] ^ W[i-9] ^ (self._ROTATE_LEFT(W[i-3], 15))) ^ (self._ROTATE_LEFT(W[i-13], 7)) ^ W[i-6] ^ W[i-64]) A, B, C, D, E, F, G, H = self.A, self.B, self.C, self.D, self.E, self.F, self.G, self.H for i in range(0, 64): SS1 = self._ROTATE_LEFT((self._ROTATE_LEFT(A, 12) + E + self._ROTATE_LEFT(self.tj[i], i % 32)) & 0xFFFFFFFF, 7) SS2 = SS1 ^ self._ROTATE_LEFT(A, 12) TT1 = (self._FFj(A, B, C, i) + D + SS2 + W[i]) & 0xFFFFFFFF TT2 = (self._GGj(E, F, G, i) + H + SS1 + W[i]) & 0xFFFFFFFF D = C C = self._ROTATE_LEFT(B, 9) B = A A = TT1 H = G G = self._ROTATE_LEFT(F, 19) F = E E = self._P0(TT2) self.A = (self.A + A) & 0xFFFFFFFF self.B = (self.B + B) & 0xFFFFFFFF self.C = (self.C + C) & 0xFFFFFFFF self.D = (self.D + D) & 0xFFFFFFFF self.E = (self.E + E) & 0xFFFFFFFF self.F = (self.F + F) & 0xFFFFFFFF self.G = (self.G + G) & 0xFFFFFFFF self.H = (self.H + H) & 0xFFFFFFFF def _P0(self, X): return X ^ self._ROTATE_LEFT(X, 9) ^ self._ROTATE_LEFT(X, 17) def _P1(self, X): return X ^ self._ROTATE_LEFT(X, 15) ^ self._ROTATE_LEFT(X, 23) def _FFj(self, X, Y, Z, j): if j >= 0 and j <= 15: return X ^ Y ^ Z else: return (X & Y) | (X & Z) | (Y & Z) def _GGj(self, X, Y, Z, j): if j >= 0 and j <= 15: return X ^ Y ^ Z else: return (X & Y) | (~X & Z) def _ROTATE_LEFT(self, x, n): return (((x) << (n)) & 0xFFFFFFFF) | ((x) >> (32-(n))) def _padding(self, data): length = len(data) * 8 data += b'\x80' data += b'\x00' * (((56 - (length + 8) % 64) % 64) - 1) data += struct.pack('>Q', length) return data def update(self, data): data = self._padding(data) for i in range(0, len(data), 64): block = data[i:i+64] X = [] for j in range(0, 16): X.append(struct.unpack('>I', block[j*4:j*4+4])[0]) self._CF(X) def digest(self): return struct.pack('>IIIIIIII', self.A, self.B, self.C, self.D, self.E, self.F, self.G, self.H) def hexdigest(self): return self.digest().hex() def sm3(data): h = SM3Hash() h.update(data) return h.hexdigest() message = b'This is a test message.' hash_value = sm3(message) print(hash_value) ``` 需要注意的是,在使用时需要将待哈希的数据转换为字节串传入`sm3`函数中。另外,在实现过程中,需要使用一些位运算的技巧,详情请参考SM3算法的相关文献。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值