Skip to content
Snippets Groups Projects
Commit fc72b560 authored by Renzo Frigato's avatar Renzo Frigato
Browse files

replace sha1 with sha384

skip md5 when it is not sended
parent 41751bb4
No related branches found
No related tags found
No related merge requests found
......@@ -54,18 +54,18 @@ class FileRequest(object):
def _save_temp_file(self, folder):
filepath = os.path.join(folder, self.filename)
md5 = hashlib.md5()
sha1 = hashlib.sha1()
sha384 = hashlib.sha384()
filesize = 0
start_time = datetime.datetime.utcnow()
with open(filepath, 'wb') as fd:
for chunk in iter(lambda: self.body.read(2**20), ''):
md5.update(chunk)
sha1.update(chunk)
sha384.update(chunk)
filesize += len(chunk)
fd.write(chunk)
self.filesize = filesize
self.md5 = md5.hexdigest()
self.sha1 = sha1.hexdigest()
self.sha384 = sha384.hexdigest()
duration = datetime.datetime.utcnow() - start_time
success = (self.md5 == self.received_md5) if self.received_md5 is not None else True
return success, duration
......
......@@ -376,7 +376,7 @@ class FileListHandler(ListHandler):
file_properties = {
'name': file_request.filename,
'size': file_request.filesize,
'hash': file_request.sha1,
'hash': file_request.sha384,
'type': file_request.mimetype,
'tags': file_request.tags,
'metadata': file_request.metadata,
......
......@@ -267,18 +267,23 @@ def download_ticket(ip, type_, target, filename, size):
def receive_stream_and_validate(stream, filepath, received_md5):
md5 = hashlib.md5()
sha1 = hashlib.sha1()
skip_md5 = False
if received_md5 is not None:
md5 = hashlib.md5()
else:
skip_md5 = True
sha384 = hashlib.sha384()
filesize = 0
start_time = datetime.datetime.utcnow()
with open(filepath, 'wb') as fd:
for chunk in iter(lambda: stream.read(2**20), ''):
md5.update(chunk)
sha1.update(chunk)
if received_md5 is not None:
md5.update(chunk)
sha384.update(chunk)
filesize += len(chunk)
fd.write(chunk)
duration = datetime.datetime.utcnow() - start_time
return (md5.hexdigest() == received_md5) if received_md5 is not None else True, sha1.hexdigest(), filesize, duration
return skip_md5 or (md5.hexdigest() == received_md5), sha384.hexdigest(), filesize, duration
def guess_mimetype(filepath):
......
......@@ -78,7 +78,7 @@ def sort(args):
log.info('found %d files to sort (ignoring symlinks and dotfiles)' % file_cnt)
for i, filepath in enumerate(files):
log.info('sorting %s [%s] (%d/%d)' % (os.path.basename(filepath), util.hrsize(os.path.getsize(filepath)), i+1, file_cnt))
hash_ = hashlib.sha1()
hash_ = hashlib.sha384()
if not args.quick:
with open(filepath, 'rb') as fd:
for chunk in iter(lambda: fd.read(2**20), ''):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment