Skip to content

Commit

Permalink
generate key
Browse files Browse the repository at this point in the history
  • Loading branch information
wptoux committed Oct 22, 2021
1 parent 0957af1 commit 84b6382
Show file tree
Hide file tree
Showing 7 changed files with 769 additions and 497 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ SM2是国家密码管理局发布的椭圆曲线公钥密码算法。对标RSA

使用方法:

+ 生成秘钥
```
from fastgm import SM2
sk, pk = SM2.generate_key() # sk为私钥,pk为公钥,均为hex格式
```

+ 使用公钥加密

```
Expand Down Expand Up @@ -62,6 +70,13 @@ SM4.0(原名SMS4.0)是中华人民共和国政府采用的一种分组密码

使用方法:

+ 生成秘钥
```
from fastgm import SM4
key = SM4.generate_key() # key为16位bytes数组
```

+ Zero Padding ECB加解密
```
from fastgm import SM4
Expand Down
59 changes: 37 additions & 22 deletions notebook/sm2.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def no_cythonize(extensions, **_ignore):

setup(
name="fastgm",
version="0.1.0", # expected format is one of x.y.z.dev0, or x.y.z.rc1 or x.y.z (no to dashes, yes to dots)
version="0.1.1", # expected format is one of x.y.z.dev0, or x.y.z.rc1 or x.y.z (no to dashes, yes to dots)
author="wptoux",
author_email="[email protected]",
description="Fast GMSSL Library for Python",
Expand Down
21 changes: 21 additions & 0 deletions src/fastgm/sm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ def decrypt(sk, data, mode='C1C3C2'):
else:
return None


def generate_key():
"""
return: sk, pk
"""

k = os.urandom(32)
k = int(k.hex(), 16)

pk = kP(k, G)

return ('%064x'% k).upper(), point2hex(pk).upper()


class SM2:
def __init__(self, mode='C1C3C2'):
"""
Expand All @@ -216,6 +230,13 @@ def __init__(self, mode='C1C3C2'):

self._mode = mode

@classmethod
def generate_key(cls):
"""
return: 私钥、公钥组成的tuple
"""
return generate_key()

def encrypt(self, pk, data):
"""
pk: 公钥, hex编码
Expand Down
Loading

0 comments on commit 84b6382

Please sign in to comment.