feat(file): 优化文件处理和缓存机制

- 重构文件处理逻辑,提高性能和可维护性
- 增加缓存机制,减少重复读取和处理
- 改进错误处理和日志记录
- 优化缩略图生成算法
- 添加性能监控和测试依赖
This commit is contained in:
2025-07-11 00:21:57 +08:00
parent d0f9e65ad1
commit 8c4e5885c7
15 changed files with 1034 additions and 94 deletions

34
utils/security.py Normal file
View File

@@ -0,0 +1,34 @@
import hashlib
import secrets
import hmac
from typing import Optional
def hash_password(password: str, salt: Optional[str] = None) -> tuple[str, str]:
"""
哈希密码并返回哈希值和盐值
"""
if salt is None:
salt = secrets.token_hex(32)
password_hash = hashlib.pbkdf2_hmac(
'sha256',
password.encode('utf-8'),
salt.encode('utf-8'),
100000 # 迭代次数
)
return password_hash.hex(), salt
def verify_password(password: str, hashed_password: str, salt: str) -> bool:
"""
验证密码是否正确
"""
test_hash, _ = hash_password(password, salt)
return hmac.compare_digest(test_hash, hashed_password)
def generate_session_token() -> str:
"""
生成安全的会话令牌
"""
return secrets.token_urlsafe(32)