smcryptopy


Namesmcryptopy JSON
Version 0.2.1 PyPI version JSON
download
home_page
SummaryA Rust Implementation of China's Standards of Encryption Algorithms(SM2/SM3/SM4)
upload_time2024-03-03 19:42:25
maintainer
docs_urlNone
authoryumeng <yumeng@foxmail.com>
requires_python>=3.8
licenseMIT
keywords cipher cryptography
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            本项目是`smcrypto`的Python3实现,改库实现了国密`SM3`、`SM2`、`SM4`算法。

## 安装

```
pip install smcryptopy
```

## 快速开始

### SM3消息摘要算法

```python
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非对称加密算法

```python
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对称加密算法

```python
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')
```


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "smcryptopy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "cipher,cryptography",
    "author": "yumeng <yumeng@foxmail.com>",
    "author_email": "yumeng <yumeng@foxmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/f1/4c/5af0325b8dac0536b09c89329bae6d2f47a60d30a16909569962de0d1fc8/smcryptopy-0.2.1.tar.gz",
    "platform": null,
    "description": "\u672c\u9879\u76ee\u662f`smcrypto`\u7684Python3\u5b9e\u73b0\uff0c\u6539\u5e93\u5b9e\u73b0\u4e86\u56fd\u5bc6`SM3`\u3001`SM2`\u3001`SM4`\u7b97\u6cd5\u3002\n\n## \u5b89\u88c5\n\n```\npip install smcryptopy\n```\n\n## \u5feb\u901f\u5f00\u59cb\n\n### SM3\u6d88\u606f\u6458\u8981\u7b97\u6cd5\n\n```python\nfrom smcryptopy import sm3\n\n# hash\u7ed3\u679c\u4ee516\u8fdb\u5236\u5b57\u7b26\u4e32\u5f62\u5f0f\u8fd4\u56de\nhash = sm3.sm3_hash(b'abc')\nassert(hash == '66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0')\n\nhash = sm3.sm3_hash_string('abc')\nassert(hash == '66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0')\n```\n\n### SM2\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\n\n```python\nfrom smcryptopy import sm2\n\n# \u751f\u6210\u5bc6\u94a5\u5bf9\uff0c\u4ee564\u4f4d\u6216128\u4f4d16\u8fdb\u5236\u5b57\u7b26\u4e32\u5f62\u5f0f\u63d0\u4f9b\uff0c\u516c\u94a5\u4e0d\u5305\u542b\u5f00\u5934\u768404\u6807\u8bc6\n(sk, pk) = sm2.gen_keypair()\nassert(len(sk) == 64)\nassert(len(pk) == 128)\nassert(sm2.hex_valid(sk))\nassert(sm2.hex_valid(pk))\n\n# \u4ece\u79c1\u94a5\u5bfc\u51fa\u516c\u94a5\npk_ = sm2.pk_from_sk(sk)\nassert(pk == pk_)\n\nassert(sm2.privkey_valid(sk))\nassert(sm2.pubkey_valid(pk))\n\n# \u7b7e\u540d\u548c\u9a8c\u7b7e\nsign = sm2.sign(b'yumeng', b'abc', sk)\nverify = sm2.verify(b'yumeng', b'abc', sign, pk)\nassert(verify)\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\nenc = sm2.encrypt(b'abc', pk)\ndec = sm2.decrypt(enc, sk)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u4f46\u4f7f\u7528c1c2c3\u7684\u6392\u5217\u65b9\u5f0f\nenc = sm2.encrypt_c1c2c3(b'abc', pk)\ndec = sm2.decrypt_c1c2c3(enc, sk)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u4f46\u4f7f\u7528asn1\u7f16\u7801\nenc = sm2.encrypt_asna1(b'abc', pk)\ndec = sm2.decrypt_asna1(enc, sk)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u4f46\u5bc6\u6587\u4ee516\u8fdb\u5236\u5b57\u7b26\u4e32\u5f62\u5f0f\u63d0\u4f9b\nenc = sm2.encrypt_hex(b'abc', pk)\nassert(sm2.hex_valid(enc))\ndec = sm2.decrypt_hex(enc, sk)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u4f46\u5bc6\u6587\u4ee5base64\u7f16\u7801\u5f62\u5f0f\u63d0\u4f9b\nenc = sm2.encrypt_base64(b'abc', pk)\nassert(sm2.base64_valid(enc))\ndec = sm2.decrypt_base64(enc, sk)\nassert(dec == b'abc')\n\n# \u5bc6\u94a5\u4ea4\u6362\nid_a = b'a@example.com'\nid_b = b'b@example.com'\nklen = 16\nsk_a = sm2.gen_keypair()[0]\nsk_b = sm2.gen_keypair()[0]\na_1 = sm2.keyexchange_1ab(klen, id_a, sk_a)\nb_1 = sm2.keyexchange_1ab(klen, id_b, sk_b)\na_k = sm2.keyexchange_2a(id_a, sk_a, a_1[1], b_1[0])\nb_k = sm2.keyexchange_2b(id_b, sk_b, b_1[1], a_1[0])\nassert(a_k == b_k)\n```\n\n### SM4\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\n\n```python\nfrom smcryptopy import sm4\n\n# \u79d8\u94a5\u548c\u521d\u59cb\u5411\u91cf\u5fc5\u987b\u4e3a32\u4f4d16\u8fdb\u5236\u5b57\u7b26\u4e32\uff08\u957f\u5ea6\u4e3a16\u7684\u5b57\u8282\u4e32\uff09\nkey = b'1234567812345678'\niv = b'0000000000000000'\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0cECB\u6a21\u5f0f\nenc = sm4.encrypt_ecb(b'abc', key)\ndec = sm4.decrypt_ecb(enc, key)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0cECB\u6a21\u5f0f\uff0c\u5bc6\u6587\u4f7f\u7528base64\u7f16\u7801\u5f62\u5f0f\nenc = sm4.encrypt_ecb_base64(b'abc', key)\ndec = sm4.decrypt_ecb_base64(enc, key)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0cECB\u6a21\u5f0f\uff0c\u5bc6\u6587\u4f7f\u752816\u8fdb\u5236\u5b57\u7b26\u4e32\u5f62\u5f0f\nenc = sm4.encrypt_ecb_hex(b'abc', key)\ndec = sm4.decrypt_ecb_hex(enc, key)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0cCBC\u6a21\u5f0f\nenc = sm4.encrypt_cbc(b'abc', key, iv)\ndec = sm4.decrypt_cbc(enc, key, iv)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0cCBC\u6a21\u5f0f\uff0c\u5bc6\u6587\u4f7f\u7528base64\u7f16\u7801\u5f62\u5f0f\nenc = sm4.encrypt_cbc_base64(b'abc', key, iv)\ndec = sm4.decrypt_cbc_base64(enc, key, iv)\nassert(dec == b'abc')\n\n# \u52a0\u5bc6\u548c\u89e3\u5bc6\uff0cCBC\u6a21\u5f0f\uff0c\u5bc6\u6587\u4f7f\u752816\u8fdb\u5236\u5b57\u7b26\u4e32\u5f62\u5f0f\nenc = sm4.encrypt_cbc_hex(b'abc', key, iv)\ndec = sm4.decrypt_cbc_hex(enc, key, iv)\nassert(dec == b'abc')\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Rust Implementation of China's Standards of Encryption Algorithms(SM2/SM3/SM4)",
    "version": "0.2.1",
    "project_urls": {
        "Source Code": "https://github.com/zhuobie/smcrypto-ffi"
    },
    "split_keywords": [
        "cipher",
        "cryptography"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "78800697e6b56c11f9c563c6fe8f8946e4cec4100290325352d57ca95dca4a4f",
                "md5": "5b6cd31e10d78f0d7e8893e31f20f1ed",
                "sha256": "a3f910b76119e68e89fc5111b4e4d36a5bfefa478f640a336eb11fede11b0f90"
            },
            "downloads": -1,
            "filename": "smcryptopy-0.2.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "5b6cd31e10d78f0d7e8893e31f20f1ed",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6740352,
            "upload_time": "2024-03-03T19:42:16",
            "upload_time_iso_8601": "2024-03-03T19:42:16.495063Z",
            "url": "https://files.pythonhosted.org/packages/78/80/0697e6b56c11f9c563c6fe8f8946e4cec4100290325352d57ca95dca4a4f/smcryptopy-0.2.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "44ebd0c8c13927ead1274ddaa192bcfb5e687136b9b92eeb86ac188e5551b158",
                "md5": "f2c2de44bcd0ffeb045270a85077b863",
                "sha256": "404c8b7fef2445842ecb2f9cb1771844f4d5da0c281f0f2fd724711d2aee63b9"
            },
            "downloads": -1,
            "filename": "smcryptopy-0.2.1-py3-none-musllinux_1_2_x86_64.whl",
            "has_sig": false,
            "md5_digest": "f2c2de44bcd0ffeb045270a85077b863",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6770608,
            "upload_time": "2024-03-03T19:42:20",
            "upload_time_iso_8601": "2024-03-03T19:42:20.278656Z",
            "url": "https://files.pythonhosted.org/packages/44/eb/d0c8c13927ead1274ddaa192bcfb5e687136b9b92eeb86ac188e5551b158/smcryptopy-0.2.1-py3-none-musllinux_1_2_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a4e9fe7545321c5a3b2c244d13fa6276e2a742473bb56f7e7d7661603bb31b08",
                "md5": "c7a1a18ca1b9d5d366ae8f339427fb26",
                "sha256": "a7f844ab665eb051b38c470688d6521bac5e182911641c94010dd981f41b70e7"
            },
            "downloads": -1,
            "filename": "smcryptopy-0.2.1-py3-none-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "c7a1a18ca1b9d5d366ae8f339427fb26",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 1542691,
            "upload_time": "2024-03-03T19:42:23",
            "upload_time_iso_8601": "2024-03-03T19:42:23.189098Z",
            "url": "https://files.pythonhosted.org/packages/a4/e9/fe7545321c5a3b2c244d13fa6276e2a742473bb56f7e7d7661603bb31b08/smcryptopy-0.2.1-py3-none-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f14c5af0325b8dac0536b09c89329bae6d2f47a60d30a16909569962de0d1fc8",
                "md5": "0ab80807a236a2097af2b5e300fdb352",
                "sha256": "e6bee0f245107914b019c033a97449a0d2f77ab5646c33756f6d1f8aab6270c4"
            },
            "downloads": -1,
            "filename": "smcryptopy-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "0ab80807a236a2097af2b5e300fdb352",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 25130,
            "upload_time": "2024-03-03T19:42:25",
            "upload_time_iso_8601": "2024-03-03T19:42:25.183040Z",
            "url": "https://files.pythonhosted.org/packages/f1/4c/5af0325b8dac0536b09c89329bae6d2f47a60d30a16909569962de0d1fc8/smcryptopy-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-03 19:42:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zhuobie",
    "github_project": "smcrypto-ffi",
    "github_not_found": true,
    "lcname": "smcryptopy"
}
        
Elapsed time: 1.06904s