maestral.utils.content_hasher

Module for content hashing.

Module Contents

Classes

DropboxContentHasher

Computes a hash using the same algorithm that the Dropbox API uses for the

StreamHasher

A wrapper around a file-like object (either for reading or writing)

class maestral.utils.content_hasher.DropboxContentHasher[source]

Computes a hash using the same algorithm that the Dropbox API uses for the the “content_hash” metadata field. The digest() method returns a raw binary representation of the hash. The hexdigest() convenience method returns a hexadecimal-encoded version, which is what the “content_hash” metadata field uses. This class has the same interface as the hashers in the standard ‘hashlib’ package.

Example

hasher = DropboxContentHasher() with open(‘some-file’, ‘rb’) as f:

while True:

chunk = f.read(1024) # or whatever chunk size you want if len(chunk) == 0:

break

hasher.update(chunk)

print(hasher.hexdigest())

BLOCK_SIZE[source]
update(self, new_data)[source]
digest(self)[source]
hexdigest(self)[source]
copy(self)[source]
class maestral.utils.content_hasher.StreamHasher(f, hasher)[source]

A wrapper around a file-like object (either for reading or writing) that hashes everything that passes through it. Can be used with DropboxContentHasher or any ‘hashlib’ hasher. .. rubric:: Example

hasher = DropboxContentHasher() with open(‘some-file’, ‘rb’) as f:

wrapped_f = StreamHasher(f, hasher) response = some_api_client.upload(wrapped_f)

locally_computed = hasher.hexdigest() assert response.content_hash == locally_computed

close(self)[source]
flush(self)[source]
fileno(self)[source]
tell(self)[source]
read(self, *args)[source]
write(self, b)[source]
next(self)[source]
readline(self, *args)[source]
readlines(self, *args)[source]