import textwrap
import base64


def _bytes_to_pem_str(der_bytes, pem_type):
    """
    Utility function for creating PEM files

    Args:
        der_bytes: DER encoded bytes
        pem_type: type of PEM, e.g Certificate, Private key, or RSA private key

    Returns:
        PEM String for a DER-encoded certificate or private key
    """
    pem_str = ""
    pem_str = pem_str + "-----BEGIN {}-----".format(pem_type) + "\n"
    pem_str = pem_str + "\r\n".join(textwrap.wrap(base64.b64encode(der_bytes).decode('ascii'), 64)) + "\n"
    pem_str = pem_str + "-----END {}-----".format(pem_type) + "\n"
    return pem_str