Returns SHA256 checksum of a file

    Args:
        path (string): Absolute file path of file to hash

        block_size (int, optional): Number of bytes to read per block
767526
def hash_file(path, block_size=65536):
    
    sha256 = hashlib.sha256()
    with open(path, 'rb') as f:
        for block in iter(lambda: f.read(block_size), b''):
            sha256.update(block)
    return sha256.hexdigest()
211369
def get_hashes(path, exclude=None):
    '''
    
    '''
    out = {}
    for f in Path(path).rglob('*'):
        if f.is_dir():
            # We want to watch files, not directories.
            continue
        if exclude and re.match(exclude, f.as_posix()):
            retox_log.debug("excluding '{}'".format(f.as_posix()))
            continue
        pytime = f.stat().st_mtime
        out[f.as_posix()] = pytime
    return out
786326
def file_hash(path, hash_type='md5'):
    
    if os.path.exists(path):
        h = getattr(hashlib, hash_type)()
        with open(path, 'rb') as source:
            h.update(source.read())
        return h.hexdigest()
    else:
        return None
1269302
def write_and_get_hash(path, write_to_file, hash=None):
    
    hash = hash or hashlib.md5()
    try:
        # Access path
        r = config.DOWNLOAD_SESSION.get(path, stream=True)
        r.raise_for_status()
        for chunk in r:
            write_to_file.write(chunk)
            hash.update(chunk)

    except (MissingSchema, InvalidSchema):
        # If path is a local file path, try to open the file (generate hash if none provided)
        with open(path, 'rb') as fobj:
            for chunk in iter(lambda: fobj.read(2097152), b""):
                write_to_file.write(chunk)
                hash.update(chunk)

    assert write_to_file.tell() > 0, "File failed to write (corrupted)."

    return hash
822327
def _hash_file(self, path):
        

        m = hashlib.md5()
        with open(path, "rb") as f:
            while True:
                buf = f.read(128)
                if not buf:
                    break
                m.update(buf)
        return m.hexdigest()
350248
def block_header_index( cls, path, block_header ):
        
        with open( path, "rb" ) as f:
            chain_raw = f.read()

        for blk in xrange(0, len(chain_raw) / (BLOCK_HEADER_SIZE)):
            if chain_raw[blk * BLOCK_HEADER_SIZE : blk * BLOCK_HEADER_SIZE + BLOCK_HEADER_SIZE] == block_header:
                return blk

        return -1
525135
def read_checksum_digest(path, checksum_cls=hashlib.sha256):
  """"""
  checksum = checksum_cls()
  size = 0
  with tf.io.gfile.GFile(path, "rb") as f:
    while True:
      block = f.read(io.DEFAULT_BUFFER_SIZE)
      size += len(block)
      if not block:
        break
      checksum.update(block)
  return checksum.hexdigest(), size
572747
def read_sha1(
    file_path,
    buf_size = None,
    start_byte = 0,
    read_size = None,
    extra_hashers = [], # update(data) will be called on all of these
):
    '''
    
    '''
    read_size = read_size or os.stat(file_path).st_size
    buf_size = buf_size or DEFAULT_BUFFER_SIZE

    data_read = 0
    total_sha1 = hashlib.sha1()
    while data_read < read_size:
        with open( file_path, 'rb', buffering = 0 ) as f:
            f.seek( start_byte )
            data = f.read( min(buf_size, read_size - data_read) )
            assert( len(data) > 0 )
            total_sha1.update( data )
            for hasher in extra_hashers:
                hasher.update( data )
            data_read += len(data)
            start_byte += len(data)
    assert( data_read == read_size )

    return total_sha1
1042778
def md5sum(file_path, blocksize=65536):
    """"""
    md5 = hashlib.md5()
    with open(file_path, "rb") as f:
        for block in iter(lambda: f.read(blocksize), ""):
            md5.update(block)
    return md5.hexdigest()
347165
def _get_sizes_checksums(checksums_path):
  """"""
  checksums = {}
  for line in _read_file(checksums_path).split('\n'):
    if not line:
      continue
    # URL might have spaces inside, but size and checksum will not.
    url, size, checksum = line.rsplit(' ', 2)
    checksums[url] = (int(size), checksum)
  return checksums
252134
def _filehash(filepath, blocksize=4096):
    
    sha = hashlib.sha256()
    with open(filepath, 'rb') as fp:
        while 1:
            data = fp.read(blocksize)
            if data:
                sha.update(data)
            else:
                break
    return sha
900937
def _get_hashed_path(self, path):
        """"""
        return self._get_path('%s.pkl' % hashlib.md5(path.encode("utf-8")).hexdigest())
546653
def get_image_file_hash(image_path):
    '''
    '''
    hasher = hashlib.md5()
    with open(image_path, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hasher.update(chunk)
    return hasher.hexdigest()
1338350
def checksum_file(scheme, path):
    """"""
    h = getattr(hashlib, scheme)()

    with open(path, 'rb') as f:
        chunk = f.read(65535)
        while chunk:
            h.update(chunk)
            chunk = f.read(65535)
    return h.hexdigest()
152509
def file_blob_id(path)
    file_path = Rails.root.join('test/fixtures'.freeze).join(path)
    blob_contents = File.binread file_path

    # This needs to be kept in sync with blob_model.rb.
    Base64.urlsafe_encode64(Digest::SHA256.digest(blob_contents)).inspect
  end
120754
def _get_file_size(file_path):
    

    size = getsize(file_path)

    file_size = bytearray(4)
    pack_into(b"I", file_size, 0, size)

    return file_size
669432
def new_checksum(self):
        """"""
        if self.context.is_file(self.filename):
            friendly_name = format_path(self.filename)
            logger.debug("Calculating SHA1 of %s ..", friendly_name)
            context = hashlib.sha1()
            context.update(self.context.read_file(self.filename))
            checksum = context.hexdigest()
            logger.debug("The SHA1 digest of %s is %s.", friendly_name, checksum)
            return checksum