{"library":"types-pyopenssl","code":"from OpenSSL import crypto, SSL\nfrom datetime import datetime, timedelta\n\ndef generate_self_signed_cert(\n    common_name: str,\n    country: str,\n    state: str,\n    city: str,\n    organization: str,\n    email: str,\n    valid_days: int = 365,\n) -> tuple[crypto.PKey, crypto.X509]:\n    \"\"\"\n    Generates a self-signed certificate and private key objects with type hints.\n    This demonstrates common pyOpenSSL types like PKey and X509.\n    \"\"\"\n    # Generate a new key pair\n    k = crypto.PKey()\n    k.generate_key(crypto.TYPE_RSA, 2048)\n\n    # Create a self-signed cert\n    cert = crypto.X509()\n    cert.get_subject().C = country\n    cert.get_subject().ST = state\n    cert.get_subject().L = city\n    cert.get_subject().O = organization\n    cert.get_subject().CN = common_name\n    cert.get_subject().emailAddress = email\n    cert.set_serial_number(1000)\n    # Dates must be in UTC. Format 'YYYYMMDDHHMMSSZ'\n    cert.gmtime_before = (datetime.utcnow() - timedelta(days=1)).strftime('%Y%m%d%H%M%SZ')\n    cert.gmtime_after = (datetime.utcnow() + timedelta(days=valid_days)).strftime('%Y%m%d%H%M%SZ')\n    cert.set_issuer(cert.get_subject())\n    cert.set_pubkey(k)\n    cert.sign(k, 'sha256')\n\n    return k, cert\n\n# Example usage with type-hinted variables\nprivate_key: crypto.PKey\ncertificate: crypto.X509\nprivate_key, certificate = generate_self_signed_cert(\n    common_name=\"example.com\",\n    country=\"US\",\n    state=\"CA\",\n    city=\"San Francisco\",\n    organization=\"Example Org\",\n    email=\"admin@example.com\"\n)\n\nprint(\"Generated private key and certificate objects with type hints.\")\nprint(f\"Certificate subject CN: {certificate.get_subject().CN}\")\nprint(f\"Private key type: {private_key.type()}\")","lang":"python","description":"This quickstart demonstrates how to use `types-pyopenssl` by creating a self-signed certificate and private key, showcasing type annotations for common `pyOpenSSL` objects like `PKey` and `X509`. Type checkers would use these stubs to validate the types in such code.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}