gmsm 1.4.5
A dependency library for GM, SM, and AES encryption, supporting CFB CBC and ECB modes.
Requires Python: >=3.6
Keywords: gmsm

# `GMSM`

> *一个简单高性能的国密与AES加密库*

支持 SM2, SM3, SM4 (ECB, CBC, CFB)

支持 AES (ECB, CBC, CFB, CTR, GCM), SHA (256, 512, 1), MD5, HMAC

## 安装 (Installation)

**支持 Python 3.6+ 版本.**

```console
$ pip install gmsm
```

## 简单示例 (Examples)

```python
import gmsm

# --- SM2 加解密 ---
# mode 0|C1C3C2默认, 1|C1C2C3
pub_key = '04e581fb5575aa046c4e00e1c893cdfd461b8276a7850730389573bffa3ce5e49f51a08d729834b13556fcf43891ecd2f5d02ce3a2948a375ade931c7de8e1e88d'
plaintext = 'hello world'
encrypted_hex = gmsm.sm2_encode(pub_key, plaintext, 0)
print(f"SM2 Encrypted (Hex): {encrypted_hex}")

priv_key = '3f9347edfafacdf29f1b968734110ec71aefae215030807dde3167875453b8ff'
decrypted = gmsm.sm2_decode(priv_key, encrypted_hex, 0) #解密时如果encrypted_hex开头不带04需要自己加上04
print(f"SM2 Decrypted: {decrypted}")

# --- SM4 加密 (输出为Hex) ---
key = '3l5a8r6h2o4s7c5p'
plaintext = 'hello world'
encrypted_hex = gmsm.sm4_encrypt_cbc_hex(plaintext, key)
print(f"SM4 Encrypt CBC (Hex): {encrypted_hex}")

decrypted = gmsm.sm4_decrypt_cbc_hex(encrypted_hex, key)
print(f"SM4 Decrypt CBC: {decrypted}")

encrypted_base64 = gmsm.sm4_encrypt_ecb_base64(plaintext, key)
print(f"SM4 Encrypt ECB (Base64): {encrypted_base64}")

decrypted = gmsm.sm4_decrypt_ecb_base64(encrypted_base64, key)
print(f"SM4 Decrypt ECB (Base64): {decrypted}")

# --- SM3 ---
plaintext = 'hello world'
sm3_hashed = gmsm.sm3_encode(plaintext)
print(f"SM3 {sm3_hashed}")

# --- AES Key ---
# AES-128 -> 16 个字符 (纯英文/数字)
# AES-192 -> 24 个字符
# AES-256 -> 32 个字符

# 密钥必须是字符串、如果为Base64需要手动转换
aeskey = '687b37450d97ce55389c718a16b56a1b'

# --- AES CBC 加密 (输出为Base64) ---
plaintext = 'hello world'
aes_cbc_encrypted = gmsm.aes_encrypt_cbc_base64(plaintext, aeskey)
print(f"AES CBC Encrypt: {aes_cbc_encrypted}")

# --- AES CBC 解密 (输入为Base64) ---
aes_cbc_encrypted_b64 = 'GWPVtL6EP7ZvlUorFgFU6Q=='
aes_cbc_decrypted_text = gmsm.aes_decrypt_cbc_base64(aes_cbc_encrypted_b64, aeskey)
print(f"AES CBC Decrypted: {aes_cbc_decrypted_text}")

# --- SHA256 ---
content_to_hash = "this is a very important message"
hash_result = gmsm.sha256_encode(content_to_hash)
print(f"SHA256: {hash_result}")


# --- HMAC ---
message = "data=payload&timestamp=1655186720"
hmac_key = "my-secret-api-key"
signature = gmsm.hmac_sha256(message, hmac_key)
print(f"HMAC Signature: {signature}")
```

## 注意:

本库只适用于prod环境, 不带有任何debug返回, 任何错误只会得到一个空的结果。

加密后输出格式由函数名决定 (_hex 或 _base64)。

## 函数参考 (Function Reference)
### SM2 (非对称加密)

mode 0|C1C3C2默认 1|C1C2C3

sm2_encode(pub_key: str, plaintext: str, mode: num) -> str

sm2_decode(priv_key: str, data: str, mode: num) -> str

### SM3 (哈希算法)

sm3_encode(plaintext: str) -> str

### SM4 (对称加密)

sm4_encrypt_ecb_hex(plaintext: str, key: str) -> str

sm4_encrypt_ecb_base64(plaintext: str, key: str) -> str

sm4_decrypt_ecb_hex(ciphertext_hex: str, key: str) -> str

sm4_decrypt_ecb_base64(ciphertext_base64: str, key: str) -> str

sm4_encrypt_cbc_hex(plaintext: str, key: str) -> str

sm4_encrypt_cbc_base64(plaintext: str, key: str) -> str

sm4_decrypt_cbc_hex(ciphertext_hex: str, key: str) -> str

sm4_decrypt_cbc_base64(ciphertext_base64: str, key: str) -> str

sm4_encrypt_cfb_hex(plaintext: str, key: str) -> str

sm4_encrypt_cfb_base64(plaintext: str, key: str) -> str

sm4_decrypt_cfb_hex(ciphertext_hex: str, key: str) -> str

sm4_decrypt_cfb_base64(ciphertext_base64: str, key: str) -> str

### AES (对称加密)

aes_encrypt_ecb_hex(orig_data: str, key_input: str) -> str

aes_encrypt_ecb_base64(orig_data: str, key_input: str) -> str

aes_decrypt_ecb_hex(encrypted_hex: str, key_input: str) -> str

aes_decrypt_ecb_base64(encrypted_base64: str, key_input: str) -> str

aes_encrypt_cbc_hex(orig_data: str, key_input: str) -> str

aes_encrypt_cbc_base64(orig_data: str, key_input: str) -> str

aes_decrypt_cbc_hex(encrypted_hex: str, key_input: str) -> str

aes_decrypt_cbc_base64(encrypted_base64: str, key_input: str) -> str

aes_encrypt_cfb_hex(orig_data: str, key_input: str) -> str

aes_encrypt_cfb_base64(orig_data: str, key_input: str) -> str

aes_decrypt_cfb_hex(encrypted_hex: str, key_input: str) -> str

aes_decrypt_cfb_base64(encrypted_base64: str, key_input: str) -> str

aes_encrypt_gcm_hex(orig_data: str, key_input: str) -> str

aes_encrypt_gcm_base64(orig_data: str, key_input: str) -> str

aes_decrypt_gcm_hex(encrypted_hex: str, key_input: str) -> str

aes_decrypt_gcm_base64(encrypted_base64: str, key_input: str) -> str

aes_encrypt_ctr_hex(orig_data: str, key_input: str) -> str

aes_encrypt_ctr_base64(orig_data: str, key_input: str) -> str

aes_decrypt_ctr_hex(encrypted_hex: str, key_input: str) -> str

aes_decrypt_ctr_base64(encrypted_base64: str, key_input: str) -> str

### Hashes (哈希算法)

sha256_encode(content: str) -> str

sha512_encode(content: str) -> str

sha1_encode(content: str) -> str

md5_encode(content: str) -> str

### HMAC (消息认证码)

hmac_sha256(message: str, key_input: str) -> str

hmac_sha512(message: str, key_input: str) -> str

hmac_sha1(message: str, key_input: str) -> str

### License

This project is licensed under the MIT License.