制造 MD5 碰撞
下面这两张图片拥有相同的 MD5 值 253dd04e87492e4fc3471de5e776bc3d
:
使用 Python 计算文件 MD5 值的脚本:1
2
3
4
5
6
7
8
9
10
11
12
13
14#!/usr/bin/env python3
import hashlib
from functools import partial
def md5sum(filename):
with open(filename, mode = "rb") as f:
myhash = hashlib.md5()
for buf in iter(partial(f.read, 128), b""):
myhash.update(buf)
return myhash.hexdigest()
filepath = input("请输入文件路径:")
print(md5sum(filepath))
还有 Python 2 的版本:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#!/usr/bin/env python
#-*- coding:utf-8 -*-
import hashlib
import os
def md5sum(filename):
if not os.path.isfile(filename):
return
myhash = hashlib.md5()
f = file(filename, "rb")
while True:
b = f.read(8192)
if not b:
break
myhash.update(b)
f.close()
return myhash.hexdigest()
filepath = raw_input("请输入文件路径:")
print md5sum(filepath)
拓展阅读:
至今有没有发现哪几个字符串的 MD5 相同?- 知乎
Create your own MD5 collisions
hashlib — Secure hashes and message digests — Python 3.8.3 documentation