43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import resumablehashlib
|
|
|
|
|
|
class PieceHasher(object):
|
|
def __init__(self, piece_size, starting_offset, starting_piece_hash_str, hash_fragment_to_resume):
|
|
if not isinstance(starting_offset, (int, long)):
|
|
raise TypeError('starting_offset must be an integer')
|
|
elif not isinstance(piece_size, (int, long)):
|
|
raise TypeError('piece_size must be an integer')
|
|
|
|
self._current_offset = starting_offset
|
|
self._piece_size = piece_size
|
|
self._hash_fragment = hash_fragment_to_resume
|
|
self._piece_hashes = [starting_piece_hash_str]
|
|
|
|
def update(self, buf):
|
|
buf_offset = 0
|
|
while buf_offset < len(buf):
|
|
buf_bytes_to_hash = buf[0:self._piece_length_remaining()]
|
|
to_hash_len = len(buf_bytes_to_hash)
|
|
|
|
if self._piece_offset() == 0 and to_hash_len > 0 and self._current_offset > 0:
|
|
# We are opening a new piece
|
|
self._piece_hashes.append(self._hash_fragment.hexdigest())
|
|
self._hash_fragment = resumablehashlib.sha1()
|
|
|
|
self._hash_fragment.update(buf_bytes_to_hash)
|
|
self._current_offset += to_hash_len
|
|
buf_offset += to_hash_len
|
|
|
|
def _piece_length_remaining(self):
|
|
return self._piece_size - (self._current_offset % self._piece_size)
|
|
|
|
def _piece_offset(self):
|
|
return self._current_offset % self._piece_size
|
|
|
|
@property
|
|
def piece_hashes(self):
|
|
return ''.join(self._piece_hashes)
|
|
|
|
@property
|
|
def hash_fragment(self):
|
|
return self._hash_fragment
|