# Buggy code
def create_https_certificates(ssl_cert, ssl_key):
    """
    Create self-signed HTTPS certificares and store in paths 'ssl_cert' and 'ssl_key'

    :param ssl_cert: Path of SSL certificate file to write
    :param ssl_key: Path of SSL keyfile to write
    :return: True on success, False on failure
    """

    # assert isinstance(ssl_key, unicode)
    # assert isinstance(ssl_cert, unicode)

    try:
        from OpenSSL import crypto  # noinspection PyUnresolvedReferences
        from certgen import (
            createKeyPair,
            createCertRequest,
            createCertificate,
            TYPE_RSA,
            serial,
        )  # @UnresolvedImport
    except Exception:
        logger.log(
            "pyopenssl module missing, please install for https access", logger.WARNING
        )
        return False

    # Create the CA Certificate
    cakey = createKeyPair(TYPE_RSA, 4096)
    careq = createCertRequest(cakey, CN="Certificate Authority")
    cacert = createCertificate(
        careq, (careq, cakey), serial, (0, 60 * 60 * 24 * 365 * 10)
    )  # ten years

    cname = "SickRage"
    pkey = createKeyPair(TYPE_RSA, 4096)
    req = createCertRequest(pkey, CN=cname)
    cert = createCertificate(
        req, (cacert, cakey), serial, (0, 60 * 60 * 24 * 365 * 10)
    )  # ten years

    # Save the key and certificate to disk
    try:
        # pylint: disable=no-member
        # Module has no member
        io.open(ssl_key, "wb").write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey))
        io.open(ssl_cert, "wb").write(
            crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
        )
    except Exception:
        logger.log("Error creating SSL key and certificate", logger.ERROR)
        return False

    return True


# Traceback (SSLError)
Traceback (most recent call last):
File "/opt/sickrage/lib/tornado/ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "/opt/sickrage/lib/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/opt/sickrage/lib/tornado/netutil.py", line 276, in accept_handler
callback(connection, address)
File "/opt/sickrage/lib/tornado/tcpserver.py", line 264, in _handle_connection
do_handshake_on_connect=False)
File "/opt/sickrage/lib/tornado/netutil.py", line 517, in ssl_wrap_socket
context = ssl_options_to_context(ssl_options)
File "/opt/sickrage/lib/tornado/netutil.py", line 494, in ssl_options_to_context
context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))
SSLError: [SSL: CA_MD_TOO_WEAK] ca md too weak (_ssl.c:2699)

# Fixed code
def create_https_certificates(ssl_cert, ssl_key):
    """
    Create self-signed HTTPS certificares and store in paths 'ssl_cert' and 'ssl_key'

    :param ssl_cert: Path of SSL certificate file to write
    :param ssl_key: Path of SSL keyfile to write
    :return: True on success, False on failure
    """

    # assert isinstance(ssl_key, unicode)
    # assert isinstance(ssl_cert, unicode)

    try:
        # noinspection PyUnresolvedReferences
        from OpenSSL import crypto
        from certgen import (
            createKeyPair,
            createCertRequest,
            createCertificate,
            TYPE_RSA,
        )
    except Exception:
        logger.log(
            "pyopenssl module missing, please install for https access", logger.WARNING
        )
        return False

    import time

    serial = int(time.time())
    validity_period = (0, 60 * 60 * 24 * 365 * 10)  # ten years
    # Create the CA Certificate
    cakey = createKeyPair(TYPE_RSA, 4096)
    careq = createCertRequest(cakey, CN="Certificate Authority")
    cacert = createCertificate(careq, (careq, cakey), serial, validity_period, "sha256")

    cname = "SickRage"
    pkey = createKeyPair(TYPE_RSA, 4096)
    req = createCertRequest(pkey, CN=cname)
    cert = createCertificate(req, (cacert, cakey), serial, validity_period, "sha256")

    # Save the key and certificate to disk
    try:
        # pylint: disable=no-member
        # Module has no member
        io.open(ssl_key, "wb").write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey))
        io.open(ssl_cert, "wb").write(
            crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
        )
    except Exception:
        logger.log("Error creating SSL key and certificate", logger.ERROR)
        return False

    return True