资源简介
压缩包里面包含了sm3算法代码,sm2需要调用sm3杂凑算法来求hash值,里面包括了kdf密钥派生函数,和一些字符串进制转换函数,都放在sm2头文件里面,s m2包含了一些必要的数字签名和验签,加解密算法,有注释,对应理解。
代码片段和文件信息
#!/user/bin/python
# -*-coding: utf-8 -*-
from random import choice
import SM3
# 选择素域,设置椭圆曲线参数
sm2_N = int(‘FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123‘ 16)
sm2_P = int(‘FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF‘ 16)
sm2_G = ‘32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0‘ # G点
sm2_a = int(‘FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC‘16)
sm2_b = int(‘28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93‘16)
sm2_a_3 = (sm2_a + 3) % sm2_P # 倍点用到的中间值
Fp = 256
def get_random_str(strlen): #随机产生字符串
letterlist = [‘0‘‘1‘‘2‘‘3‘‘4‘‘5‘‘6‘‘7‘‘8‘‘9‘‘a‘‘b‘‘c‘‘d‘‘e‘‘f‘]
str = ‘‘
for i in range(strlen):
a = choice(letterlist)
str = ‘%s%s‘ % (stra)
return str
def kG(k Pointlen_para): # kP运算
Point = ‘%s%s‘ % (Point ‘1‘)
mask_str = ‘8‘
for i in range(len_para-1):
mask_str += ‘0‘
# print(mask_str)
mask = int(mask_str 16)
Temp = Point
flag = False
for n in range(len_para * 4):
if (flag):
Temp = DoublePoint(Templen_para)
if (k & mask) != 0:
if (flag):
Temp = AddPoint(Temp Pointlen_para)
else:
flag = True
Temp = Point
k = k << 1
return ConvertJacb2Nor(Templen_para)
def DoublePoint(Pointlen_para): # 倍点
l = len(Point)
len_2 = 2 * len_para
if l< len_para*2:
return None
else:
x1 = int(Point[0:len_para] 16)
y1 = int(Point[len_para:len_2] 16)
if l == len_2:
z1 = 1
else:
z1 = int(Point[len_2:] 16)
T6 = (z1 * z1) % sm2_P
T2 = (y1 * y1) % sm2_P
T3 = (x1 + T6) % sm2_P
T4 = (x1 - T6) % sm2_P
T1 = (T3 * T4) % sm2_P
T3 = (y1 * z1) % sm2_P
T4 = (T2 * 8) % sm2_P
T5 = (x1 * T4) % sm2_P
T1 = (T1 * 3) % sm2_P
T6 = (T6 * T6) % sm2_P
T6 = (sm2_a_3 * T6) % sm2_P
T1 = (T1 + T6) % sm2_P
z3 = (T3 + T3) % sm2_P
T3 = (T1 * T1) % sm2_P
T2 = (T2 * T4) % sm2_P
x3 = (T3 - T5) % sm2_P
if (T5 % 2) == 1:
T4 = (T5 + ((T5 + sm2_P) >> 1) - T3) % sm2_P
else:
T4 = (T5 + (T5 >> 1) - T3) % sm2_P
T1 = (T1 * T4) % sm2_P
y3 = (T1 - T2) % sm2_P
form = ‘%%0%dx‘ % len_para
form = form * 3
return form % (x3 y3 z3)
def AddPoint(P1 P2len_para): # 点加函数,P2点为仿射坐标即z=1,P1为Jacobian加重射影坐标
len_2 = 2 * len_para
l1 = len(P1)
l2 = len(P2)
if (l1 < len_2) or (l2 < len_2):
return None
else:
X1 = int(P1[0:len_para] 16)
Y1 = int(P1[len_para:len_2] 16)
if (l1 == len_2):
Z1 = 1
else:
Z1 = in
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5497 2019-01-05 22:10 SM3.py
目录 0 2019-01-06 22:40 __MACOSX\
文件 172 2019-01-05 22:10 __MACOSX\._SM3.py
文件 8280 2019-01-06 01:51 sm2.py
文件 228 2019-01-06 01:51 __MACOSX\._sm2.py
- 上一篇:python 识别物体跟踪
- 下一篇:决策树算法的PPT与实现代码
评论
共有 条评论