def _mgf1(hash_algorithm, seed, mask_length):
if not isinstance(seed, byte_cls):<EOL><INDENT>raise TypeError(pretty_message(<EOL>'''<STR_LIT>''',<EOL>type_name(seed)<EOL>))<EOL><DEDENT>if not isinstance(mask_length, int_types):<EOL><INDENT>raise TypeError(pretty_message(<EOL>'''<STR_LIT>''',<EOL>type_name(mask_length)<EOL>))<EOL><DEDENT>if mask_length < <NUM_LIT:1>:<EOL><INDENT>raise ValueError(pretty_message(<EOL>'''<STR_LIT>''',<EOL>repr(mask_length)<EOL>))<EOL><DEDENT>if hash_algorithm not in set(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']):<EOL><INDENT>raise ValueError(pretty_message(<EOL>'''<STR_LIT>''',<EOL>repr(hash_algorithm)<EOL>))<EOL><DEDENT>output = b'<STR_LIT>'<EOL>hash_length = {<EOL>'<STR_LIT>': <NUM_LIT:20>,<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT:32>,<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT:64><EOL>}[hash_algorithm]<EOL>iterations = int(math.ceil(mask_length / hash_length))<EOL>pack = struct.Struct(b'<STR_LIT>').pack<EOL>hash_func = getattr(hashlib, hash_algorithm)<EOL>for counter in range(<NUM_LIT:0>, iterations):<EOL><INDENT>b = pack(counter)<EOL>output += hash_func(seed + b).digest()<EOL><DEDENT>return output[<NUM_LIT:0>:mask_length]<EOL>
The PKCS#1 MGF1 mask generation algorithm

:param hash_algorithm:
    The string name of the hash algorithm to use: "sha1", "sha224",
    "sha256", "sha384", "sha512"

:param seed:
    A byte string to use as the seed for the mask

:param mask_length:
    The desired mask length, as an integer

:return:
    A byte string of the mask