pygmssl


Namepygmssl JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/j-z10/pygmssl
SummaryA Python ctypes implementation of GmSSLv3.1.1
upload_time2024-03-30 07:33:40
maintainerZhang Jie
docs_urlNone
authorZhang Jie
requires_python<4.0,>=3.11
licenseMIT
keywords 国密 gmssl gmssl sm2 sm3 sm4
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![codecov](https://codecov.io/gh/j-z10/pygmssl/graph/badge.svg?token=PS29GCO00T)](https://codecov.io/gh/j-z10/pygmssl)
# pygmssl

A Python ctypes [GmSSL](https://github.com/guanzhi/GmSSL)v3.1.1 implementation
=======

## INSTALL
### install GmSSLv3.1.1
```bash
git clone https://github.com/guanzhi/GmSSL.git
cd GmSSL && git checkout tags/v3.1.1
mkdir build && cd build && cmake ..
make && make test && sudo make install
sudo ldconfig

# check gmssl installed
gmssl version
```

### install pygmssl
```bash
python -m pip install pygmssl
```

## USAGE

### SM3
```python3
from pygmssl.sm3 import SM3

# sm3 hash all data
data = b'hello, world'
assert SM3(data).hexdigest() == '02df30dff15f2ccb72bffdcb44e68d4d09974036dc7a6927e556fbef421c7f34'

# sm3 hash data by part
s3 = SM3()
for part_data in [b'hel', b'lo', b', world']:
    s3.update(part_data)
assert s3.hexdigest() == '02df30dff15f2ccb72bffdcb44e68d4d09974036dc7a6927e556fbef421c7f34'

# sm3 hash with sm2 public key and id extra
# if not id, id will be sm2 default id, which is b'1234567812345678'
sm2_pub_key = b'\xe8G\x0be\xc3P\x12\x10\xa9+\xe6n%\x9dc\xe9\xed\xaeBEf\xab\xd0\x12t\x01RQ\xb8\xceJ\xb0\x9b;\x17\xbb.\xf7i\x00\x18Nq~\xa3\xf4n\xf8\xd7\xdd%m-@\xa3\xc3tv\xe4\xe2\xf7\x81\x83\xe0'
assert SM3.hash_with_sm2(data, sm2_pub_key).hexdigest() == 'cad9730d3d178bf4c234ab7d2b1fc39569af314faecda258f30ee92456f53d2f'
assert SM3.hash_with_sm2(data, sm2_pub_key, id=b'1234567812345678').hexdigest() == 'cad9730d3d178bf4c234ab7d2b1fc39569af314faecda258f30ee92456f53d2f'
assert SM3.hash_with_sm2(data, sm2_pub_key, id=b'123').hexdigest() == 'd5ba879b0197c1a528283ff9a2b25f347474749b27ab5fd7c8a55648fff1f861'

# sm3 hash with sm2 public key by part
s3 = SM3.hash_with_sm2(b'', sm2_pub_key)
for part_data in [b'hel', b'lo', b', world']:
    s3.update(part_data)
assert s3.hexdigest() == 'cad9730d3d178bf4c234ab7d2b1fc39569af314faecda258f30ee92456f53d2f'
```
### SM3-HMAC

```python3
from pygmssl.sm3 import SM3HMAC

# sm3 hmac all data
data = b'hello, world'
assert SM3HMAC(key=b'123', data=data).hexdigest() == '4410e0fef1ae0a641c7c4f1a7f6c7cef5b992f80607d5275f669d8942a77cc08'

# sm3 hmac data by part
s3 = SM3HMAC(key=b'123')
for part_data in [b'hel', b'lo', b', world']:
    s3.update(part_data)
assert s3.hexdigest() == '4410e0fef1ae0a641c7c4f1a7f6c7cef5b992f80607d5275f669d8942a77cc08'
```

### SM4
```python3
from pygmssl.sm4 import SM4, MOD

# CBC, must 16 bytes key and 16 bytes iv
key = b'F\x7f\x8e7\x05\xc8\x14\x92\xa8P\x8feGx\xf6\xfc'
iv = b'W\xd3,A\x97L\x0e\xfd\xbe\xb5@\xa9\xb0\xe2L\xdf'
cipher = SM4(key, mode=MOD.CBC, iv=iv)
data = b'hello, world'
assert cipher.decrypt(cipher.encrypt(data)) == data

```

### SM2
```python3
from pygmssl.sm2 import SM2

# generate sm2 private key and public key
s2 = SM2.generate_new_pair()
print(s2.pub_key)   # 64 byte public key
print(s2.pri_key)   # 32 byte private key

# 64 byte public_key or 65 byte public key(which is b'\x04' + 64 byte)
test_pub_key = b'\xe8G\x0be\xc3P\x12\x10\xa9+\xe6n%\x9dc\xe9\xed\xaeBEf' \
               b'\xab\xd0\x12t\x01RQ\xb8\xceJ\xb0\x9b;\x17\xbb.\xf7i\x00' \
               b'\x18Nq~\xa3\xf4n\xf8\xd7\xdd%m-@\xa3\xc3tv\xe4\xe2\xf7\x81\x83\xe0'
test_pri_key = b'\x87\x95\x84V\xcej\x8cq\xd1\x10\x94\xa7\xb7\x8d\xc1\x9a' \
               b'\x98\xcf\xe7\x84\x90\x9d\x8d\xd2\xff\xb4\xaeo2\xb8j\x1b'

# SM2 sign and verify with default id
signer = SM2(pub_key=test_pub_key, pri_key=test_pri_key)
data = b'hello, world'
sig = signer.sign(data) # if not id, id will be sm2.SM2_DEFAULT_ID
assert signer.verify(data, sig) == True

# SM2 sign and verify with id
signer2 = SM2(pub_key=test_pub_key, pri_key=test_pri_key)
data = b'hello, world'
sig = signer2.sign(data, id=b'123') # if not id, id will be sm2.SM2_DEFAULT_ID
assert signer2.verify(data, sig, id=b'123') == True
assert signer2.verify(data + b'\x00', sig, id=b'123') == False  # libgmssl will print some fail info

# 如果Java sign可能给出的不是asn1 der格式的sig, 签名和验签的时候指定asn1=True, 将会获取签名后的64位实际数据
zk = SM2(pub_key=test_pub_key, pri_key=test_pri_key)
data = b'hello, world'
sig = zk.sign(data, id=b'123', asn1=True)
assert len(sig) == 64
assert zk.verify(data, sig, id=b'123', asn1=True) == True

# SM2 encrypt and decrypt, data's length <= sm2.SM2_MAX_PLAINTEXT_SIZE
en = SM2(pub_key=test_pub_key)
data = b'hello, world'
s_data = en.encrypt(data)

de = SM2(pri_key=test_pri_key)
d_data = de.decrypt(s_data)
assert d_data == data
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/j-z10/pygmssl",
    "name": "pygmssl",
    "maintainer": "Zhang Jie",
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": "zhangj_10ujs@foxmail.com",
    "keywords": "\u56fd\u5bc6, GmSSL, gmssl, sm2, sm3, sm4",
    "author": "Zhang Jie",
    "author_email": "zhangj_10ujs@foxmail.com",
    "download_url": "https://files.pythonhosted.org/packages/89/cb/0e0b57d4aa329315681f5564e9a3b0b540022d20412b7fba4924d1173151/pygmssl-0.1.2.tar.gz",
    "platform": null,
    "description": "[![codecov](https://codecov.io/gh/j-z10/pygmssl/graph/badge.svg?token=PS29GCO00T)](https://codecov.io/gh/j-z10/pygmssl)\n# pygmssl\n\nA Python ctypes [GmSSL](https://github.com/guanzhi/GmSSL)v3.1.1 implementation\n=======\n\n## INSTALL\n### install GmSSLv3.1.1\n```bash\ngit clone https://github.com/guanzhi/GmSSL.git\ncd GmSSL && git checkout tags/v3.1.1\nmkdir build && cd build && cmake ..\nmake && make test && sudo make install\nsudo ldconfig\n\n# check gmssl installed\ngmssl version\n```\n\n### install pygmssl\n```bash\npython -m pip install pygmssl\n```\n\n## USAGE\n\n### SM3\n```python3\nfrom pygmssl.sm3 import SM3\n\n# sm3 hash all data\ndata = b'hello, world'\nassert SM3(data).hexdigest() == '02df30dff15f2ccb72bffdcb44e68d4d09974036dc7a6927e556fbef421c7f34'\n\n# sm3 hash data by part\ns3 = SM3()\nfor part_data in [b'hel', b'lo', b', world']:\n    s3.update(part_data)\nassert s3.hexdigest() == '02df30dff15f2ccb72bffdcb44e68d4d09974036dc7a6927e556fbef421c7f34'\n\n# sm3 hash with sm2 public key and id extra\n# if not id, id will be sm2 default id, which is b'1234567812345678'\nsm2_pub_key = b'\\xe8G\\x0be\\xc3P\\x12\\x10\\xa9+\\xe6n%\\x9dc\\xe9\\xed\\xaeBEf\\xab\\xd0\\x12t\\x01RQ\\xb8\\xceJ\\xb0\\x9b;\\x17\\xbb.\\xf7i\\x00\\x18Nq~\\xa3\\xf4n\\xf8\\xd7\\xdd%m-@\\xa3\\xc3tv\\xe4\\xe2\\xf7\\x81\\x83\\xe0'\nassert SM3.hash_with_sm2(data, sm2_pub_key).hexdigest() == 'cad9730d3d178bf4c234ab7d2b1fc39569af314faecda258f30ee92456f53d2f'\nassert SM3.hash_with_sm2(data, sm2_pub_key, id=b'1234567812345678').hexdigest() == 'cad9730d3d178bf4c234ab7d2b1fc39569af314faecda258f30ee92456f53d2f'\nassert SM3.hash_with_sm2(data, sm2_pub_key, id=b'123').hexdigest() == 'd5ba879b0197c1a528283ff9a2b25f347474749b27ab5fd7c8a55648fff1f861'\n\n# sm3 hash with sm2 public key by part\ns3 = SM3.hash_with_sm2(b'', sm2_pub_key)\nfor part_data in [b'hel', b'lo', b', world']:\n    s3.update(part_data)\nassert s3.hexdigest() == 'cad9730d3d178bf4c234ab7d2b1fc39569af314faecda258f30ee92456f53d2f'\n```\n### SM3-HMAC\n\n```python3\nfrom pygmssl.sm3 import SM3HMAC\n\n# sm3 hmac all data\ndata = b'hello, world'\nassert SM3HMAC(key=b'123', data=data).hexdigest() == '4410e0fef1ae0a641c7c4f1a7f6c7cef5b992f80607d5275f669d8942a77cc08'\n\n# sm3 hmac data by part\ns3 = SM3HMAC(key=b'123')\nfor part_data in [b'hel', b'lo', b', world']:\n    s3.update(part_data)\nassert s3.hexdigest() == '4410e0fef1ae0a641c7c4f1a7f6c7cef5b992f80607d5275f669d8942a77cc08'\n```\n\n### SM4\n```python3\nfrom pygmssl.sm4 import SM4, MOD\n\n# CBC, must 16 bytes key and 16 bytes iv\nkey = b'F\\x7f\\x8e7\\x05\\xc8\\x14\\x92\\xa8P\\x8feGx\\xf6\\xfc'\niv = b'W\\xd3,A\\x97L\\x0e\\xfd\\xbe\\xb5@\\xa9\\xb0\\xe2L\\xdf'\ncipher = SM4(key, mode=MOD.CBC, iv=iv)\ndata = b'hello, world'\nassert cipher.decrypt(cipher.encrypt(data)) == data\n\n```\n\n### SM2\n```python3\nfrom pygmssl.sm2 import SM2\n\n# generate sm2 private key and public key\ns2 = SM2.generate_new_pair()\nprint(s2.pub_key)   # 64 byte public key\nprint(s2.pri_key)   # 32 byte private key\n\n# 64 byte public_key or 65 byte public key(which is b'\\x04' + 64 byte)\ntest_pub_key = b'\\xe8G\\x0be\\xc3P\\x12\\x10\\xa9+\\xe6n%\\x9dc\\xe9\\xed\\xaeBEf' \\\n               b'\\xab\\xd0\\x12t\\x01RQ\\xb8\\xceJ\\xb0\\x9b;\\x17\\xbb.\\xf7i\\x00' \\\n               b'\\x18Nq~\\xa3\\xf4n\\xf8\\xd7\\xdd%m-@\\xa3\\xc3tv\\xe4\\xe2\\xf7\\x81\\x83\\xe0'\ntest_pri_key = b'\\x87\\x95\\x84V\\xcej\\x8cq\\xd1\\x10\\x94\\xa7\\xb7\\x8d\\xc1\\x9a' \\\n               b'\\x98\\xcf\\xe7\\x84\\x90\\x9d\\x8d\\xd2\\xff\\xb4\\xaeo2\\xb8j\\x1b'\n\n# SM2 sign and verify with default id\nsigner = SM2(pub_key=test_pub_key, pri_key=test_pri_key)\ndata = b'hello, world'\nsig = signer.sign(data) # if not id, id will be sm2.SM2_DEFAULT_ID\nassert signer.verify(data, sig) == True\n\n# SM2 sign and verify with id\nsigner2 = SM2(pub_key=test_pub_key, pri_key=test_pri_key)\ndata = b'hello, world'\nsig = signer2.sign(data, id=b'123') # if not id, id will be sm2.SM2_DEFAULT_ID\nassert signer2.verify(data, sig, id=b'123') == True\nassert signer2.verify(data + b'\\x00', sig, id=b'123') == False  # libgmssl will print some fail info\n\n# \u5982\u679cJava sign\u53ef\u80fd\u7ed9\u51fa\u7684\u4e0d\u662fasn1 der\u683c\u5f0f\u7684sig, \u7b7e\u540d\u548c\u9a8c\u7b7e\u7684\u65f6\u5019\u6307\u5b9aasn1=True, \u5c06\u4f1a\u83b7\u53d6\u7b7e\u540d\u540e\u768464\u4f4d\u5b9e\u9645\u6570\u636e\nzk = SM2(pub_key=test_pub_key, pri_key=test_pri_key)\ndata = b'hello, world'\nsig = zk.sign(data, id=b'123', asn1=True)\nassert len(sig) == 64\nassert zk.verify(data, sig, id=b'123', asn1=True) == True\n\n# SM2 encrypt and decrypt, data's length <= sm2.SM2_MAX_PLAINTEXT_SIZE\nen = SM2(pub_key=test_pub_key)\ndata = b'hello, world'\ns_data = en.encrypt(data)\n\nde = SM2(pri_key=test_pri_key)\nd_data = de.decrypt(s_data)\nassert d_data == data\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python ctypes implementation of GmSSLv3.1.1",
    "version": "0.1.2",
    "project_urls": {
        "Homepage": "https://github.com/j-z10/pygmssl"
    },
    "split_keywords": [
        "\u56fd\u5bc6",
        " gmssl",
        " gmssl",
        " sm2",
        " sm3",
        " sm4"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8d90cc48779ccedc2a19d360b145ad9771a4a456bb18d8de2dd67a1b685c69a0",
                "md5": "57d4861e7b8a177326c16c4f197c5410",
                "sha256": "0cbfa7c06752680ff7dc933f4070925d483f64a594ef41631a16428038d69c10"
            },
            "downloads": -1,
            "filename": "pygmssl-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "57d4861e7b8a177326c16c4f197c5410",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 8921,
            "upload_time": "2024-03-30T07:33:38",
            "upload_time_iso_8601": "2024-03-30T07:33:38.895493Z",
            "url": "https://files.pythonhosted.org/packages/8d/90/cc48779ccedc2a19d360b145ad9771a4a456bb18d8de2dd67a1b685c69a0/pygmssl-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "89cb0e0b57d4aa329315681f5564e9a3b0b540022d20412b7fba4924d1173151",
                "md5": "90c8c3a002361d9afd46000083255c8e",
                "sha256": "691c157f9ae10d78a99df1a8dd27a7f926a3b5da80b5b714aaf21eb527d46d32"
            },
            "downloads": -1,
            "filename": "pygmssl-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "90c8c3a002361d9afd46000083255c8e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 8631,
            "upload_time": "2024-03-30T07:33:40",
            "upload_time_iso_8601": "2024-03-30T07:33:40.680597Z",
            "url": "https://files.pythonhosted.org/packages/89/cb/0e0b57d4aa329315681f5564e9a3b0b540022d20412b7fba4924d1173151/pygmssl-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-30 07:33:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "j-z10",
    "github_project": "pygmssl",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pygmssl"
}
        
Elapsed time: 0.69410s