|
| 1 | +""" |
| 2 | +Dropbox "content_hash" hashers, from |
| 3 | +https://github.com/dropbox/dropbox-api-content-hasher (adapted for Python 3). |
| 4 | +See https://www.dropbox.com/developers/reference/content-hash. |
| 5 | +""" |
| 6 | + |
| 7 | +import hashlib |
| 8 | + |
| 9 | + |
| 10 | +class DropboxContentHasher(object): |
| 11 | + """ |
| 12 | + Computes a hash using the same algorithm that the Dropbox API uses for the |
| 13 | + the "content_hash" metadata field. |
| 14 | +
|
| 15 | + The digest() method returns a raw binary representation of the hash. The |
| 16 | + hexdigest() convenience method returns a hexadecimal-encoded version, which |
| 17 | + is what the "content_hash" metadata field uses. |
| 18 | +
|
| 19 | + This class has the same interface as the hashers in the standard 'hashlib' |
| 20 | + package. |
| 21 | +
|
| 22 | + Example: |
| 23 | +
|
| 24 | + hasher = DropboxContentHasher() |
| 25 | + with open('some-file', 'rb') as f: |
| 26 | + while True: |
| 27 | + chunk = f.read(1024) # or whatever chunk size you want |
| 28 | + if len(chunk) == 0: |
| 29 | + break |
| 30 | + hasher.update(chunk) |
| 31 | + print(hasher.hexdigest()) |
| 32 | + """ |
| 33 | + |
| 34 | + BLOCK_SIZE = 4 * 1024 * 1024 |
| 35 | + |
| 36 | + def __init__(self): |
| 37 | + self._overall_hasher = hashlib.sha256() |
| 38 | + self._block_hasher = hashlib.sha256() |
| 39 | + self._block_pos = 0 |
| 40 | + |
| 41 | + self.digest_size = self._overall_hasher.digest_size |
| 42 | + # hashlib classes also define 'block_size', but I don't know how people use that value |
| 43 | + |
| 44 | + def update(self, new_data): |
| 45 | + if self._overall_hasher is None: |
| 46 | + raise AssertionError( |
| 47 | + "can't use this object anymore; you already called digest()") |
| 48 | + |
| 49 | + assert isinstance(new_data, bytes), ( |
| 50 | + "Expecting a byte string, got {!r}".format(new_data)) |
| 51 | + |
| 52 | + new_data_pos = 0 |
| 53 | + while new_data_pos < len(new_data): |
| 54 | + if self._block_pos == self.BLOCK_SIZE: |
| 55 | + self._overall_hasher.update(self._block_hasher.digest()) |
| 56 | + self._block_hasher = hashlib.sha256() |
| 57 | + self._block_pos = 0 |
| 58 | + |
| 59 | + space_in_block = self.BLOCK_SIZE - self._block_pos |
| 60 | + part = new_data[new_data_pos:(new_data_pos + space_in_block)] |
| 61 | + self._block_hasher.update(part) |
| 62 | + |
| 63 | + self._block_pos += len(part) |
| 64 | + new_data_pos += len(part) |
| 65 | + |
| 66 | + def _finish(self): |
| 67 | + if self._overall_hasher is None: |
| 68 | + raise AssertionError( |
| 69 | + "can't use this object anymore; you already called digest() or hexdigest()") |
| 70 | + |
| 71 | + if self._block_pos > 0: |
| 72 | + self._overall_hasher.update(self._block_hasher.digest()) |
| 73 | + self._block_hasher = None |
| 74 | + h = self._overall_hasher |
| 75 | + self._overall_hasher = None # Make sure we can't use this object anymore. |
| 76 | + return h |
| 77 | + |
| 78 | + def digest(self): |
| 79 | + return self._finish().digest() |
| 80 | + |
| 81 | + def hexdigest(self): |
| 82 | + return self._finish().hexdigest() |
| 83 | + |
| 84 | + def copy(self): |
| 85 | + c = DropboxContentHasher.__new__(DropboxContentHasher) |
| 86 | + c._overall_hasher = self._overall_hasher.copy() |
| 87 | + c._block_hasher = self._block_hasher.copy() |
| 88 | + c._block_pos = self._block_pos |
| 89 | + c.digest_size = self.digest_size |
| 90 | + return c |
| 91 | + |
| 92 | + |
| 93 | +class StreamHasher(object): |
| 94 | + """ |
| 95 | + A wrapper around a file-like object (either for reading or writing) |
| 96 | + that hashes everything that passes through it. Can be used with |
| 97 | + DropboxContentHasher or any 'hashlib' hasher. |
| 98 | +
|
| 99 | + Example: |
| 100 | +
|
| 101 | + hasher = DropboxContentHasher() |
| 102 | + with open('some-file', 'rb') as f: |
| 103 | + wrapped_f = StreamHasher(f, hasher) |
| 104 | + response = some_api_client.upload(wrapped_f) |
| 105 | +
|
| 106 | + locally_computed = hasher.hexdigest() |
| 107 | + assert response.content_hash == locally_computed |
| 108 | + """ |
| 109 | + |
| 110 | + def __init__(self, f, hasher): |
| 111 | + self._f = f |
| 112 | + self._hasher = hasher |
| 113 | + |
| 114 | + def close(self): |
| 115 | + return self._f.close() |
| 116 | + |
| 117 | + def flush(self): |
| 118 | + return self._f.flush() |
| 119 | + |
| 120 | + def fileno(self): |
| 121 | + return self._f.fileno() |
| 122 | + |
| 123 | + def tell(self): |
| 124 | + return self._f.tell() |
| 125 | + |
| 126 | + def read(self, *args): |
| 127 | + b = self._f.read(*args) |
| 128 | + self._hasher.update(b) |
| 129 | + return b |
| 130 | + |
| 131 | + def write(self, b): |
| 132 | + self._hasher.update(b) |
| 133 | + return self._f.write(b) |
| 134 | + |
| 135 | + def __iter__(self): |
| 136 | + return self |
| 137 | + |
| 138 | + def __next__(self): |
| 139 | + b = next(self._f) |
| 140 | + self._hasher.update(b) |
| 141 | + return b |
| 142 | + |
| 143 | + def next(self): |
| 144 | + return self.__next__() |
| 145 | + |
| 146 | + def readline(self, *args): |
| 147 | + b = self._f.readline(*args) |
| 148 | + self._hasher.update(b) |
| 149 | + return b |
| 150 | + |
| 151 | + def readlines(self, *args): |
| 152 | + bs = self._f.readlines(*args) |
| 153 | + for b in bs: |
| 154 | + self._hasher.update(b) |
| 155 | + return bs |
| 156 | + |
| 157 | + |
| 158 | +def content_hash(content): |
| 159 | + """Return the Dropbox content hash of a ``bytes`` payload as a hex string.""" |
| 160 | + hasher = DropboxContentHasher() |
| 161 | + hasher.update(content) |
| 162 | + return hasher.hexdigest() |
0 commit comments