我在两个不同的位置比较两个qcow2图像文件以查看差异。 /opt/images/file.qcow2 /mnt/images/file.qcow2
当我跑步时
md5sum /opt/images/file.qcow2
md5sum /mnt/images/file.qcow2
两个文件的校验和相同
但是当尝试使用以下代码找到md5sum时
def isImageLatest(file1,file2):
print('Checking md5sum of {} {}'.format(file1, file2))
if os.path.isfile(file1) and os.path.isfile(file2):
md5File1 = hashlib.md5(file1).hexdigest()
md5File2 = hashlib.md5(file2).hexdigest()
print('md5sum of {} is {}'.format(file1, md5File1))
print('md5sum of {} is {}'.format(file2, md5File2))
else:
print('Either {} or {} File not found'.format(file1,file2))
return False
if md5File1 == md5File2:
return True
else:
return False
它表示校验和不一样
更新 文件大小可以是8 GB
答案 0 :(得分:4)
您正在散列文件的路径,而不是内容......
eventID ... tag_id tag
--------------------
1124 2,3 Hiphop, Dance
hashlib.md5(file1).hexdigest() # file1 = '/path/to/file.ext'
Sidenote :您可能希望使用def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def isImageLatest(file1,file2):
print('Checking md5sum of {} {}'.format(file1, file2))
if os.path.isfile(file1) and os.path.isfile(file2):
md5File1 = md5(file1)
md5File2 = md5(file2)
print('md5sum of {} is {}'.format(file1, md5File1))
print('md5sum of {} is {}'.format(file2, md5File2))
else:
print('Either {} or {} File not found'.format(file1,file2))
return False
if md5File1 == md5File2:
return True
else:
return False
(使用unix' s hashlib.sha1()
)而不是sha1sum
,这已被破坏和弃用...
修改:使用各种缓冲区和md5
vs md5
进行基准测试
在糟糕的服务器上使用sha1
随机文件(Atom N2800 @ 1.86GHz):
100mB
因此┏━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Algorithm ┃ Buffer ┃ Time (s) ┃
┡━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ md5sum │ --- │ 0.387 │
│ MD5 │ 2⁶ │ 21.5670549870 │
│ MD5 │ 2⁸ │ 6.64844799042 │
│ MD5 │ 2¹⁰ │ 3.12886619568 │
│ MD5 │ 2¹² │ 1.82865810394 │
│ MD5 │ 2¹⁴ │ 1.27349495888 │
│ MD5 │ 128¹ │ 11.5235209465 │
│ MD5 │ 128² │ 1.27280807495 │
│ MD5 │ 128³ │ 1.16839885712 │
│ sha1sum │ --- │ 1.013 │
│ SHA1 │ 2⁶ │ 23.4520659447 │
│ SHA1 │ 2⁸ │ 7.75686216354 │
│ SHA1 │ 2¹⁰ │ 3.82775402069 │
│ SHA1 │ 2¹² │ 2.52755594254 │
│ SHA1 │ 2¹⁴ │ 1.93437695503 │
│ SHA1 │ 128¹ │ 12.9430441856 │
│ SHA1 │ 128² │ 1.93382811546 │
│ SHA1 │ 128³ │ 1.81412386894 │
└───────────┴─────────┴───────────────┘
比md5sum
快,而python的实现显示相同。拥有更大的缓冲区可以提高性能但是在一定限度内(sha1sum
似乎是一个很好的权衡(不是太大和有效))。
答案 1 :(得分:1)
试试这个:
spnSignupCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
btnSubmit.setVisibility(GONE);
}else{
btnSubmit.setVisibility(VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
当您执行SELECT
a1.AccountID,
a1.Forename,
a1.Surname,
img1.Filename AS ProfileImg,
img2.Filename AS CoverImg
FROM
dbo.Account a1
INNER JOIN
dbo.AccountImage img1 ON
img1.AccountID = a1.AccountID AND ProfileImg = 1
INNER JOIN
dbo.AccountImage img2 ON
img2.AccountID = a1.AccountID and CoverImg = 2
时,您实际上只是md5的文件名。您实际上想要md5内容,这需要使用Python文件操作打开和读取文件。我上面发布的方法可以散列大文件而无需将整个内容读入内存。
答案 2 :(得分:1)
如何使用以下代码:
def isImageLatest(file1,file2):
print('Checking md5sum of {} {}'.format(file1, file2))
if os.path.isfile(file1) and os.path.isfile(file2):
md5File1 = hashlib.md5(open(file1,"rb").read()).hexdigest()
md5File2 = hashlib.md5(open(file2,"rb").read()).hexdigest()
print('md5sum of {} is {}'.format(file1, md5File1))
print('md5sum of {} is {}'.format(file2, md5File2))
else:
print('Either {} or {} File not found'.format(file1,file2))
return False
if md5File1 == md5File2:
return True
else:
return False
请注意,这适用于较小的文件。如果文件很大,最好像上面给出的例子一样逐块地读取文件。对于这种情况,您可以使用以下代码:
import time
import hashlib
import time
with open("Some_Very_Large_File", "rb") as f:
hasher = hashlib.md5()
a = time.time()
while True:
data = f.read(3 * 1024)
if not data:
break
hasher.update(data)
print hasher.hexdigest()
b = time.time()
print "Done hashing in ", b - a, " seconds"
以下是我观察到的基准:
3.26GB media file and calculated the hash in 11.26 sec.
4.8GB file and hash calculated in 16.47 sec.
10.8GB file and hash calculated in 102.36 sec.
请尝试使用该代码并告诉我。