def generate_key_bytes(hashclass, salt, key, nbytes):
keydata = '<STR_LIT>'<EOL>digest = '<STR_LIT>'<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 = hashclass.new()<EOL>if len(digest) > <NUM_LIT:0>:<EOL><INDENT>hash_obj.update(digest)<EOL><DEDENT>hash_obj.update(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 hashclass: class from L{Crypto.Hash} that can be used as a secure
    hashing function (like C{MD5} or C{SHA}).
@type hashclass: L{Crypto.Hash}
@param salt: data to salt the hash with.
@type salt: string
@param key: human-entered password or passphrase.
@type key: string
@param nbytes: number of bytes to generate.
@type nbytes: int
@return: key data
@rtype: string