raytoolsbox 2.0.1__tar.gz → 3.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {raytoolsbox-2.0.1 → raytoolsbox-3.0.0}/.gitignore +3 -1
- raytoolsbox-3.0.0/CHANGELOG.md +107 -0
- raytoolsbox-3.0.0/LICENSE +21 -0
- {raytoolsbox-2.0.1 → raytoolsbox-3.0.0}/PKG-INFO +40 -62
- {raytoolsbox-2.0.1 → raytoolsbox-3.0.0}/README.md +31 -58
- raytoolsbox-3.0.0/pyproject.toml +51 -0
- raytoolsbox-3.0.0/src/raytoolsbox/__init__.py +36 -0
- raytoolsbox-3.0.0/src/raytoolsbox/_lazy.py +55 -0
- raytoolsbox-3.0.0/src/raytoolsbox/security/__init__.py +31 -0
- raytoolsbox-3.0.0/src/raytoolsbox/security/authenticator.py +393 -0
- {raytoolsbox-2.0.1 → raytoolsbox-3.0.0}/src/raytoolsbox/security/crypto_manager.py +216 -190
- raytoolsbox-3.0.0/src/raytoolsbox/security/pin_manager.py +220 -0
- raytoolsbox-3.0.0/src/raytoolsbox/utils/__init__.py +47 -0
- {raytoolsbox-2.0.1 → raytoolsbox-3.0.0}/src/raytoolsbox/utils/mailToPhone.py +52 -23
- {raytoolsbox-2.0.1 → raytoolsbox-3.0.0}/src/raytoolsbox/utils/monitor_func.py +14 -26
- {raytoolsbox-2.0.1 → raytoolsbox-3.0.0}/src/raytoolsbox/utils/useful_func.py +86 -12
- raytoolsbox-3.0.0/tests/security/test_authenticator.py +253 -0
- {raytoolsbox-2.0.1/tests → raytoolsbox-3.0.0/tests/security}/test_crypto_manager.py +75 -3
- {raytoolsbox-2.0.1/tests → raytoolsbox-3.0.0/tests/security}/test_pin_manager.py +41 -0
- raytoolsbox-3.0.0/tests/test_lazy_imports.py +98 -0
- raytoolsbox-3.0.0/tests/utils/test_mail_to_phone.py +107 -0
- {raytoolsbox-2.0.1/tests → raytoolsbox-3.0.0/tests/utils}/test_useful_func.py +39 -0
- raytoolsbox-2.0.1/.python-version +0 -1
- raytoolsbox-2.0.1/C++/350/260/203/347/224/250/cpp/346/211/223/345/214/205/344/270/272dll.py +0 -176
- raytoolsbox-2.0.1/C++/350/260/203/347/224/250/fa.py +0 -14
- raytoolsbox-2.0.1/C++/350/260/203/347/224/250/mylib.cpp +0 -6
- raytoolsbox-2.0.1/C++/350/260/203/347/224/250/mylib.dll +0 -0
- raytoolsbox-2.0.1/CHANGELOG.md +0 -77
- raytoolsbox-2.0.1/CLAUDE.md +0 -108
- raytoolsbox-2.0.1/main.py +0 -6
- raytoolsbox-2.0.1/pyproject.toml +0 -55
- raytoolsbox-2.0.1/src/raytoolsbox/__init__.py +0 -0
- raytoolsbox-2.0.1/src/raytoolsbox/security/__init__.py +0 -0
- raytoolsbox-2.0.1/src/raytoolsbox/security/authenticator.py +0 -282
- raytoolsbox-2.0.1/src/raytoolsbox/security/pin_manager.py +0 -114
- raytoolsbox-2.0.1/src/raytoolsbox/utils/__init__.py +0 -0
- raytoolsbox-2.0.1/tests/test_authenticator.py +0 -140
- raytoolsbox-2.0.1/tests/test_mail_to_phone.py +0 -54
- raytoolsbox-2.0.1/uv.lock +0 -1619
- raytoolsbox-2.0.1/uv.toml +0 -1
- raytoolsbox-2.0.1/web/345/217/212/346/225/260/346/215/256/345/267/245/345/205/267/Flask/345/222/214FastAPI/345/257/271/346/257/224/FastAPI/351/243/216/346/240/274.py +0 -14
- raytoolsbox-2.0.1/web/345/217/212/346/225/260/346/215/256/345/267/245/345/205/267/Flask/345/222/214FastAPI/345/257/271/346/257/224/Flask/351/243/216/346/240/274.py +0 -13
- raytoolsbox-2.0.1/web/345/217/212/346/225/260/346/215/256/345/267/245/345/205/267//345/256/211/345/205/250/346/200/247//345/212/240/345/257/206/350/247/243/345/257/206/344/270/216/347/255/276/345/220/215/351/252/214/347/255/276.py +0 -73
- raytoolsbox-2.0.1/web/345/217/212/346/225/260/346/215/256/345/267/245/345/205/267//345/256/211/345/205/250/346/200/247//351/252/214/350/257/201/345/231/250.py +0 -241
- raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/DPI/347/252/227/345/217/243/346/265/213/351/207/217.py +0 -135
- raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/USB/347/275/221/347/273/234/345/205/261/344/272/253.py +0 -208
- raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/resource/ICON_running.png +0 -0
- raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/resource/ICON_stopped.png +0 -0
- raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/resource/favicon (1).ico +0 -0
- raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/usb_watcher.py +0 -79
- raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267//350/257/255/351/237/263/350/275/254/346/226/207/345/255/227.py +0 -104
- raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267//350/275/256/350/257/242/347/216/207/346/237/245/350/257/242.py +0 -159
- raytoolsbox-2.0.1//351/202/256/344/273/266/345/217/221/351/200/201/mailToPhone.py +0 -81
- raytoolsbox-2.0.1//351/202/256/344/273/266/345/217/221/351/200/201//351/207/215/350/246/201/346/227/245/346/234/237/346/217/220/351/206/222/date_emalil_note.py +0 -147
- raytoolsbox-2.0.1//351/202/256/344/273/266/345/217/221/351/200/201//351/207/215/350/246/201/346/227/245/346/234/237/346/217/220/351/206/222/html_get.py +0 -55
- {raytoolsbox-2.0.1 → raytoolsbox-3.0.0}/tests/readme.md +0 -0
- {raytoolsbox-2.0.1/tests → raytoolsbox-3.0.0/tests/utils}/test_monitor_func.py +0 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
所有对本项目的显著变更都将记录在此文件中。
|
|
4
|
+
|
|
5
|
+
格式基于 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),
|
|
6
|
+
版本管理遵循 [Semantic Versioning](https://semver.org/lang/zh-CN/)。
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [3.0.0] - 2026-12-07
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- `Authenticator.get_secret()` 不再打印终端二维码,只返回 `{uri, secret, expires_in}`,二维码由调用方自行生成
|
|
15
|
+
- `Authenticator.delete()` 改为 `delete(code)`:已绑定时必须提供当前验证码,验证失败抛 `PermissionError`,避免本机任意脚本静默摘除 2FA
|
|
16
|
+
- `Authenticator.get_secret()` 增加防误用限制:已有未完成的待绑定 secret 时拒绝重复生成;待绑定 secret 有 5 分钟有效期与 5 次失败上限,超时或超次后作废
|
|
17
|
+
|
|
18
|
+
### Removed
|
|
19
|
+
|
|
20
|
+
- 移除库内二维码生成能力与 `qrcode[pil]` 运行时依赖(连带去掉 pillow),展示层交还调用方
|
|
21
|
+
- `Authenticator.get_secret(force=True)`:重新绑定必须先 `delete(code)` 解除,不再支持直接覆盖已绑定账户
|
|
22
|
+
|
|
23
|
+
### ⚠ Breaking
|
|
24
|
+
|
|
25
|
+
- `get_secret()` 返回值新增 `expires_in`,不再输出二维码;依赖终端 ASCII 二维码的调用方需自行渲染
|
|
26
|
+
- `get_secret()` 移除 `force` 参数,已绑定时一律抛 `RuntimeError`,需先 `delete(code)`
|
|
27
|
+
- `delete()` 已绑定时必须传当前验证码 `code`,否则抛 `ValueError`
|
|
28
|
+
|
|
29
|
+
## [2.1.0] - 2026-12-06
|
|
30
|
+
|
|
31
|
+
### Added
|
|
32
|
+
|
|
33
|
+
- 新增子进程/子线程执行器(`run_in_subprocess` / `run_in_subthread`,支持超时与异常透传),并提供顶层公共 API 与 `raytoolsbox.__version__`
|
|
34
|
+
- 安全模块增强:`get_secret(force=True)` 支持重新绑定,keyring 不可用时回退 600 权限文件存储,新增输入校验
|
|
35
|
+
- `send_email()` 支持 `timeout` 与 SSL/TLS;新增 `scripts/release.py` 一键发布脚本
|
|
36
|
+
|
|
37
|
+
### Changed
|
|
38
|
+
|
|
39
|
+
- 改用 `src/` layout,子包改为 PEP 562 惰性导入,不再连带加载无关依赖
|
|
40
|
+
- 库内 `print` 改为 `logging`,移除 `__main__` 演示块与死循环;切换 Hatchling 构建,重构测试与 `example/` 目录结构
|
|
41
|
+
- 版本管理改为手动,`uv version --bump` 升版本(同步 `uv.lock`),tag 在发布成功后创建
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
|
|
45
|
+
- `CryptoManager` 修复空 PIN 无法加载,非法参数改为先校验并抛 `TypeError`,不再静默返回 `None`
|
|
46
|
+
- `run_in_subprocess()` 无法 pickle 时抛出带原始原因的异常;PIN 哈希比较改用 `hmac.compare_digest`;`make_timer()` 改用单调时钟
|
|
47
|
+
|
|
48
|
+
### Removed
|
|
49
|
+
|
|
50
|
+
- 移除 `python-semantic-release` 及配置、早期重复实现、构建残留与示例多余开发依赖
|
|
51
|
+
|
|
52
|
+
### ⚠ Breaking
|
|
53
|
+
|
|
54
|
+
- `get_secret()` 已绑定时改抛 `RuntimeError`;`decrypt()` / `verify()` 参数类型错误改抛 `TypeError`
|
|
55
|
+
|
|
56
|
+
## [2.0.1] - 2026-07-03
|
|
57
|
+
|
|
58
|
+
### Added
|
|
59
|
+
|
|
60
|
+
- 集成 `python-semantic-release` 自动版本管理
|
|
61
|
+
|
|
62
|
+
### Changed
|
|
63
|
+
|
|
64
|
+
- `uv` 全面替代 `pip`/`poetry` 管理依赖和构建
|
|
65
|
+
|
|
66
|
+
## [1.1.2] - 2026-02-05
|
|
67
|
+
|
|
68
|
+
### Fixed
|
|
69
|
+
|
|
70
|
+
- 修复资源路径获取逻辑,增强 PyInstaller 打包兼容性
|
|
71
|
+
|
|
72
|
+
### Changed
|
|
73
|
+
|
|
74
|
+
- 重构函数注释体系,统一文档风格
|
|
75
|
+
|
|
76
|
+
## [1.1.1] - 2026-02-02
|
|
77
|
+
|
|
78
|
+
### Fixed
|
|
79
|
+
|
|
80
|
+
- 调整 Python 支持范围为 `>=3.11`
|
|
81
|
+
|
|
82
|
+
### Changed
|
|
83
|
+
|
|
84
|
+
- Python 版本从 3.13 降级至 3.11,提升兼容性
|
|
85
|
+
|
|
86
|
+
## [1.1.0] - 2026-02-02
|
|
87
|
+
|
|
88
|
+
### Added
|
|
89
|
+
|
|
90
|
+
- **PinManager** — PBKDF2-HMAC-SHA256 PIN 码哈希存储模块
|
|
91
|
+
- **Authenticator** — TOTP 双因素认证模块
|
|
92
|
+
|
|
93
|
+
### Changed
|
|
94
|
+
|
|
95
|
+
- 重构项目结构,将所有密码学相关操作归入 `security` 子包
|
|
96
|
+
|
|
97
|
+
## [1.0.0] - 2026-02-01
|
|
98
|
+
|
|
99
|
+
### Added
|
|
100
|
+
|
|
101
|
+
- 项目初始发布
|
|
102
|
+
- **CryptoManager** — ECC 加密套件(X25519 + ChaCha20-Poly1305 + Ed25519 签名)
|
|
103
|
+
- 邮件发送、性能监控、资源路径解析等工具模块
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
各版本标签:`v1.0.0` `v1.1.0` `v1.1.1` `v1.1.2` `2.0.1`
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TIMLES
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: raytoolsbox
|
|
3
|
-
Version:
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 3.0.0
|
|
4
|
+
Summary: 个人 Python 工具箱:常用开发工具、性能监控与安全模块(加密签名 / TOTP / PIN)
|
|
5
|
+
Project-URL: Homepage, https://github.com/TIMLES/RayToolsbox
|
|
6
|
+
Project-URL: Repository, https://github.com/TIMLES/RayToolsbox
|
|
7
|
+
Project-URL: Documentation, https://github.com/TIMLES/RayToolsbox/tree/main/example
|
|
8
|
+
Project-URL: Changelog, https://github.com/TIMLES/RayToolsbox/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: Issues, https://github.com/TIMLES/RayToolsbox/issues
|
|
5
10
|
Author: TIMLES@https://github.com/TIMLES
|
|
6
11
|
License: MIT
|
|
12
|
+
License-File: LICENSE
|
|
7
13
|
Requires-Python: >=3.11
|
|
8
14
|
Requires-Dist: cryptography>=46.0.4
|
|
9
15
|
Requires-Dist: keyring>=25.7.0
|
|
10
16
|
Requires-Dist: psutil>=7.2.2
|
|
11
17
|
Requires-Dist: pyotp>=2.9.0
|
|
12
18
|
Requires-Dist: pyyaml>=6.0.3
|
|
13
|
-
Requires-Dist: qrcode>=8.2
|
|
14
19
|
Description-Content-Type: text/markdown
|
|
15
20
|
|
|
16
21
|
|
|
@@ -31,6 +36,9 @@ Description-Content-Type: text/markdown
|
|
|
31
36
|
- 🔐 **现代化加密工具**
|
|
32
37
|
使用 X25519 密钥交换 + ChaCha20-Poly1305 对称加密 + Ed25519 数字签名,支持文本、字节和文件的加密解密与签名验签。
|
|
33
38
|
|
|
39
|
+
- 🛡️ **TOTP 双因素认证与 PIN 管理**
|
|
40
|
+
基于 RFC 6238 的 TOTP 绑定/验证/解绑,密钥存入系统 keyring(不可用时回退 600 权限文件);PIN 使用 PBKDF2-SHA256 加盐哈希,常数时间比较。库只返回数据,二维码由调用方渲染。
|
|
41
|
+
|
|
34
42
|
- ⏱️ **性能监控工具**
|
|
35
43
|
简易性能计时函数 `make_timer()` 与内存监控函数 `make_memory_monitor()`,
|
|
36
44
|
以及完整 benchmark 测试工具 `to_test_func()`,适合代码性能分析和调试。
|
|
@@ -103,76 +111,46 @@ decrypted = cm.decrypt(encrypted)
|
|
|
103
111
|
signed = cm.sign("重要数据", sign_key)
|
|
104
112
|
verified = cm.verify(signed, verify_key)
|
|
105
113
|
```
|
|
114
|
+
|
|
106
115
|
---
|
|
107
116
|
|
|
108
|
-
##
|
|
117
|
+
## 🔐 安全模块(TOTP / PIN)
|
|
109
118
|
|
|
110
|
-
|
|
111
|
-
raytoolsbox/
|
|
112
|
-
│
|
|
113
|
-
├── pyproject.toml # 项目信息 + 依赖管理(uv)
|
|
114
|
-
├── README.md # 项目文档
|
|
115
|
-
├── src/
|
|
116
|
-
│ └── raytoolsbox/
|
|
117
|
-
│ ├── __init__.py
|
|
118
|
-
│ ├── utils/
|
|
119
|
-
│ │ ├── __init__.py
|
|
120
|
-
│ │ ├── useful_func.py # 资源路径、窗口居中、子进程/子线程执行器
|
|
121
|
-
│ │ ├── mailToPhone.py # 邮件发送工具
|
|
122
|
-
│ │ └── monitor_func.py # 性能计时器、内存监控、benchmark 测试
|
|
123
|
-
│ └── security/
|
|
124
|
-
│ ├── __init__.py
|
|
125
|
-
│ ├── crypto_manager.py # ECC 加密/签名工具
|
|
126
|
-
│ ├── authenticator.py
|
|
127
|
-
│ └── pin_manager.py
|
|
128
|
-
│
|
|
129
|
-
└── tests/
|
|
130
|
-
├── test_authenticator.py # TOTP 认证测试
|
|
131
|
-
├── test_crypto_manager.py # 加密/签名 + fuzz 测试
|
|
132
|
-
├── test_mail_to_phone.py # 邮件发送测试
|
|
133
|
-
├── test_monitor_func.py # 性能监控测试
|
|
134
|
-
├── test_pin_manager.py # PIN 管理测试
|
|
135
|
-
└── test_useful_func.py # 路径/窗口/子进程工具测试
|
|
136
|
-
```
|
|
119
|
+
### TOTP 双因素认证
|
|
137
120
|
|
|
138
|
-
|
|
121
|
+
库只负责生成/校验并保存密钥,**不生成二维码**——`get_secret()` 返回 `uri` 供任意 QR 库渲染(`qrcode`、`segno`、前端的 `qrcodejs` 都行)。
|
|
139
122
|
|
|
140
|
-
|
|
123
|
+
```python
|
|
124
|
+
from raytoolsbox.security import Authenticator
|
|
141
125
|
|
|
142
|
-
|
|
126
|
+
auth = Authenticator("MyApp", "alice")
|
|
143
127
|
|
|
144
|
-
|
|
145
|
-
|
|
128
|
+
if not auth.exist():
|
|
129
|
+
info = auth.get_secret() # {"uri", "secret", "expires_in"}
|
|
130
|
+
# info["uri"] → otpauth://totp/... 交给 QR 库画图
|
|
131
|
+
# info["secret"] → 手机端手动录入用
|
|
132
|
+
# info["expires_in"] → 待绑定密钥有效期(秒),过期需重新获取
|
|
133
|
+
auth.verify(input("6 位验证码:")) # 首次验证通过即完成绑定
|
|
134
|
+
|
|
135
|
+
auth.verify(code) # 之后每次校验,window=1 容忍 ±30 秒时钟误差
|
|
136
|
+
auth.delete(code) # 解绑需提供当前验证码
|
|
146
137
|
```
|
|
147
138
|
|
|
148
|
-
|
|
139
|
+
已绑定时 `get_secret()` 抛出 `RuntimeError`,必须先 `delete(code)`;待绑定密钥有 5 分钟有效期与 5 次失败上限。
|
|
149
140
|
|
|
150
|
-
|
|
151
|
-
58 passed
|
|
152
|
-
```
|
|
141
|
+
### PIN 管理
|
|
153
142
|
|
|
154
|
-
|
|
143
|
+
```python
|
|
144
|
+
from raytoolsbox.security import PinManager
|
|
145
|
+
|
|
146
|
+
pm = PinManager("MyApp")
|
|
147
|
+
pm.set_pin("1234")
|
|
148
|
+
pm.verify_pin("1234") # "OK" / "FAIL" / "not_set"
|
|
149
|
+
pm.change_pin("1234", "5678") # 需提供旧 PIN
|
|
150
|
+
```
|
|
155
151
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
### 已完成
|
|
159
|
-
- [x] 邮件发送工具封装(支持多 SMTP 服务)
|
|
160
|
-
- [x] 可本地覆盖的配置系统
|
|
161
|
-
- [x] pytest 自动化测试(mock 网络发送)
|
|
162
|
-
- [x] 现代化加密模块(X25519 + ChaCha20-Poly1305 + Ed25519)
|
|
163
|
-
- [x] 数字签名与验签功能
|
|
164
|
-
- [x] 计时工具
|
|
165
|
-
- [x] 窗口居中工具
|
|
166
|
-
- [x] 资源路径获取(PyInstaller 兼容)
|
|
167
|
-
|
|
168
|
-
### 开发中 / 计划中
|
|
169
|
-
- [ ] 屏幕工具(DPI、尺寸测量)
|
|
170
|
-
- [ ] Tkinter / PySide6 GUI 工具类
|
|
171
|
-
- [ ] 文件操作工具(临时文件、下载器)
|
|
172
|
-
- [ ] 日期工具、定时工具、任务提醒系统
|
|
173
|
-
- [ ] 扩展 CLI(命令行使用工具箱)
|
|
174
|
-
|
|
175
|
-
如果你有想加入的功能欢迎告诉我!
|
|
152
|
+
> 完整可运行示例(FastAPI + 浏览器交互,含二维码渲染与解绑):
|
|
153
|
+
> <https://github.com/TIMLES/RayToolsbox/tree/main/example>
|
|
176
154
|
|
|
177
155
|
---
|
|
178
156
|
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
- 🔐 **现代化加密工具**
|
|
17
17
|
使用 X25519 密钥交换 + ChaCha20-Poly1305 对称加密 + Ed25519 数字签名,支持文本、字节和文件的加密解密与签名验签。
|
|
18
18
|
|
|
19
|
+
- 🛡️ **TOTP 双因素认证与 PIN 管理**
|
|
20
|
+
基于 RFC 6238 的 TOTP 绑定/验证/解绑,密钥存入系统 keyring(不可用时回退 600 权限文件);PIN 使用 PBKDF2-SHA256 加盐哈希,常数时间比较。库只返回数据,二维码由调用方渲染。
|
|
21
|
+
|
|
19
22
|
- ⏱️ **性能监控工具**
|
|
20
23
|
简易性能计时函数 `make_timer()` 与内存监控函数 `make_memory_monitor()`,
|
|
21
24
|
以及完整 benchmark 测试工具 `to_test_func()`,适合代码性能分析和调试。
|
|
@@ -88,76 +91,46 @@ decrypted = cm.decrypt(encrypted)
|
|
|
88
91
|
signed = cm.sign("重要数据", sign_key)
|
|
89
92
|
verified = cm.verify(signed, verify_key)
|
|
90
93
|
```
|
|
94
|
+
|
|
91
95
|
---
|
|
92
96
|
|
|
93
|
-
##
|
|
97
|
+
## 🔐 安全模块(TOTP / PIN)
|
|
94
98
|
|
|
95
|
-
|
|
96
|
-
raytoolsbox/
|
|
97
|
-
│
|
|
98
|
-
├── pyproject.toml # 项目信息 + 依赖管理(uv)
|
|
99
|
-
├── README.md # 项目文档
|
|
100
|
-
├── src/
|
|
101
|
-
│ └── raytoolsbox/
|
|
102
|
-
│ ├── __init__.py
|
|
103
|
-
│ ├── utils/
|
|
104
|
-
│ │ ├── __init__.py
|
|
105
|
-
│ │ ├── useful_func.py # 资源路径、窗口居中、子进程/子线程执行器
|
|
106
|
-
│ │ ├── mailToPhone.py # 邮件发送工具
|
|
107
|
-
│ │ └── monitor_func.py # 性能计时器、内存监控、benchmark 测试
|
|
108
|
-
│ └── security/
|
|
109
|
-
│ ├── __init__.py
|
|
110
|
-
│ ├── crypto_manager.py # ECC 加密/签名工具
|
|
111
|
-
│ ├── authenticator.py
|
|
112
|
-
│ └── pin_manager.py
|
|
113
|
-
│
|
|
114
|
-
└── tests/
|
|
115
|
-
├── test_authenticator.py # TOTP 认证测试
|
|
116
|
-
├── test_crypto_manager.py # 加密/签名 + fuzz 测试
|
|
117
|
-
├── test_mail_to_phone.py # 邮件发送测试
|
|
118
|
-
├── test_monitor_func.py # 性能监控测试
|
|
119
|
-
├── test_pin_manager.py # PIN 管理测试
|
|
120
|
-
└── test_useful_func.py # 路径/窗口/子进程工具测试
|
|
121
|
-
```
|
|
99
|
+
### TOTP 双因素认证
|
|
122
100
|
|
|
123
|
-
|
|
101
|
+
库只负责生成/校验并保存密钥,**不生成二维码**——`get_secret()` 返回 `uri` 供任意 QR 库渲染(`qrcode`、`segno`、前端的 `qrcodejs` 都行)。
|
|
124
102
|
|
|
125
|
-
|
|
103
|
+
```python
|
|
104
|
+
from raytoolsbox.security import Authenticator
|
|
126
105
|
|
|
127
|
-
|
|
106
|
+
auth = Authenticator("MyApp", "alice")
|
|
128
107
|
|
|
129
|
-
|
|
130
|
-
|
|
108
|
+
if not auth.exist():
|
|
109
|
+
info = auth.get_secret() # {"uri", "secret", "expires_in"}
|
|
110
|
+
# info["uri"] → otpauth://totp/... 交给 QR 库画图
|
|
111
|
+
# info["secret"] → 手机端手动录入用
|
|
112
|
+
# info["expires_in"] → 待绑定密钥有效期(秒),过期需重新获取
|
|
113
|
+
auth.verify(input("6 位验证码:")) # 首次验证通过即完成绑定
|
|
114
|
+
|
|
115
|
+
auth.verify(code) # 之后每次校验,window=1 容忍 ±30 秒时钟误差
|
|
116
|
+
auth.delete(code) # 解绑需提供当前验证码
|
|
131
117
|
```
|
|
132
118
|
|
|
133
|
-
|
|
119
|
+
已绑定时 `get_secret()` 抛出 `RuntimeError`,必须先 `delete(code)`;待绑定密钥有 5 分钟有效期与 5 次失败上限。
|
|
134
120
|
|
|
135
|
-
|
|
136
|
-
58 passed
|
|
137
|
-
```
|
|
121
|
+
### PIN 管理
|
|
138
122
|
|
|
139
|
-
|
|
123
|
+
```python
|
|
124
|
+
from raytoolsbox.security import PinManager
|
|
125
|
+
|
|
126
|
+
pm = PinManager("MyApp")
|
|
127
|
+
pm.set_pin("1234")
|
|
128
|
+
pm.verify_pin("1234") # "OK" / "FAIL" / "not_set"
|
|
129
|
+
pm.change_pin("1234", "5678") # 需提供旧 PIN
|
|
130
|
+
```
|
|
140
131
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
### 已完成
|
|
144
|
-
- [x] 邮件发送工具封装(支持多 SMTP 服务)
|
|
145
|
-
- [x] 可本地覆盖的配置系统
|
|
146
|
-
- [x] pytest 自动化测试(mock 网络发送)
|
|
147
|
-
- [x] 现代化加密模块(X25519 + ChaCha20-Poly1305 + Ed25519)
|
|
148
|
-
- [x] 数字签名与验签功能
|
|
149
|
-
- [x] 计时工具
|
|
150
|
-
- [x] 窗口居中工具
|
|
151
|
-
- [x] 资源路径获取(PyInstaller 兼容)
|
|
152
|
-
|
|
153
|
-
### 开发中 / 计划中
|
|
154
|
-
- [ ] 屏幕工具(DPI、尺寸测量)
|
|
155
|
-
- [ ] Tkinter / PySide6 GUI 工具类
|
|
156
|
-
- [ ] 文件操作工具(临时文件、下载器)
|
|
157
|
-
- [ ] 日期工具、定时工具、任务提醒系统
|
|
158
|
-
- [ ] 扩展 CLI(命令行使用工具箱)
|
|
159
|
-
|
|
160
|
-
如果你有想加入的功能欢迎告诉我!
|
|
132
|
+
> 完整可运行示例(FastAPI + 浏览器交互,含二维码渲染与解绑):
|
|
133
|
+
> <https://github.com/TIMLES/RayToolsbox/tree/main/example>
|
|
161
134
|
|
|
162
135
|
---
|
|
163
136
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "raytoolsbox"
|
|
3
|
+
version = "3.0.0"
|
|
4
|
+
description = "个人 Python 工具箱:常用开发工具、性能监控与安全模块(加密签名 / TOTP / PIN)"
|
|
5
|
+
authors = [{ name = "TIMLES@https://github.com/TIMLES" }]
|
|
6
|
+
license = {text = "MIT"}
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"cryptography>=46.0.4",
|
|
11
|
+
"keyring>=25.7.0",
|
|
12
|
+
"psutil>=7.2.2",
|
|
13
|
+
"pyotp>=2.9.0",
|
|
14
|
+
"pyyaml>=6.0.3",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://github.com/TIMLES/RayToolsbox"
|
|
19
|
+
Repository = "https://github.com/TIMLES/RayToolsbox"
|
|
20
|
+
Documentation = "https://github.com/TIMLES/RayToolsbox/tree/main/example"
|
|
21
|
+
Changelog = "https://github.com/TIMLES/RayToolsbox/blob/main/CHANGELOG.md"
|
|
22
|
+
Issues = "https://github.com/TIMLES/RayToolsbox/issues"
|
|
23
|
+
|
|
24
|
+
# 开发依赖(不会随包发布)
|
|
25
|
+
[dependency-groups]
|
|
26
|
+
dev = [
|
|
27
|
+
"fastapi>=0.115",
|
|
28
|
+
"hypothesis>=6.151.4",
|
|
29
|
+
"pytest>=9.0.2",
|
|
30
|
+
"uvicorn>=0.32",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
# 指定构建系统
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["hatchling"]
|
|
36
|
+
build-backend = "hatchling.build"
|
|
37
|
+
|
|
38
|
+
# 指定源码目录(src/ layout)
|
|
39
|
+
[tool.hatch.build.targets.wheel]
|
|
40
|
+
packages = ["src/raytoolsbox"]
|
|
41
|
+
|
|
42
|
+
# sdist 只包含包源码与测试,example/ 与 scripts/ 不随包发布
|
|
43
|
+
[tool.hatch.build.targets.sdist]
|
|
44
|
+
include = [
|
|
45
|
+
"src/raytoolsbox",
|
|
46
|
+
"tests",
|
|
47
|
+
"pyproject.toml",
|
|
48
|
+
"README.md",
|
|
49
|
+
"CHANGELOG.md",
|
|
50
|
+
]
|
|
51
|
+
exclude = ["example", "scripts"]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""raytoolsbox — 个人 Python 工具箱:常用开发工具 + 安全模块。
|
|
2
|
+
|
|
3
|
+
顶层只暴露版本号与两个子包,具体功能请从子包导入,例如::
|
|
4
|
+
|
|
5
|
+
from raytoolsbox.security import CryptoManager
|
|
6
|
+
from raytoolsbox.utils import make_timer
|
|
7
|
+
|
|
8
|
+
子包内部同样按需导入(PEP 562),因此导入某个具体模块不会连带加载同包
|
|
9
|
+
其他模块的依赖。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
__all__ = ["__version__", "security", "utils"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def __getattr__(name: str) -> Any:
|
|
20
|
+
# __version__ 惰性计算:importlib.metadata 的导入本身就要几十毫秒,
|
|
21
|
+
# 不该让「只想 import raytoolsbox」的调用方为它买单。
|
|
22
|
+
if name != "__version__":
|
|
23
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
24
|
+
|
|
25
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
value = version("raytoolsbox")
|
|
29
|
+
except PackageNotFoundError: # 源码目录下未安装为发行版时
|
|
30
|
+
value = "0.0.0"
|
|
31
|
+
globals()["__version__"] = value # 缓存,后续访问走正常属性查找
|
|
32
|
+
return value
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def __dir__() -> list[str]:
|
|
36
|
+
return sorted(__all__)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""子包惰性导入辅助。
|
|
2
|
+
|
|
3
|
+
包 ``__init__.py`` 里如果直接 ``from .xxx import Yyy``,那么用户哪怕只想用
|
|
4
|
+
其中一个模块,也会把兄弟模块的依赖全部导入(例如只想用 ``monitor_func``,
|
|
5
|
+
却连带拉进 ``smtplib``、``yaml``、``multiprocessing``)。这里用 PEP 562 的
|
|
6
|
+
模块级 ``__getattr__`` 把导入推迟到真正访问的那一刻。
|
|
7
|
+
|
|
8
|
+
用法:
|
|
9
|
+
|
|
10
|
+
.. code-block:: python
|
|
11
|
+
|
|
12
|
+
from raytoolsbox._lazy import make_module_getattr
|
|
13
|
+
|
|
14
|
+
_EXPORTS = {"make_timer": "monitor_func"}
|
|
15
|
+
|
|
16
|
+
__getattr__ = make_module_getattr(globals(), _EXPORTS)
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import importlib
|
|
22
|
+
from collections.abc import Callable
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
__all__ = ["make_module_getattr"]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def make_module_getattr(module_globals: dict[str, Any], exports: dict[str, str]) -> Callable[[str], Any]:
|
|
29
|
+
"""构造一个模块级 ``__getattr__``,按需导入 ``exports`` 里的名字。
|
|
30
|
+
|
|
31
|
+
Parameters
|
|
32
|
+
----------
|
|
33
|
+
module_globals : dict
|
|
34
|
+
调用方(包 ``__init__``)的 ``globals()``。命中的名字导入后会写回
|
|
35
|
+
这里做缓存,后续访问不再进入 ``__getattr__``。
|
|
36
|
+
exports : dict[str, str]
|
|
37
|
+
公开名字 → 所在子模块名的映射,例如 ``{"make_timer": "monitor_func"}``。
|
|
38
|
+
|
|
39
|
+
Returns
|
|
40
|
+
-------
|
|
41
|
+
Callable[[str], Any]
|
|
42
|
+
可直接赋给模块的 ``__getattr__``。
|
|
43
|
+
"""
|
|
44
|
+
module_name = module_globals["__name__"]
|
|
45
|
+
|
|
46
|
+
def __getattr__(name: str) -> Any:
|
|
47
|
+
submodule_name = exports.get(name)
|
|
48
|
+
if submodule_name is None:
|
|
49
|
+
raise AttributeError(f"module {module_name!r} has no attribute {name!r}")
|
|
50
|
+
module = importlib.import_module(f".{submodule_name}", module_name)
|
|
51
|
+
value = getattr(module, name)
|
|
52
|
+
module_globals[name] = value # 缓存,后续访问走正常属性查找
|
|
53
|
+
return value
|
|
54
|
+
|
|
55
|
+
return __getattr__
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""安全模块:加密与签名、TOTP 双因素认证、PIN 码管理。
|
|
2
|
+
|
|
3
|
+
各管理器按需导入(PEP 562),因此用 ``CryptoManager`` 时不会连带把
|
|
4
|
+
``keyring`` / ``pyotp`` 拉进内存。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from raytoolsbox._lazy import make_module_getattr
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING: # 仅供类型检查器与 IDE 补全,运行时不执行
|
|
14
|
+
from .authenticator import Authenticator
|
|
15
|
+
from .crypto_manager import CryptoManager
|
|
16
|
+
from .pin_manager import PinManager
|
|
17
|
+
|
|
18
|
+
__all__ = ["Authenticator", "CryptoManager", "PinManager"]
|
|
19
|
+
|
|
20
|
+
# 公开名字 → 所在子模块
|
|
21
|
+
_EXPORTS = {
|
|
22
|
+
"Authenticator": "authenticator",
|
|
23
|
+
"CryptoManager": "crypto_manager",
|
|
24
|
+
"PinManager": "pin_manager",
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
__getattr__ = make_module_getattr(globals(), _EXPORTS)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def __dir__() -> list[str]:
|
|
31
|
+
return sorted(__all__)
|