Last active
June 14, 2017 13:37
-
-
Save albertofwb/5dbe091b3a263929183f0ba080cb5fab to your computer and use it in GitHub Desktop.
为什么应该放弃或减少使用MD5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MD5 在现实中已经被发现有相当多的数据都可能导致冲突。举例而言,如下两段数据的MD5哈希值就是完全一样的 | |
# ref: blog.jianguoyun.com/?p=481 | |
# date: 2012-09-20 | |
import os | |
from hashlib import md5 | |
def save_strs2bin(text, file_name): | |
if not isinstance(text, str): | |
return False | |
text_list = text.strip().split() | |
with open(file_name, 'wb') as fp: | |
for i in text_list: | |
pure_num = int(i, 16) | |
fp.write((pure_num).to_bytes(1, 'little', signed=False)) | |
return True | |
def md5_file(file_path): | |
if not os.path.exists(file_path): | |
print("{} not exists".format(file_path)) | |
return False | |
m = md5() | |
m.update(open(file_path, 'rb').read()) | |
return m.hexdigest() | |
def test(): | |
data = [ ( | |
'''4d c9 68 ff 0e e3 5c 20 95 72 d4 77 7b 72 15 87 | |
d3 6f a7 b2 1b dc 56 b7 4a 3d c0 78 3e 7b 95 18 | |
af bf a2 00 a8 28 4b f3 6e 8e 4b 55 b3 5f 42 75 | |
93 d8 49 67 6d a0 d1 55 5d 83 60 fb 5f 07 fe a2''', 'test1'), | |
#^ data differ | |
( | |
''' 4d c9 68 ff 0e e3 5c 20 95 72 d4 77 7b 72 15 87 | |
d3 6f a7 b2 1b dc 56 b7 4a 3d c0 78 3e 7b 95 18 | |
af bf a2 02 a8 28 4b f3 6e 8e 4b 55 b3 5f 42 75 | |
93 d8 49 67 6d a0 d1 d5 5d 83 60 fb 5f 07 fe a2''', 'test2')] | |
#^ data differ | |
for text, file_name in data: | |
save_strs2bin(text, file_name) | |
print("writing to {} ==> {}".format(file_name, md5_file(file_name))) | |
# output | |
# writing to test1 ==> 008ee33a9d58b51cfeb425b0959121c9 | |
# writing to test2 ==> 008ee33a9d58b51cfeb425b0959121c9 | |
if __name__ == '__main__': | |
test() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment