def _generate_file_metadata(self, data): import pydeep import magic from hashlib import md5, sha1, sha256 try: self.filetype = magic.from_buffer(data) except: self.filetype = "Unavailable" try: mimetype = magic.from_buffer(data, mime=True) if mimetype: self.mimetype = mimetype.split(";")[0] if not mimetype: self.mimetype = "unknown" except: self.mimetype = "Unavailable" self.size = len(data) # this is a shard key. you can't modify it once it's set. # MongoEngine will still mark the field as modified even if you set it # to the same value. if not self.md5: self.md5 = md5(data).hexdigest() self.sha1 = sha1(data).hexdigest() self.sha256 = sha256(data).hexdigest() try: self.ssdeep = pydeep.hash_bytes(data) except: self.ssdeep = None
def HASH(): return { "md5": hashlib.md5(filebytes).hexdigest(), "sha1": hashlib.sha1(filebytes).hexdigest(), "sha256": hashlib.sha256(filebytes).hexdigest(), "sha512": hashlib.sha512(filebytes).hexdigest(), "ssdeep": pydeep.hash_bytes(filebytes) }