mirror of
https://github.com/Kakune55/ComiPy.git
synced 2025-09-16 04:09:41 +08:00
feat(file): 优化文件处理和缓存机制
- 重构文件处理逻辑,提高性能和可维护性 - 增加缓存机制,减少重复读取和处理 - 改进错误处理和日志记录 - 优化缩略图生成算法 - 添加性能监控和测试依赖
This commit is contained in:
34
utils/security.py
Normal file
34
utils/security.py
Normal 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)
|
Reference in New Issue
Block a user