SM3算法 (python)
生活随笔
收集整理的這篇文章主要介紹了
SM3算法 (python)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
初步學(xué)習(xí)python可能有些語法不完善;
根據(jù)國密SM3文檔編寫,可能有誤,請指正!
國密局SM3文檔地址:302a3ada057c4a73830536d03e683110.pdf (sca.gov.cn)class SM3(object):def __init__(self, hex_string):self.hex_string = hex_stringself.bin_string = bin(int(hex_string, 16))[2:].zfill(4*len(hex_string))self.IV = '7380166f4914b2b9172442d7da8a0600a96f30bc163138aae38dee4db0fb0e4e'self.T = ['79cc4519'] * 16 + ['7a879d8a'] * 48self.B = []self.W = [''] * 68self.W_ = [''] * 64@staticmethod# 循環(huán)左移def shift_to_left(string, num):return string[num % len(string):] + string[:num % len(string)]@staticmethod# 異或def x_o_r(string_to_number_list):result = 0for i in range(len(string_to_number_list)):result = result ^ string_to_number_list[i]return bin(result)[2:]@staticmethod# 取反def n_o_t(string):op1 = '1' * len(string)op2 = stringresult = bin(int(op1, 2) - int(op2, 2))[2:].zfill(len(string))return result@staticmethoddef ff(j, x, y, z):if 0 <= j <= 15:result = SM3.x_o_r([int(x, 2), int(y, 2), int(z, 2)]).zfill(32)else:result = bin((int(x, 2) & int(y, 2)) | (int(x, 2) & int(z, 2)) | (int(y, 2) & int(z, 2)))[2:].zfill(32)return result@staticmethoddef gg(j, x, y, z):if 0 <= j <= 15:result = SM3.x_o_r([int(x, 2), int(y, 2), int(z, 2)]).zfill(32)else:result = bin((int(x, 2) & int(y, 2)) | (int(SM3.n_o_t(x), 2) & int(z, 2)))[2:].zfill(32)return result@staticmethoddef p0(x):result = SM3.x_o_r([int(x, 2), int(SM3.shift_to_left(x, 9), 2), int(SM3.shift_to_left(x, 17), 2)]).zfill(32)return result@staticmethoddef p1(x):result = SM3.x_o_r([int(x, 2), int(SM3.shift_to_left(x, 15), 2), int(SM3.shift_to_left(x, 23), 2)]).zfill(32)return resultdef cf(self, j, v, b):abcdefgh = [''] * 8for i in range(8):abcdefgh[i] = v[i*32:i*32+32]ss1 = SM3.shift_to_left(bin(int(SM3.shift_to_left(abcdefgh[0], 12), 2) + int(abcdefgh[4], 2) +int(SM3.shift_to_left(self.T[j], j % 32), 2))[2:].zfill(32)[-32:], 7)ss2 = SM3.x_o_r([int(ss1, 2), int(SM3.shift_to_left(abcdefgh[0], 12), 2)]).zfill(32)tt1 = bin(int(SM3.ff(j, abcdefgh[0], abcdefgh[1], abcdefgh[2]), 2) + int(abcdefgh[3], 2) + int(ss2, 2) +int(self.W_[j], 2))[2:].zfill(32)[-32:]tt2 = bin(int(SM3.gg(j, abcdefgh[4], abcdefgh[5], abcdefgh[6]), 2) + int(abcdefgh[7], 2) + int(ss1, 2) +int(self.W[j], 2))[2:].zfill(32)[-32:]abcdefgh[3] = abcdefgh[2]abcdefgh[2] = SM3.shift_to_left(abcdefgh[1], 9)abcdefgh[1] = abcdefgh[0]abcdefgh[0] = tt1abcdefgh[7] = abcdefgh[6]abcdefgh[6] = SM3.shift_to_left(abcdefgh[5], 19)abcdefgh[5] = abcdefgh[4]abcdefgh[4] = SM3.p0(tt2)result = abcdefgh[0] + abcdefgh[1] + abcdefgh[2] + abcdefgh[3] + \abcdefgh[4] + abcdefgh[5] + abcdefgh[6] + abcdefgh[7]return resultdef hash(self):sm3 = None# 1. 填充l = len(self.bin_string)k = 0while (l+1+k) % 512 != 448:k += 1self.bin_string += '1' + '0' * k + bin(l)[2:].zfill(64)# 2. 迭代n = (l+1+k+64) // 512for i in range(n):self.B.append(self.bin_string[i*512:i*512+512])# Tfor j in range(64):self.T[j] = bin(int(self.T[j], 16))[2:].zfill(32)# 計(jì)算 W 和 W‘for i in range(n):# Wfor j in range(68):if 0 <= j <= 15:self.W[j] = self.B[i][j*32:j*32+32]else:op1 = SM3.p1(SM3.x_o_r([int(self.W[j-16], 2), int(self.W[j-9], 2),int(SM3.shift_to_left(self.W[j-3], 15), 2)]).zfill(32))op2 = SM3.shift_to_left(self.W[j-13], 7)op3 = self.W[j-6]op4 = SM3.x_o_r([int(op1, 2), int(op2, 2), int(op3, 2)]).zfill(32)self.W[j] = op4# W’for j in range(64):op1 = SM3.x_o_r([int(self.W[j], 2), int(self.W[j+4], 2)]).zfill(32)self.W_[j] = op1# 64輪迭代v = bin(int(self.IV, 16))[2:].zfill(256)for j in range(64):v = SM3.cf(self, j, v, self.B)hex_string = hex(int(v, 2))[2:].zfill(64)sm3 = hex(int(hex_string, 16) ^ int(self.IV, 16))[2:].zfill(64)self.IV = sm3return sm3test1 = SM3('616263').hash()
print(test1)
test2 = SM3('61626364616263646162636461626364616263646162636461626364616263646162636461626364616263646162636461626364616263646162636461626364').hash()
print(test2)
總結(jié)
以上是生活随笔為你收集整理的SM3算法 (python)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux设备驱动模型架构分析 一
- 下一篇: 黑客网络安全扫描工具