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
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.011s · 26.4MB
glibcpy 3.10–3.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
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'gmssl'
The `gmssl` library is not installed in the current Python environment.
fixRun `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.
fixEnsure 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`).
fixConvert 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.