forked from ldx/DBdownload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropbox_content_hasher.py
145 lines (110 loc) · 4.24 KB
/
dropbox_content_hasher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from __future__ import absolute_import, division, print_function, unicode_literals
import hashlib
import six
class DropboxContentHasher(object):
"""
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 = 4 * 1024 * 1024
def __init__(self):
self._overall_hasher = hashlib.sha256()
self._block_hasher = hashlib.sha256()
self._block_pos = 0
self.digest_size = self._overall_hasher.digest_size
# hashlib classes also define 'block_size', but I don't know how people use that value
def update(self, new_data):
if self._overall_hasher is None:
raise AssertionError(
"can't use this object anymore; you already called digest()")
assert isinstance(new_data, six.binary_type), (
"Expecting a byte string, got {!r}".format(new_data))
new_data_pos = 0
while new_data_pos < len(new_data):
if self._block_pos == self.BLOCK_SIZE:
self._overall_hasher.update(self._block_hasher.digest())
self._block_hasher = hashlib.sha256()
self._block_pos = 0
space_in_block = self.BLOCK_SIZE - self._block_pos
part = new_data[new_data_pos:(new_data_pos+space_in_block)]
self._block_hasher.update(part)
self._block_pos += len(part)
new_data_pos += len(part)
def _finish(self):
if self._overall_hasher is None:
raise AssertionError(
"can't use this object anymore; you already called digest() or hexdigest()")
if self._block_pos > 0:
self._overall_hasher.update(self._block_hasher.digest())
self._block_hasher = None
h = self._overall_hasher
self._overall_hasher = None # Make sure we can't use this object anymore.
return h
def digest(self):
return self._finish().digest()
def hexdigest(self):
return self._finish().hexdigest()
def copy(self):
c = DropboxContentHasher.__new__(DropboxContentHasher)
c._overall_hasher = self._overall_hasher.copy()
c._block_hasher = self._block_hasher.copy()
c._block_pos = self._block_pos
return c
class StreamHasher(object):
"""
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
"""
def __init__(self, f, hasher):
self._f = f
self._hasher = hasher
def close(self):
return self._f.close()
def flush(self):
return self._f.flush()
def fileno(self):
return self._f.fileno()
def tell(self):
return self._f.tell()
def read(self, *args):
b = self._f.read(*args)
self._hasher.update(b)
return b
def write(self, b):
self._hasher.update(b)
return self._f.write(b)
def next(self):
b = self._f.next()
self._hasher.update(b)
return b
def readline(self, *args):
b = self._f.readline(*args)
self._hasher.update(b)
return b
def readlines(self, *args):
bs = self._f.readlines(*args)
for b in bs:
self._hasher.update(b)
return b