# -*- coding: utf-8 -*- import hashlib import uuid from django.core.files.uploadedfile import InMemoryUploadedFile def sha1_checksum(handler): if isinstance(handler, InMemoryUploadedFile): temp_name = '/tmp/_{}'.format(uuid.uuid4()) f = open(temp_name, 'wb+') f.write(handler.read()) f.seek(0) else: f = open(handler.name, 'rb') checksum = hashlib.sha1(f.read()) return checksum.hexdigest()