本项目是smcrypto的Python3实现,改库实现了国密SM3、SM2、SM4算法。
安装
pip install smcryptopy
快速开始
SM3消息摘要算法
from smcryptopy import sm3
# hash结果以16进制字符串形式返回
hash = sm3.sm3_hash(b'abc')
assert(hash == '66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0')
hash = sm3.sm3_hash_string('abc')
assert(hash == '66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0')
SM2非对称加密算法
from smcryptopy import sm2
# 生成密钥对,以64位或128位16进制字符串形式提供,公钥不包含开头的04标识
(sk, pk) = sm2.gen_keypair()
assert(len(sk) == 64)
assert(len(pk) == 128)
assert(sm2.hex_valid(sk))
assert(sm2.hex_valid(pk))
# 从私钥导出公钥
pk_ = sm2.pk_from_sk(sk)
assert(pk == pk_)
assert(sm2.privkey_valid(sk))
assert(sm2.pubkey_valid(pk))
# 签名和验签
sign = sm2.sign(b'yumeng', b'abc', sk)
verify = sm2.verify(b'yumeng', b'abc', sign, pk)
assert(verify)
# 加密和解密
enc = sm2.encrypt(b'abc', pk)
dec = sm2.decrypt(enc, sk)
assert(dec == b'abc')
# 加密和解密,但使用c1c2c3的排列方式
enc = sm2.encrypt_c1c2c3(b'abc', pk)
dec = sm2.decrypt_c1c2c3(enc, sk)
assert(dec == b'abc')
# 加密和解密,但使用asn1编码
enc = sm2.encrypt_asna1(b'abc', pk)
dec = sm2.decrypt_asna1(enc, sk)
assert(dec == b'abc')
# 加密和解密,但密文以16进制字符串形式提供
enc = sm2.encrypt_hex(b'abc', pk)
assert(sm2.hex_valid(enc))
dec = sm2.decrypt_hex(enc, sk)
assert(dec == b'abc')
# 加密和解密,但密文以base64编码形式提供
enc = sm2.encrypt_base64(b'abc', pk)
assert(sm2.base64_valid(enc))
dec = sm2.decrypt_base64(enc, sk)
assert(dec == b'abc')
# 密钥交换
id_a = b'a@example.com'
id_b = b'b@example.com'
klen = 16
sk_a = sm2.gen_keypair()[0]
sk_b = sm2.gen_keypair()[0]
a_1 = sm2.keyexchange_1ab(klen, id_a, sk_a)
b_1 = sm2.keyexchange_1ab(klen, id_b, sk_b)
a_k = sm2.keyexchange_2a(id_a, sk_a, a_1[1], b_1[0])
b_k = sm2.keyexchange_2b(id_b, sk_b, b_1[1], a_1[0])
assert(a_k == b_k)
SM4对称加密算法
from smcryptopy import sm4
# 秘钥和初始向量必须为32位16进制字符串(长度为16的字节串)
key = b'1234567812345678'
iv = b'0000000000000000'
# 加密和解密,ECB模式
enc = sm4.encrypt_ecb(b'abc', key)
dec = sm4.decrypt_ecb(enc, key)
assert(dec == b'abc')
# 加密和解密,ECB模式,密文使用base64编码形式
enc = sm4.encrypt_ecb_base64(b'abc', key)
dec = sm4.decrypt_ecb_base64(enc, key)
assert(dec == b'abc')
# 加密和解密,ECB模式,密文使用16进制字符串形式
enc = sm4.encrypt_ecb_hex(b'abc', key)
dec = sm4.decrypt_ecb_hex(enc, key)
assert(dec == b'abc')
# 加密和解密,CBC模式
enc = sm4.encrypt_cbc(b'abc', key, iv)
dec = sm4.decrypt_cbc(enc, key, iv)
assert(dec == b'abc')
# 加密和解密,CBC模式,密文使用base64编码形式
enc = sm4.encrypt_cbc_base64(b'abc', key, iv)
dec = sm4.decrypt_cbc_base64(enc, key, iv)
assert(dec == b'abc')
# 加密和解密,CBC模式,密文使用16进制字符串形式
enc = sm4.encrypt_cbc_hex(b'abc', key, iv)
dec = sm4.decrypt_cbc_hex(enc, key, iv)
assert(dec == b'abc')
Metadata
Release files for smcryptopy 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| smcryptopy-0.2.1.tar.gz | 25.1 kB | Without provenance | Details for smcryptopy-0.2.1.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| smcryptopy-0.2.1-py3-none-win_amd64.whl | Python 3 | none | Windows x86-64 | Details for smcryptopy-0.2.1-py3-none-win_amd64.whl |
| smcryptopy-0.2.1-py3-none-musllinux_1_2_x86_64.whl | Python 3 | none | Linux musl 1.2+ x86-64 | Details for smcryptopy-0.2.1-py3-none-musllinux_1_2_x86_64.whl |
| smcryptopy-0.2.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | Python 3 | none | Linux glibc 2.17+ x86-64 | Details for smcryptopy-0.2.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
Total release size: 15.1 MB
Release files / smcryptopy-0.2.1.tar.gz
| Download URL | smcryptopy-0.2.1.tar.gz |
|---|---|
| Size | 25.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e6bee0f245107914b019c033a97449a0d2f77ab5646c33756f6d1f8aab6270c4
|
|
BLAKE2b-256 checksum How to use checksums |
f14c5af0325b8dac0536b09c89329bae6d2f47a60d30a16909569962de0d1fc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Release files / smcryptopy-0.2.1-py3-none-win_amd64.whl
| Download URL | smcryptopy-0.2.1-py3-none-win_amd64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | Python 3 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
a7f844ab665eb051b38c470688d6521bac5e182911641c94010dd981f41b70e7
|
|
BLAKE2b-256 checksum How to use checksums |
a4e9fe7545321c5a3b2c244d13fa6276e2a742473bb56f7e7d7661603bb31b08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Release files / smcryptopy-0.2.1-py3-none-musllinux_1_2_x86_64.whl
| Download URL | smcryptopy-0.2.1-py3-none-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 6.8 MB |
| Tags | Linux musl 1.2+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
404c8b7fef2445842ecb2f9cb1771844f4d5da0c281f0f2fd724711d2aee63b9
|
|
BLAKE2b-256 checksum How to use checksums |
44ebd0c8c13927ead1274ddaa192bcfb5e687136b9b92eeb86ac188e5551b158
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Release files / smcryptopy-0.2.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | smcryptopy-0.2.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 6.7 MB |
| Tags | Linux glibc 2.17+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
a3f910b76119e68e89fc5111b4e4d36a5bfefa478f640a336eb11fede11b0f90
|
|
BLAKE2b-256 checksum How to use checksums |
78800697e6b56c11f9c563c6fe8f8946e4cec4100290325352d57ca95dca4a4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Security for smcryptopy 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.