计算文件MD5值的小脚本


有时为了方便计算文件的MD5值,需要一个由 http://keir.net 写的小工具Hash.exe,不过有些时候还是希望写个脚本实现:

#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import hashlib

def md5(fileName):
    """Compute md5 hash of the specified file"""
    m = hashlib.md5()
    try:
        fd = open(fileName,"rb")
    except IOError:
        print "Reading file has problem: ", filename
        return
    x = fd.read()
    fd.close()
    m.update(x)
    return m.hexdigest()

if __name__ == "__main__":
    for eachFile in sys.argv[1:]:
        print "%s %s" % (md5(eachFile), eachFile)

将脚本保存为calcMD5.py,然后再终端下运行:

D:> python calcMD5.py file1 [file2]…

即可。

,

《 “计算文件MD5值的小脚本” 》 有 3 条评论

  1. 有没有一个字符串的哈希值等于它自己?
    Is there a string which is the MD5 hash of itself?
    How to generate MD5 Hash value that Hashes to itself?
    Is there a string that’s hash is equal to itself?

    答案是理论上可能存在,但是需要的计算量和计算时间太长,所以暂时还没有找到。

    Is there a string which is the MD5 hash of itself?
    https://www.reddit.com/r/NoStupidQuestions/comments/b2mmr3/is_there_a_string_which_is_the_md5_hash_of_itself/
    `
    MD5 isn’t so broken that we could easily figure out what that string is, if it exists, though. MD5 is really only mildly broken, despite many people acting like it’s completely useless.
    MD5并没有那么糟糕,糟糕到让我们可以很容易地找出这个字符串(字符串的哈希和它自己相同)是什么,如果它存在的话。MD5实际上只是轻微的损坏,尽管许多人认为它完全没用。

    For a random hash algorithm their is a ~2/3 chance it contains at-least one self-referential. No known string exists, but nor has it been shown to be impossible. An exhaustive search is beyond the power of mankind’s computational ability (their are 2128 possible combinations, at 1 quantrillion hashes a second that’s dozens of times the age of the universe.)
    `
    Is there a string that’s hash is equal to itself?
    https://crypto.stackexchange.com/questions/19493/is-there-a-string-thats-hash-is-equal-to-itself

    MD5: Existence of invariant (fixed point)
    https://crypto.stackexchange.com/questions/68674/md5-existence-of-invariant-fixed-point

    Is there an MD5 Fixed Point where md5(x) == x?
    https://stackoverflow.com/questions/235785/is-there-an-md5-fixed-point-where-md5x-x

    MD5 Games 1, Web, 50pts
    https://github.com/bl4de/ctf/blob/master/2017/HackDatKiwi_CTF_2017/md5games1/md5games1.md

    Let’s find nice patterns of MD5!!
    https://github.com/zvibazak/Nice-MD5s

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注