from random import SystemRandom
from string import ascii_lowercase, ascii_uppercase, digits as ascii_digits
import os

__all__ = ["random_str", "random_bytes"]

random = SystemRandom()


def random_str(size, charset=ascii_lowercase + ascii_uppercase + ascii_digits):
    """Returns a randomly generated string"""
    return ''.join(random.choice(charset) for _ in range(size))


def random_bytes(size, badchars=b""):
    """Returns a randomly generated byte string"""
    rbytes = bytes(bytearray(b for b in os.urandom(size) if b not in badchars))
    while len(rbytes) < size:
        b = os.urandom(1)
        if b not in badchars:
            rbytes += b
    return rbytes
