def compute_hashes_from_fileobj(fileobj, chunk_size=(1024 * 1024)):
 if (six.PY3 and hasattr(fileobj, 'mode') and ('b' not in fileobj.mode)):
  raise ValueError('File-like object must be opened in binary mode!')
 linear_hash = hashlib.sha256()
 chunks = []
 chunk = fileobj.read(chunk_size)
 while chunk:
  if (not isinstance(chunk, bytes)):
   chunk = chunk.encode((getattr(fileobj, 'encoding', '') or 'utf-8'))
  linear_hash.update(chunk)
  chunks.append(hashlib.sha256(chunk).digest())
  chunk = fileobj.read(chunk_size)
 if (not chunks):
  chunks = [hashlib.sha256('').digest()]
 return (linear_hash.hexdigest(), bytes_to_hex(tree_hash(chunks)))

def compute_hashes_from_fileobj ( fileobj , chunk_size = ( 1024 * 1024 ) ) : if ( six . PY3 and hasattr ( fileobj , 'mode' ) and ( 'b' not in fileobj . mode ) ) : raise ValueError ( 'File-like object must be opened in binary mode!' ) linear_hash = hashlib . sha256 ( ) chunks = [ ] chunk = fileobj . read ( chunk_size ) while chunk : if ( not isinstance ( chunk , bytes ) ) : chunk = chunk . encode ( ( getattr ( fileobj , 'encoding' , '' ) or 'utf-8' ) ) linear_hash . update ( chunk ) chunks . append ( hashlib . sha256 ( chunk ) . digest ( ) ) chunk = fileobj . read ( chunk_size ) if ( not chunks ) : chunks = [ hashlib . sha256 ( '' ) . digest ( ) ] return ( linear_hash . hexdigest ( ) , bytes_to_hex ( tree_hash ( chunks ) ) )
compute the linear and tree hash from a fileobj .