Request/Response Encryption/Decryption Middleware
Project description
Request Encryption/Decryption Middleware
一个 Python 中间件,用于拦截请求和响应,以根据 URL 配置加密/解密特定字段。
Features
- 请求解密:解密传入请求中的指定字段。
- 响应加密:加密传出响应中的指定字段。
- 嵌套字段支持:使用点符号(例如
user.data.email)处理嵌套字段。 - 可配置加密:支持多种可逆加密算法和自定义盐值。
- 即插即用:易于集成到现有项目中。
Installation
pip install req_enc_dec
Usage
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方法注册自定义加密算法。示例:
plugin = EncryptionPlugin(app)
plugin.register_cipher("MY_CUSTOM_ALGO", MyCustomCipher)
自定义加密算法类必须实现 encrypt 和 decrypt 方法。
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
req_enc_dec-0.2.1.tar.gz
(4.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file req_enc_dec-0.2.1.tar.gz.
File metadata
- Download URL: req_enc_dec-0.2.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eac2dc0f2320ff393aba603d7da3de3f99f712f14c529f8513923f75b96f37fb
|
|
| MD5 |
aa474ad8e39fbcbd679e180928c11bf3
|
|
| BLAKE2b-256 |
3ee6a928162d092278fa3f28711a3a63003d479c61d02ce86bc4b3b7d5f93506
|
File details
Details for the file req_enc_dec-0.2.1-py3-none-any.whl.
File metadata
- Download URL: req_enc_dec-0.2.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68d1ae5f5dfb374466060c473ed6c454943849a5f78ed941986f2b32889332a4
|
|
| MD5 |
b32da644d3feabe4369918b74a1e3112
|
|
| BLAKE2b-256 |
22dfd61867a47bd27abc349c95fd0bca4fb5c1d11b68abfe89f2b0e5a1497fac
|