Fast GMSSL Library for Python (and already built to wheels)
Project description
fastgm
Fast GMSSL Library for Python
基于Cython的快速国密算法Python实现,目前支持SM2, SM3, SM4(ECB、CBC)
介绍
基于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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastgm-whl-0.4.1.tar.gz
(271.5 kB
view hashes)
Built Distributions
Close
Hashes for fastgm_whl-0.4.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e8fe27b304398e1577f72c0c5766c81ce5b92194aebbfe965d401bbfe3d336c |
|
MD5 | 91b73a3c7c07304363079f4de0c2bc92 |
|
BLAKE2b-256 | e8251e233535ef43f839390113b1b8a810c2525db1c4c65978414365565d4b2a |
Close
Hashes for fastgm_whl-0.4.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 654de4a37a9b571240b9c951c958e277f531ba876e347c9a7ee238697c8a6a2c |
|
MD5 | 04067b11f2e91a6a43bcbb15600ece51 |
|
BLAKE2b-256 | ef1ba1408d7012cdab06e4b9807d15cd039d406d885abf1e50e10a2623693c58 |
Close
Hashes for fastgm_whl-0.4.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d28d39a4588a3b16ea9cc3ad45bce33304159cb6cf522f95e53d0dd3ecb009c |
|
MD5 | 758fc914e2094570854949544c7f4f23 |
|
BLAKE2b-256 | 812ecddc4f750b47aa79881ddf72e1e6f2c6e2adcc24548c326396499e30d8a3 |
Close
Hashes for fastgm_whl-0.4.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24d116856db0e9c85b404fd7f4037295c487f6220ad4eb91592f6dfce1b5829a |
|
MD5 | a68c6f358e56b98f83736b052c23311a |
|
BLAKE2b-256 | 5f6db4032bd965d774be6ff815c6954d152fe484ea99c10f1f693317714542b3 |
Close
Hashes for fastgm_whl-0.4.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7709ceaacb944b994b830ad341f58344e0ec5c97878c675dc4b90b5d0826784e |
|
MD5 | f412bbbea485cc1f7d7b2a0e0d79689f |
|
BLAKE2b-256 | eedcfd53fe4497837033c30b2c2343face10653f6f4e01ad7e7ff5078817c0ce |