Compare md5 hashes of two files in python

前端 未结 2 2039
一整个雨季
一整个雨季 2020-12-17 01:11

I want to compare hashes of two files. But no matter if files are different or not, even with different hashes comparison results True

Here is the code:

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-17 01:54

    This worked for me :

    import hashlib
    #File 1
    hasher1 = hashlib.md5()
    afile1 = open('C:/.../elephant_rgb.png', 'rb')
    buf1 = afile1.read()
    a = hasher1.update(buf1)
    md5_a=(str(hasher1.hexdigest()))
    #File 2
    hasher2 = hashlib.md5()
    afile2 = open('C:/.../innDay-Pic2.png', 'rb')
    buf2 = afile2.read()
    b = hasher2.update(buf2)
    md5_b=(str(hasher2.hexdigest()))
    #Compare md5
    if(md5_a==md5_b):
        print("Yes")
    else:
        print("No")
    
    ##No
    

提交回复
热议问题