Registry / auth-security / gmssl
library3.2.2pypypi✓ verified 84d ago

GMSSL is a pure-Python library providing implementations of China's national cryptographic algorithms: SM2 (elliptic curve cryptography), SM3 (hash function), and SM4 (block cipher). It is currently at version 3.2.2 and is actively maintained, with releases typically following bug fixes or minor enhancements.

pip install gmssl
INSTALL
IMPORT
SIG · GMSSL
G
gmssl
auth-securitypythonv3.2.2
Install
2.1s avg
Import
10ms
Disk
25MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.2.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.011s · 26.4MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.1s · import 0.010s · 27MB
25MB installed
● package 25MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sm2
from gmssl import sm2
sm3
from gmssl import sm3
sm4
from gmssl import sm4

Encrypts and decrypts data using SM4 in both ECB and CBC modes, demonstrating the required `bytes` input for keys, IVs, and data. The library automatically handles PKCS7 padding for `encrypt` and removes it for `decrypt`.

from gmssl import sm4 # SM4 operates on 16-byte blocks with a 16-byte key. key = b'this is a 16-byt' # Must be 16 bytes iv = b'this is a 16-byt' # Must be 16 bytes for CBC mode # Example for ECB mode (no IV needed, but data must be padded/block aligned) # gmssl.sm4.encrypt/decrypt handles PKCS7 padding automatically if not block-aligned. original_data_ecb = b'Hello world! This is a test message for SM4 ECB mode.' encrypted_ecb = sm4.encrypt(key, original_data_ecb) decrypted_ecb = sm4.decrypt(key, encrypted_ecb) assert original_data_ecb == decrypted_ecb print(f"SM4 ECB: Original: {original_data_ecb}, Decrypted: {decrypted_ecb}") # Example for CBC mode (IV required) original_data_cbc = b'Another test message for SM4 CBC mode, with an IV.' encrypted_cbc = sm4.encrypt(key, original_data_cbc, iv) decrypted_cbc = sm4.decrypt(key, encrypted_cbc, iv) assert original_data_cbc == decrypted_cbc print(f"SM4 CBC: Original: {original_data_cbc}, Decrypted: {decrypted_cbc}") print("SM4 encryption/decryption examples completed successfully.")
gmssl --version
Debug
Known issues
gotchaAll cryptographic inputs (keys, IVs, data) and outputs must be `bytes`. Passing `str` will result in a `TypeError`.
fix
Ensure all string literals are prefixed with `b` (e.g., `b'mydata'`) or explicitly encoded (e.g., `mystring.encode('utf-8')`).
affects: All versions
gotchaSM4 keys and IVs must be exactly 16 bytes long. Providing an incorrect length will raise a `ValueError`.
fix
Ensure the key and IV variables are `bytes` objects exactly 16 bytes long. For example, `key = b'mysecretkey12345'`.
affects: All versions
gotchaAs a pure-Python implementation, `gmssl` might exhibit lower performance for high-throughput cryptographic operations compared to C-accelerated libraries.
fix
For performance-critical applications, consider benchmarking `gmssl` against alternatives or optimizing application design to minimize cryptographic overhead.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'gmssl'
The `gmssl` library is not installed in the current Python environment.
fix
Run `pip install gmssl` to install the library.
ValueError: key is not 16 bytes
The SM4 key provided for encryption/decryption does not have the required length of 16 bytes.
fix
Ensure the key variable is a `bytes` object exactly 16 bytes long. For example, `key = b'mysecretkey12345'`.
TypeError: argument should be bytes or other byte-like object, not str
A string (`str`) was passed to a cryptographic function that expects byte-like objects (`bytes`).
fix
Convert the string to bytes using `.encode('utf-8')` or ensure string literals are byte literals (e.g., `b'data'`).
Upgrade
Version history
3.2.2latest on PyPI · released Jun 21, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
152 hits · last 30 days
node
142
OpenAI (training)
1
Resources
gmssl — pip install gmssl · libregistry