import re
import base64


def _get_base64pad(data: str) -> str:
    """Base64 encodes with space padding to ensure output only contains [a-zA-Z0-9+]."""
    pattern = r"[^a-zA-Z0-9\+]"
    regex = re.compile(pattern)
    while True:
        ebytes = base64.b64encode(data.encode("utf-8"))
        estring = str(ebytes, "utf-8")
        if not regex.findall(estring):
            break
        # Pad with trailing space and try again to eliminate base64 pad chars
        data = data + " "

    return estring