Spaces:
Sleeping
Sleeping
| """ | |
| QCrypt RNG API - Request Models | |
| Pydantic models for API request validation | |
| """ | |
| from pydantic import BaseModel, Field, validator | |
| from typing import Optional, Literal, List | |
| from enum import Enum | |
| class OutputFormat(str, Enum): | |
| """Supported output formats for random data""" | |
| HEX = "hex" | |
| BASE64 = "base64" | |
| ARRAY = "array" | |
| RAW = "raw" | |
| class Algorithm(str, Enum): | |
| """Supported cryptographic algorithms""" | |
| AES = "AES" | |
| RSA = "RSA" | |
| ECDSA = "ECDSA" | |
| class GenerateBytesRequest(BaseModel): | |
| """Request model for byte generation""" | |
| length: int = Field( | |
| ..., | |
| ge=1, | |
| le=10240, | |
| description="Number of random bytes to generate" | |
| ) | |
| format: OutputFormat = Field( | |
| OutputFormat.HEX, | |
| description="Output format for the generated bytes" | |
| ) | |
| quantum_bits: Optional[int] = Field( | |
| 8, | |
| ge=1, | |
| le=16, | |
| description="Number of qubits to use for generation" | |
| ) | |
| class Config: | |
| schema_extra = { | |
| "example": { | |
| "length": 32, | |
| "format": "hex", | |
| "quantum_bits": 8 | |
| } | |
| } | |
| def validate_length(cls, v): | |
| """Validate byte length based on tier limits""" | |
| # In production, this would check against user's tier | |
| max_length = 1024 # Default limit | |
| if v > max_length: | |
| raise ValueError(f"Length exceeds maximum of {max_length} bytes") | |
| return v | |
| class GenerateKeyRequest(BaseModel): | |
| """Request model for cryptographic key generation""" | |
| algorithm: Algorithm = Field( | |
| ..., | |
| description="Cryptographic algorithm" | |
| ) | |
| key_size: int = Field( | |
| ..., | |
| description="Key size in bits" | |
| ) | |
| format: Literal["hex", "base64", "pem"] = Field( | |
| "hex", | |
| description="Output format for the key" | |
| ) | |
| class Config: | |
| schema_extra = { | |
| "example": { | |
| "algorithm": "AES", | |
| "key_size": 256, | |
| "format": "hex" | |
| } | |
| } | |
| def validate_key_size(cls, v, values): | |
| """Validate key size for algorithm""" | |
| if 'algorithm' not in values: | |
| return v | |
| valid_sizes = { | |
| Algorithm.AES: [128, 192, 256], | |
| Algorithm.RSA: [2048, 3072, 4096], | |
| Algorithm.ECDSA: [256, 384, 521] | |
| } | |
| algorithm = values['algorithm'] | |
| if v not in valid_sizes.get(algorithm, []): | |
| raise ValueError( | |
| f"Invalid key size {v} for {algorithm}. " | |
| f"Valid sizes: {valid_sizes[algorithm]}" | |
| ) | |
| return v | |
| class GenerateTokenRequest(BaseModel): | |
| """Request model for session token generation""" | |
| length: int = Field( | |
| 32, | |
| ge=16, | |
| le=128, | |
| description="Token length in bytes" | |
| ) | |
| url_safe: bool = Field( | |
| True, | |
| description="Generate URL-safe token" | |
| ) | |
| expires_in: Optional[int] = Field( | |
| 3600, | |
| ge=60, | |
| le=31536000, | |
| description="Token expiration time in seconds" | |
| ) | |
| class Config: | |
| schema_extra = { | |
| "example": { | |
| "length": 32, | |
| "url_safe": True, | |
| "expires_in": 3600 | |
| } | |
| } | |
| class GenerateUUIDRequest(BaseModel): | |
| """Request model for UUID generation""" | |
| version: Literal[4] = Field( | |
| 4, | |
| description="UUID version (only v4 currently supported)" | |
| ) | |
| count: int = Field( | |
| 1, | |
| ge=1, | |
| le=100, | |
| description="Number of UUIDs to generate" | |
| ) | |
| format: Literal["standard", "raw", "urn"] = Field( | |
| "standard", | |
| description="UUID format" | |
| ) | |
| class Config: | |
| schema_extra = { | |
| "example": { | |
| "version": 4, | |
| "count": 1, | |
| "format": "standard" | |
| } | |
| } | |
| class GeneratePasswordRequest(BaseModel): | |
| """Request model for password generation""" | |
| length: int = Field( | |
| 16, | |
| ge=8, | |
| le=64, | |
| description="Password length" | |
| ) | |
| include_uppercase: bool = Field( | |
| True, | |
| description="Include uppercase letters" | |
| ) | |
| include_lowercase: bool = Field( | |
| True, | |
| description="Include lowercase letters" | |
| ) | |
| include_numbers: bool = Field( | |
| True, | |
| description="Include numbers" | |
| ) | |
| include_symbols: bool = Field( | |
| True, | |
| description="Include special symbols" | |
| ) | |
| exclude_ambiguous: bool = Field( | |
| False, | |
| description="Exclude ambiguous characters (0, O, l, I, etc.)" | |
| ) | |
| min_uppercase: Optional[int] = Field( | |
| None, | |
| ge=0, | |
| le=10, | |
| description="Minimum uppercase letters" | |
| ) | |
| min_lowercase: Optional[int] = Field( | |
| None, | |
| ge=0, | |
| le=10, | |
| description="Minimum lowercase letters" | |
| ) | |
| min_numbers: Optional[int] = Field( | |
| None, | |
| ge=0, | |
| le=10, | |
| description="Minimum numbers" | |
| ) | |
| min_symbols: Optional[int] = Field( | |
| None, | |
| ge=0, | |
| le=10, | |
| description="Minimum symbols" | |
| ) | |
| class Config: | |
| schema_extra = { | |
| "example": { | |
| "length": 16, | |
| "include_uppercase": True, | |
| "include_lowercase": True, | |
| "include_numbers": True, | |
| "include_symbols": True, | |
| "exclude_ambiguous": True | |
| } | |
| } | |
| def validate_minimum_requirements(cls, v, values): | |
| """Ensure length can accommodate minimum requirements""" | |
| total_min = sum([ | |
| values.get('min_uppercase', 0) or 0, | |
| values.get('min_lowercase', 0) or 0, | |
| values.get('min_numbers', 0) or 0, | |
| values.get('min_symbols', 0) or 0 | |
| ]) | |
| if total_min > v: | |
| raise ValueError( | |
| f"Password length ({v}) must be >= sum of minimum requirements ({total_min})" | |
| ) | |
| return v | |
| class BatchGenerateRequest(BaseModel): | |
| """Request model for batch generation""" | |
| requests: List[GenerateBytesRequest] = Field( | |
| ..., | |
| min_items=1, | |
| max_items=100, | |
| description="List of generation requests" | |
| ) | |
| parallel: bool = Field( | |
| True, | |
| description="Process requests in parallel" | |
| ) | |
| class Config: | |
| schema_extra = { | |
| "example": { | |
| "requests": [ | |
| {"length": 32, "format": "hex"}, | |
| {"length": 64, "format": "base64"}, | |
| {"length": 16, "format": "array"} | |
| ], | |
| "parallel": True | |
| } | |
| } |