def generate_key_bytes(hash_alg, salt, key, nbytes):
keydata = bytes()<EOL>digest = bytes()<EOL>if len(salt) > <NUM_LIT:8>:<EOL><INDENT>salt = salt[:<NUM_LIT:8>]<EOL><DEDENT>while nbytes > <NUM_LIT:0>:<EOL><INDENT>hash_obj = hash_alg()<EOL>if len(digest) > <NUM_LIT:0>:<EOL><INDENT>hash_obj.update(digest)<EOL><DEDENT>hash_obj.update(b(key))<EOL>hash_obj.update(salt)<EOL>digest = hash_obj.digest()<EOL>size = min(nbytes, len(digest))<EOL>keydata += digest[:size]<EOL>nbytes -= size<EOL><DEDENT>return keydata<EOL>
Given a password, passphrase, or other human-source key, scramble it
through a secure hash into some keyworthy bytes.  This specific algorithm
is used for encrypting/decrypting private key files.

:param function hash_alg: A function which creates a new hash object, such
    as ``hashlib.sha256``.
:param salt: data to salt the hash with.
:type salt: byte string
:param str key: human-entered password or passphrase.
:param int nbytes: number of bytes to generate.
:return: Key data `str`