req-enc-dec


Namereq-enc-dec JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttps://github.com/Michaelxwb/ReqEncDec
SummaryRequest/Response Encryption/Decryption Middleware
upload_time2025-07-25 07:45:58
maintainerNone
docs_urlNone
authorJahan
requires_pythonNone
licenseMIT
keywords middleware encryption decryption
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Request Encryption/Decryption Middleware

一个 Python 中间件,用于拦截请求和响应,以根据 URL 配置加密/解密特定字段。

## Features

1. **请求解密**:解密传入请求中的指定字段。
2. **响应加密**:加密传出响应中的指定字段。
3. **嵌套字段支持**:使用点符号(例如 `user.data.email`)处理嵌套字段。
4. **可配置加密**:支持多种可逆加密算法和自定义盐值。
5. **即插即用**:易于集成到现有项目中。

## Installation

```bash
pip install req_enc_dec
```

## Usage

```python
from flask import Flask, request

from req_enc_dec import EncryptionPlugin

app = Flask(__name__)

# Configure the middleware
app.config["ENCRYPTION_ALGO"] = "AES"
app.config["ENCRYPTION_SALT"] = b"your_salt_value"
app.config["ENCRYPTION_KEY"] = b'secret_key'
app.config["ENCRYPTION_URL_CONFIGS"] = {
    "/api/user": {
        "decrypt_fields": ["email"],
        "encrypt_fields": ["user.token", "user.list.name", "user.list.email.email_name", "user.list.qq"]
    }
}

EncryptionPlugin(app=app)


@app.route("/api/user", methods=["POST"])
def handle_user():
    request_data = request.get_json()
    print("email: {}".format(request_data.get("email")))
    return {
        "user":
            {
                "token": "test_token",
                "list": [
                    {
                        "name": "test_name01",
                        "email": [
                            {"email_name": "test_email01"},
                            {"email_name": "test_email02"}
                        ],
                        "qq": ["test_qq01", "test_qq02"],
                    },
                    {
                        "name": "test_name02",
                        "email": [],
                        "qq": [],
                    }
                ]
            }
    }


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

```

## Configuration
- `ENCRYPTION_ALGO`:要使用的加密算法(默认值:`AES`)。
- `ENCRYPTION_SALT`:用于加密的自定义盐值。
- `ENCRYPTION_URL_CONFIGS`:将 URL 映射到其各自字段配置的字典。
## Supported Algorithms

- `AES`(默认)
- 可以通过扩展中间件添加更多算法。

## Performance Optimization

- **缓存加密实例**:中间件缓存加密实例,避免重复初始化,提高重复加密/解密操作的性能。

## Extensibility

- **自定义加密算法**:用户可以通过调用 `register_cipher` 方法注册自定义加密算法。示例:
```python
plugin = EncryptionPlugin(app)
plugin.register_cipher("MY_CUSTOM_ALGO", MyCustomCipher)
```
自定义加密算法类必须实现 `encrypt` 和 `decrypt` 方法。

## License

MIT


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Michaelxwb/ReqEncDec",
    "name": "req-enc-dec",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "middleware encryption decryption",
    "author": "Jahan",
    "author_email": "ambition_xu@163.com",
    "download_url": "https://files.pythonhosted.org/packages/3e/e6/a928162d092278fa3f28711a3a63003d479c61d02ce86bc4b3b7d5f93506/req_enc_dec-0.2.1.tar.gz",
    "platform": null,
    "description": "# Request Encryption/Decryption Middleware\r\n\r\n\u4e00\u4e2a Python \u4e2d\u95f4\u4ef6\uff0c\u7528\u4e8e\u62e6\u622a\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u4ee5\u6839\u636e URL \u914d\u7f6e\u52a0\u5bc6/\u89e3\u5bc6\u7279\u5b9a\u5b57\u6bb5\u3002\r\n\r\n## Features\r\n\r\n1. **\u8bf7\u6c42\u89e3\u5bc6**\uff1a\u89e3\u5bc6\u4f20\u5165\u8bf7\u6c42\u4e2d\u7684\u6307\u5b9a\u5b57\u6bb5\u3002\r\n2. **\u54cd\u5e94\u52a0\u5bc6**\uff1a\u52a0\u5bc6\u4f20\u51fa\u54cd\u5e94\u4e2d\u7684\u6307\u5b9a\u5b57\u6bb5\u3002\r\n3. **\u5d4c\u5957\u5b57\u6bb5\u652f\u6301**\uff1a\u4f7f\u7528\u70b9\u7b26\u53f7\uff08\u4f8b\u5982 `user.data.email`\uff09\u5904\u7406\u5d4c\u5957\u5b57\u6bb5\u3002\r\n4. **\u53ef\u914d\u7f6e\u52a0\u5bc6**\uff1a\u652f\u6301\u591a\u79cd\u53ef\u9006\u52a0\u5bc6\u7b97\u6cd5\u548c\u81ea\u5b9a\u4e49\u76d0\u503c\u3002\r\n5. **\u5373\u63d2\u5373\u7528**\uff1a\u6613\u4e8e\u96c6\u6210\u5230\u73b0\u6709\u9879\u76ee\u4e2d\u3002\r\n\r\n## Installation\r\n\r\n```bash\r\npip install req_enc_dec\r\n```\r\n\r\n## Usage\r\n\r\n```python\r\nfrom flask import Flask, request\r\n\r\nfrom req_enc_dec import EncryptionPlugin\r\n\r\napp = Flask(__name__)\r\n\r\n# Configure the middleware\r\napp.config[\"ENCRYPTION_ALGO\"] = \"AES\"\r\napp.config[\"ENCRYPTION_SALT\"] = b\"your_salt_value\"\r\napp.config[\"ENCRYPTION_KEY\"] = b'secret_key'\r\napp.config[\"ENCRYPTION_URL_CONFIGS\"] = {\r\n    \"/api/user\": {\r\n        \"decrypt_fields\": [\"email\"],\r\n        \"encrypt_fields\": [\"user.token\", \"user.list.name\", \"user.list.email.email_name\", \"user.list.qq\"]\r\n    }\r\n}\r\n\r\nEncryptionPlugin(app=app)\r\n\r\n\r\n@app.route(\"/api/user\", methods=[\"POST\"])\r\ndef handle_user():\r\n    request_data = request.get_json()\r\n    print(\"email: {}\".format(request_data.get(\"email\")))\r\n    return {\r\n        \"user\":\r\n            {\r\n                \"token\": \"test_token\",\r\n                \"list\": [\r\n                    {\r\n                        \"name\": \"test_name01\",\r\n                        \"email\": [\r\n                            {\"email_name\": \"test_email01\"},\r\n                            {\"email_name\": \"test_email02\"}\r\n                        ],\r\n                        \"qq\": [\"test_qq01\", \"test_qq02\"],\r\n                    },\r\n                    {\r\n                        \"name\": \"test_name02\",\r\n                        \"email\": [],\r\n                        \"qq\": [],\r\n                    }\r\n                ]\r\n            }\r\n    }\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    app.run(host=\"0.0.0.0\", port=5000)\r\n\r\n```\r\n\r\n## Configuration\r\n- `ENCRYPTION_ALGO`\uff1a\u8981\u4f7f\u7528\u7684\u52a0\u5bc6\u7b97\u6cd5\uff08\u9ed8\u8ba4\u503c\uff1a`AES`\uff09\u3002\r\n- `ENCRYPTION_SALT`\uff1a\u7528\u4e8e\u52a0\u5bc6\u7684\u81ea\u5b9a\u4e49\u76d0\u503c\u3002\r\n- `ENCRYPTION_URL_CONFIGS`\uff1a\u5c06 URL \u6620\u5c04\u5230\u5176\u5404\u81ea\u5b57\u6bb5\u914d\u7f6e\u7684\u5b57\u5178\u3002\r\n## Supported Algorithms\r\n\r\n- `AES`\uff08\u9ed8\u8ba4\uff09\r\n- \u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u4e2d\u95f4\u4ef6\u6dfb\u52a0\u66f4\u591a\u7b97\u6cd5\u3002\r\n\r\n## Performance Optimization\r\n\r\n- **\u7f13\u5b58\u52a0\u5bc6\u5b9e\u4f8b**\uff1a\u4e2d\u95f4\u4ef6\u7f13\u5b58\u52a0\u5bc6\u5b9e\u4f8b\uff0c\u907f\u514d\u91cd\u590d\u521d\u59cb\u5316\uff0c\u63d0\u9ad8\u91cd\u590d\u52a0\u5bc6/\u89e3\u5bc6\u64cd\u4f5c\u7684\u6027\u80fd\u3002\r\n\r\n## Extensibility\r\n\r\n- **\u81ea\u5b9a\u4e49\u52a0\u5bc6\u7b97\u6cd5**\uff1a\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 `register_cipher` \u65b9\u6cd5\u6ce8\u518c\u81ea\u5b9a\u4e49\u52a0\u5bc6\u7b97\u6cd5\u3002\u793a\u4f8b\uff1a\r\n```python\r\nplugin = EncryptionPlugin(app)\r\nplugin.register_cipher(\"MY_CUSTOM_ALGO\", MyCustomCipher)\r\n```\r\n\u81ea\u5b9a\u4e49\u52a0\u5bc6\u7b97\u6cd5\u7c7b\u5fc5\u987b\u5b9e\u73b0 `encrypt` \u548c `decrypt` \u65b9\u6cd5\u3002\r\n\r\n## License\r\n\r\nMIT\r\n\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Request/Response Encryption/Decryption Middleware",
    "version": "0.2.1",
    "project_urls": {
        "Homepage": "https://github.com/Michaelxwb/ReqEncDec"
    },
    "split_keywords": [
        "middleware",
        "encryption",
        "decryption"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "22dfd61867a47bd27abc349c95fd0bca4fb5c1d11b68abfe89f2b0e5a1497fac",
                "md5": "b32da644d3feabe4369918b74a1e3112",
                "sha256": "68d1ae5f5dfb374466060c473ed6c454943849a5f78ed941986f2b32889332a4"
            },
            "downloads": -1,
            "filename": "req_enc_dec-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b32da644d3feabe4369918b74a1e3112",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4908,
            "upload_time": "2025-07-25T07:45:56",
            "upload_time_iso_8601": "2025-07-25T07:45:56.956148Z",
            "url": "https://files.pythonhosted.org/packages/22/df/d61867a47bd27abc349c95fd0bca4fb5c1d11b68abfe89f2b0e5a1497fac/req_enc_dec-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "3ee6a928162d092278fa3f28711a3a63003d479c61d02ce86bc4b3b7d5f93506",
                "md5": "aa474ad8e39fbcbd679e180928c11bf3",
                "sha256": "eac2dc0f2320ff393aba603d7da3de3f99f712f14c529f8513923f75b96f37fb"
            },
            "downloads": -1,
            "filename": "req_enc_dec-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "aa474ad8e39fbcbd679e180928c11bf3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4514,
            "upload_time": "2025-07-25T07:45:58",
            "upload_time_iso_8601": "2025-07-25T07:45:58.248562Z",
            "url": "https://files.pythonhosted.org/packages/3e/e6/a928162d092278fa3f28711a3a63003d479c61d02ce86bc4b3b7d5f93506/req_enc_dec-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-25 07:45:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Michaelxwb",
    "github_project": "ReqEncDec",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "req-enc-dec"
}
        
Elapsed time: 1.58090s