maestral.utils.hashing

Module for content hashing file contents.

Module Contents

class maestral.utils.hashing.DropboxContentHasher[source]

Computes a hash using the same algorithm that the Dropbox API uses for 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:

Read a file in chunks of 1024 bytes and compute its content hash:

>>> hasher = DropboxContentHasher()
>>> with open('some-file', 'rb') as f:
...     while True:
...         chunk = f.read(1024)
...         if len(chunk) == 0:
...             break
...         hasher.update(chunk)
... print(hasher.hexdigest())
BLOCK_SIZE[source]
update(new_data)[source]
Parameters:

new_data (bytes | bytearray)

Return type:

None

digest()[source]
Return type:

bytes

hexdigest()[source]
Return type:

str

copy()[source]
Return type:

DropboxContentHasher

class maestral.utils.hashing.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.

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
Parameters:
  • f (BinaryIO) – File-like object.

  • hasher (DropboxContentHasher) – Hasher to use. Must implement an update method.

close()[source]
Return type:

None

flush()[source]
Return type:

None

fileno()[source]
Return type:

int

tell()[source]
Return type:

int

read(size=-1)[source]
Parameters:

size (int)

Return type:

bytes

write(b)[source]
Parameters:

b (_WritableBuffer)

Return type:

int

readline(size=-1)[source]
Parameters:

size (int)

Return type:

bytes

readlines(hint=-1)[source]
Parameters:

hint (int)

Return type:

list[bytes]