Skip to main content

fastgm

Fast GMSSL Library for Python

基于Cython的快速国密算法Python实现,目前支持SM2, SM3, SM4(ECB、CBC)

pyver

test cov

download

介绍

基于Cython的快速国密算法Python实现,目前支持SM2, SM3, SM4(ECB、CBC)

安装教程

pip install fastgm

使用说明

SM2

SM2是国家密码管理局发布的椭圆曲线公钥密码算法。对标RSA

使用方法:

  • 生成秘钥
from fastgm import SM2

sk, pk = SM2.generate_key()  # sk为私钥,pk为公钥,均为hex格式

  • 使用公钥加密
from fastgm import SM2

pk = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A6994B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207' # 公钥,Hex格式

data = b'helloworld' # 待加密内容,格式为bytes数组

sm2 = SM2() # 初始化
enc = sm2.encrypt(pk, data) # 运行加密算法

  • 使用私钥解密
from fastgm import SM2

sk = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5'  # 私钥

sm2 = SM2() # 初始化

dec = sm2.decrypt(sk, enc) # 运行解密算法,enc为加密内容

SM3

SM3密码杂凑算法是中国国家密码管理局2010年公布的中国商用密码杂凑算法标准。对标MD5

使用方法:

from fastgm import SM3
data = b'helloworld'  # 待哈希bytes数组
h = SM3().hash(data)  # 哈希运算

SM4

SM4.0(原名SMS4.0)是中华人民共和国政府采用的一种分组密码标准。对标AES。

使用方法:

  • 生成秘钥
from fastgm import SM4

key = SM4.generate_key() # key为16位bytes数组
  • Zero Padding ECB加解密
from fastgm import SM4

key = b'1' * 16  # key为16位bytes数组
data = b'helloworld' # 待加密内容

sm4 = SM4(key, padding='pkcs7')  # 初始化, padding可选zero, pkcs7。gmssl-python库默认为pkcs7

enc = sm4.encrypt_ecb(data) # 加密
dec = sm4.decrypt_ecb(enc)  # 解密

  • PKCS7 Padding CBC加解密
from fastgm import SM4

key = b'1' * 16  # key为16位bytes数组
iv = b'2' * 16  # 初始化向量, iv为16位bytes数组
data = b'helloworld' # 待加密内容

sm4 = SM4(key, padding='pkcs7')  # 初始化, padding可选zero, pkcs7。gmssl-python库默认为pkcs7

enc = sm4.encrypt_cbc(iv, data) # 加密
dec = sm4.decrypt_cbc(iv, enc)  # 解密

benchmark

以SM4为例,加密1024个helloworld,共计10240个字符,只需约0.3ms。纯Python实现的gmssl-python需要约171ms。

fastgm(本方法) gmssl-python crytography
sm2 29.9 ms ± 1.39 ms 1.12 s ± 22.3 ms 未提供
sm3 802 µs ± 21.4 µs 111 ms ± 10.4 ms 未提供
sm4 299 µs ± 6.61 µs 170 ms ± 1.22 ms 283 µs ± 6.57 µs

测试环境,Intel 8265U,WSL2。

参考

[1] https://github.com/guanzhi/GmSSL
[2] https://github.com/duanhongyi/gmssl
[3] https://github.com/gongxian-ding/gmssl-python
[4] https://github.com/pyca/cryptography

Metadata

Release files for fastgm-whl 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastgm-whl 0.4.1
File Size Uploaded
fastgm-whl-0.4.1.tar.gz 271.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for fastgm-whl 0.4.1
File
fastgm_whl-0.4.1-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
fastgm_whl-0.4.1-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
fastgm_whl-0.4.1-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
fastgm_whl-0.4.1-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
fastgm_whl-0.4.1-cp36-cp36m-win_amd64.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-64 Details

Total release size: 1.1 MB

Release files / fastgm-whl-0.4.1.tar.gz

Download URL fastgm-whl-0.4.1.tar.gz
Size 271.5 kB
Tags Source
SHA-256 checksum
How to use checksums
80a6ea8da74421be10dfac3b2ec5ab3a37518988660795f939c6be1b6328bef2
BLAKE2b-256 checksum
How to use checksums
dc44dfab41b93e054352737012335ace5c5bb93b62bf2337c66a7860370a7d98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / fastgm_whl-0.4.1-cp310-cp310-win_amd64.whl

Download URL fastgm_whl-0.4.1-cp310-cp310-win_amd64.whl
Size 160.9 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
9e8fe27b304398e1577f72c0c5766c81ce5b92194aebbfe965d401bbfe3d336c
BLAKE2b-256 checksum
How to use checksums
e8251e233535ef43f839390113b1b8a810c2525db1c4c65978414365565d4b2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / fastgm_whl-0.4.1-cp39-cp39-win_amd64.whl

Download URL fastgm_whl-0.4.1-cp39-cp39-win_amd64.whl
Size 160.9 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
654de4a37a9b571240b9c951c958e277f531ba876e347c9a7ee238697c8a6a2c
BLAKE2b-256 checksum
How to use checksums
ef1ba1408d7012cdab06e4b9807d15cd039d406d885abf1e50e10a2623693c58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / fastgm_whl-0.4.1-cp38-cp38-win_amd64.whl

Download URL fastgm_whl-0.4.1-cp38-cp38-win_amd64.whl
Size 160.8 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
4d28d39a4588a3b16ea9cc3ad45bce33304159cb6cf522f95e53d0dd3ecb009c
BLAKE2b-256 checksum
How to use checksums
812ecddc4f750b47aa79881ddf72e1e6f2c6e2adcc24548c326396499e30d8a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / fastgm_whl-0.4.1-cp37-cp37m-win_amd64.whl

Download URL fastgm_whl-0.4.1-cp37-cp37m-win_amd64.whl
Size 159.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
24d116856db0e9c85b404fd7f4037295c487f6220ad4eb91592f6dfce1b5829a
BLAKE2b-256 checksum
How to use checksums
5f6db4032bd965d774be6ff815c6954d152fe484ea99c10f1f693317714542b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / fastgm_whl-0.4.1-cp36-cp36m-win_amd64.whl

Download URL fastgm_whl-0.4.1-cp36-cp36m-win_amd64.whl
Size 179.9 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
7709ceaacb944b994b830ad341f58344e0ec5c97878c675dc4b90b5d0826784e
BLAKE2b-256 checksum
How to use checksums
eedcfd53fe4497837033c30b2c2343face10653f6f4e01ad7e7ff5078817c0ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.4.1 This release

6 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page