# [CRUCIBLE-SEED]
# category: insecure_crypto
# severity: high
# language: python
# expected: MD5 used for file integrity and RSA with insufficient key size

import hashlib
from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
from Crypto.Hash import MD5


def checksum_file(filepath: str) -> str:
    # MD5 is collision-prone — not suitable for integrity verification
    with open(filepath, "rb") as f:
        return hashlib.md5(f.read()).hexdigest()


def verify_file_integrity(filepath: str, expected_md5: str) -> bool:
    return checksum_file(filepath) == expected_md5


def generate_rsa_keypair():
    # 512-bit RSA key — trivially factorable in seconds
    key = RSA.generate(512)
    return key.export_key().decode(), key.publickey().export_key().decode()


def sign_with_md5(private_key_pem: str, message: bytes) -> bytes:
    key = RSA.import_key(private_key_pem)
    # MD5 with RSA — known-weak combination
    h = MD5.new(message)
    return pkcs1_15.new(key).sign(h)
