首页 > 编程语言 >Python利用模糊哈希实现对比文件相似度详解

Python利用模糊哈希实现对比文件相似度详解

时间:2023-03-05 09:12:12浏览次数:51  
标签:file2 file1 Python print 详解 哈希 time test mmdt

导读 对比两个文件相似度,python中可通过difflib.SequenceMatcher/ssdeep/python_mmdt/tlsh实现,
在大量需要对比,且文件较大时,需要更高的效率,可以考虑模糊哈希,本文就来和大家详细聊聊

对比两个文件相似度,python中可通过difflib.SequenceMatcher/ssdeep/python_mmdt/tlsh实现,在大量需要对比,且文件较大时,需要更高的效率,可以考虑模糊哈希(fuzzy hash),如ssdeep/python_mmdt

测试过程发现:

  • difflib方法,读取文件后,可以实现匹配度输出
  • ssdeep/mmdt/tlsh方法可以实现,实现提前模糊哈希值,验证时,只读取一次,完成对比,从而优化对比时间,及内存/cpu消耗
  • tlsh测试时,值越小,相似度越高,在对比小文件时,很不理想
  • 在对比小文件时,三种方法相差不大,在对比大文件(案例中81MB),difflib方法慢的难以接受
  • 在实际环境中,建议使用mmdt方法,因为ssdeep在二进制对比中差别较大,失去参考价值,具体还有哪些文件类型存在此问题有待考量,

测试环境:

OS:ubuntu20.04

python:3.8.10

py-tlsh==4.7.2

python-mmdt==0.3.1

ssdeep==3.4

# -*- coding: utf-8 -*-
 
import ssdeep
import time
from python_mmdt.mmdt.mmdt import MMDT
from difflib import SequenceMatcher
 
def difflib_test(file1,file2):
    start_time = time.time()
    with open(file1,'rb') as f:
        s1 = f.read()
    with open(file2,'rb') as f:
        s2 = f.read()
    match_obj =  SequenceMatcher(None,s1,s2)
    print("difflib match:",match_obj.ratio())
    end_time = time.time()
    print('difflib_test cost :',end_time-start_time)
 
def mmdt_test(file1,file2):
    start_time = time.time()
    mmdt=MMDT()
    r1 = mmdt.mmdt_hash(file1)
    print(r1)
    r2 = mmdt.mmdt_hash_streaming(file2)
    print(r2)
    # sim1 = mmdt.mmdt_compare(file1, file2)
    # print("mmdt match:",sim1)
    sim2 = mmdt.mmdt_compare_hash(r1, r2)
    print("mmdt match:",sim2)
    end_time = time.time()
    print('mmdt_test cost :',end_time-start_time)
 
def ssdeep_test(file1,file2):
    start_time = time.time()
    sig1=ssdeep.hash_from_file(file1)
    sig2=ssdeep.hash_from_file(file2)
    print(sig1)
    print(sig2)
    print("ssdeep match:",ssdeep.compare(sig1,sig2))
    end_time = time.time()
    print('ssdeep_test cost :',end_time-start_time)
 
if __name__ == '__main__':
    start_time = time.time()
    file1='/root/test/fstab'
    file2='/root/test/fstab2'
    # file1 = '/root/test/initrd.img-5.4.0-125-generic'
    # file2 = '/root/test/initrd.img-5.4.0-135-generic'
    mmdt_test(file1,file2)    
    ssdeep_test(file1,file2)
    difflib_test(file1,file2)
    end_time = time.time()
    print('总执行时间:',end_time-start_time)

下面给出对比小文件/大文件效果:

Python利用模糊哈希实现对比文件相似度详解Python利用模糊哈希实现对比文件相似度详解

测试tlsh

import tlsh
import time
 
def tlsh_test(file1,file2):
    start_time = time.time()
    with open(file1,'rb') as f:
        s1 = tlsh.hash(f.read())
    with open(file2,'rb') as f:
        s2 = tlsh.hash(f.read())
    match_obj =  tlsh.diff(s1,s2)
    print("tlsh match:",match_obj)
    end_time = time.time()
    print('difflib_test cost :',end_time-start_time)
 
 
if __name__ == '__main__':
    start_time = time.time()
    # file1='/root/test/fstab'
    # file2='/root/test/fstab2'
    file1 = '/root/test/initrd.img-5.4.0-125-generic'
    file2 = '/root/test/initrd.img-5.4.0-135-generic'
    tlsh_test(file1,file2)
    end_time = time.time()
    print('总执行时间:',end_time-start_time)

对比小文件/大文件

Python利用模糊哈希实现对比文件相似度详解Python利用模糊哈希实现对比文件相似度详解

到此这篇关于Python利用模糊哈希实现对比文件相似度的文章就介绍到这了https://www.linuxprobe.com/

标签:file2,file1,Python,print,详解,哈希,time,test,mmdt
From: https://www.cnblogs.com/linuxprobe19/p/17155415.html

相关文章

  • 微信小程序结合php后台实现登录授权机制详解
    微信小程序应用的用户登录授权机制相当复杂,官方给出了下面一张流程图来解释:下面结合这张图来详细讲述下小程序的登录验证授权机制。首先,小程序应用实现登录验证的前提是......
  • 简单介绍Python中如何给字典设置默认值
    这篇文章主要介绍了Python中如何给字典设置默认值问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教Python字典设置默认值我们都知道,在Pyt......
  • python回顾
    重点!!!只记录了我感觉需要记得,还有大部分没有记录,希望可以去看网址-------⬇------网址python回顾and(&)or(|)#1.当用&与|用于数值计算时2&1将数值化为......
  • python版基于协同过滤算法的图书管理系统
    基于协同过滤算法的图书管理系统DEMOdemo传送门可以参考一、简介​ 本系统基于推荐算法给用户实现精准推荐图书。​ 根据用户对物品或者信息的偏好,发现物品或者内容......
  • Django --python manage
    Django--pythonmanage.pycollectstatic应用场景:线上部署(Linux)注意:执行时,需要与manag.py处于同一路径下作用:执行该命令时,将所有STATICFILES_DIRS中的所有文件夹......
  • python-httpx 发送http2.0时代请求
    原文,自己做个笔记用https://blog.51cto.com/u_15127674/3872190官方文档的地址https://www.python-httpx.org/我们在日常开发中,经常会发送各种各样的网络请求。Python......
  • 树哈希
    模板#include<cctype>#include<chrono>#include<cstdio>#include<random>#include<set>#include<vector>typedefunsignedlonglongull;constullmask=......
  • Activity的四种启动模式详解
    Activity的启动模式1.启动模式的类别标准模式(standard)栈顶复用模式(singleTop)栈内复用模式(singleTask)单例模式(singleInstance)2.任务栈任务栈Task,是一种用来放置......
  • python爬虫学习——xlwt库
    xlwt库主要是对excel进行操作,主要流程就是创建对象,创建工作表,写入数据,保存数据表。代码练习如下'''importxlwtworkbook=xlwt.Workbook(encoding="utf-8")#创建......
  • 详解Prometheus四种指标类型
    指标是用来衡量性能、消耗、效率和许多其他软件属性随时间的变化趋势。它们允许工程师通过警报和仪表盘来监控一系列测量值的演变(如CPU或内存使用量、请求持续时间、延迟......