def generate_key_bytes(hash_alg, salt, key, nbytes):
 keydata = bytes()
 digest = bytes()
 if (len(salt) > 8):
  salt = salt[:8]
 while (nbytes > 0):
  hash_obj = hash_alg()
  if (len(digest) > 0):
   hash_obj.update(digest)
  hash_obj.update(b(key))
  hash_obj.update(salt)
  digest = hash_obj.digest()
  size = min(nbytes, len(digest))
  keydata += digest[:size]
  nbytes -= size
 return keydata

def generate_key_bytes ( hash_alg , salt , key , nbytes ) : keydata = bytes ( ) digest = bytes ( ) if ( len ( salt ) > 8 ) : salt = salt [ : 8 ] while ( nbytes > 0 ) : hash_obj = hash_alg ( ) if ( len ( digest ) > 0 ) : hash_obj . update ( digest ) hash_obj . update ( b ( key ) ) hash_obj . update ( salt ) digest = hash_obj . digest ( ) size = min ( nbytes , len ( digest ) ) keydata += digest [ : size ] nbytes -= size return keydata
given a password .