Files
autoPlus/utils/crypto.py
2026-01-26 15:04:02 +08:00

162 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
加密与指纹生成工具模块
提供以下功能:
- 生成 OpenAI 设备 ID (oai-did)
- 生成符合要求的强密码
- Proof of Work 挑战解决(预留接口)
"""
import uuid
import secrets
import string
from typing import Optional
def generate_oai_did() -> str:
"""
生成 OpenAI 设备 ID
使用 UUIDv4 格式,例如:
"a1b2c3d4-e5f6-4789-a012-b3c4d5e6f7a8"
返回:
36 个字符的 UUID 字符串(包含 4 个连字符)
"""
return str(uuid.uuid4())
def generate_random_password(length: int = 12) -> str:
"""
生成符合 OpenAI 要求的强密码
要求:
- 长度8-16 位(默认 12 位)
- 必须包含:大写字母、小写字母、数字
- 不包含特殊符号(避免编码问题)
参数:
length: 密码长度(默认 12
返回:
符合要求的随机密码
"""
if length < 8 or length > 16:
raise ValueError("Password length must be between 8 and 16")
# 字符集:大写字母 + 小写字母 + 数字
chars = string.ascii_letters + string.digits
# 重复生成直到满足所有条件
max_attempts = 100
for _ in range(max_attempts):
password = ''.join(secrets.choice(chars) for _ in range(length))
# 验证条件
has_lower = any(c.islower() for c in password)
has_upper = any(c.isupper() for c in password)
has_digit = any(c.isdigit() for c in password)
if has_lower and has_upper and has_digit:
return password
# 如果随机生成失败,手动构造一个符合要求的密码
# 确保至少有一个大写、一个小写、一个数字
parts = [
secrets.choice(string.ascii_uppercase), # 至少一个大写
secrets.choice(string.ascii_lowercase), # 至少一个小写
secrets.choice(string.digits), # 至少一个数字
]
# 填充剩余长度
remaining = length - len(parts)
parts.extend(secrets.choice(chars) for _ in range(remaining))
# 打乱顺序
password_list = list(parts)
for i in range(len(password_list) - 1, 0, -1):
j = secrets.randbelow(i + 1)
password_list[i], password_list[j] = password_list[j], password_list[i]
return ''.join(password_list)
def generate_proof_of_work(seed: str, difficulty: str, **kwargs) -> str:
"""
解决 Sentinel 的 Proof of Work 挑战
参数:
seed: PoW 种子值
difficulty: 难度参数
**kwargs: 其他可能需要的参数
返回:
PoW 答案字符串
抛出:
NotImplementedError: 用户需要实现此方法
"""
raise NotImplementedError(
"Proof of Work solver not implemented. "
"User has existing Sentinel solution that should be integrated here.\n"
"Integration options:\n"
"1. Call external script/service\n"
"2. Import existing Python module\n"
"3. HTTP API call to solver service"
)
def validate_oai_did(oai_did: str) -> bool:
"""
验证 oai-did 格式是否正确
参数:
oai_did: 待验证的设备 ID
返回:
True 如果格式正确,否则 False
"""
try:
# 尝试解析为 UUID
uuid_obj = uuid.UUID(oai_did)
# 验证是 UUIDv4
return uuid_obj.version == 4
except (ValueError, AttributeError):
return False
def validate_password(password: str) -> tuple[bool, Optional[str]]:
"""
验证密码是否符合 OpenAI 要求
参数:
password: 待验证的密码
返回:
(是否有效, 错误信息)
"""
if len(password) < 8 or len(password) > 16:
return False, "Password must be 8-16 characters"
if not any(c.islower() for c in password):
return False, "Password must contain at least one lowercase letter"
if not any(c.isupper() for c in password):
return False, "Password must contain at least one uppercase letter"
if not any(c.isdigit() for c in password):
return False, "Password must contain at least one digit"
return True, None
# 导出主要接口
__all__ = [
"generate_oai_did",
"generate_random_password",
"generate_proof_of_work",
"validate_oai_did",
"validate_password",
]