Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Killer #252

Open
caledrummond90 opened this issue Nov 23, 2023 · 0 comments
Open

Killer #252

caledrummond90 opened this issue Nov 23, 2023 · 0 comments

Comments

@caledrummond90
Copy link

from datetime import datetime, timedelta, timezone
from pathlib import Path

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.hashes import SHA256
from cryptography.hazmat.primitives.serialization import (
Encoding,
NoEncryption,
PrivateFormat,
)
from cryptography.x509 import (
CertificateBuilder,
DNSName,
Name,
NameAttribute,
SubjectAlternativeName,
random_serial_number,
)
from cryptography.x509.oid import NameOID

https://cryptography.io/en/latest/x509/tutorial/#creating-a-self-signed-certificate

def generate_keys():
folder = Path(file).parent

key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,
    backend=default_backend(),
)
(folder / "localhost.key").write_bytes(
    key.private_bytes(
        encoding=Encoding.PEM,
        format=PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=NoEncryption(),
    ),
)

subject = issuer = Name(
    [
        NameAttribute(NameOID.COUNTRY_NAME, "IE"),
        NameAttribute(NameOID.ORGANIZATION_NAME, "Scrapy"),
        NameAttribute(NameOID.COMMON_NAME, "localhost"),
    ]
)
cert = (
    CertificateBuilder()
    .subject_name(subject)
    .issuer_name(issuer)
    .public_key(key.public_key())
    .serial_number(random_serial_number())
    .not_valid_before(datetime.now(tz=timezone.utc))
    .not_valid_after(datetime.now(tz=timezone.utc) + timedelta(days=10))
    .add_extension(
        SubjectAlternativeName([DNSName("localhost")]),
        critical=False,
    )
    .sign(key, SHA256(), default_backend())
)
(folder / "localhost.crt").write_bytes(cert.public_bytes(Encoding.PEM))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant