Generating an Md5 Checksum of a File

How do I calculate the MD5 checksum of a file in Python?

In regards to your error and what's missing in your code. m is a name which is not defined for getmd5() function.

No offence, I know you are a beginner, but your code is all over the place. Let's look at your issues one by one :)

First, you are not using hashlib.md5.hexdigest() method correctly. Please refer explanation on hashlib functions in Python Doc Library. The correct way to return MD5 for provided string is to do something like this:

>>> import hashlib
>>> hashlib.md5("example string").hexdigest()
'2a53375ff139d9837e93a38a279d63e5'

However, you have a bigger problem here. You are calculating MD5 on a file name string, where in reality MD5 is calculated based on file contents. You will need to basically read file contents and pipe it though MD5. My next example is not very efficient, but something like this:

>>> import hashlib
>>> hashlib.md5(open('filename.exe','rb').read()).hexdigest()
'd41d8cd98f00b204e9800998ecf8427e'

As you can clearly see second MD5 hash is totally different from the first one. The reason for that is that we are pushing contents of the file through, not just file name.

A simple solution could be something like that:

# Import hashlib library (md5 method is part of it)
import hashlib

# File to check
file_name = 'filename.exe'

# Correct original md5 goes here
original_md5 = '5d41402abc4b2a76b9719d911017c592'

# Open,close, read file and calculate MD5 on its contents
with open(file_name, 'rb') as file_to_check:
# read contents of the file
data = file_to_check.read()
# pipe contents of the file through
md5_returned = hashlib.md5(data).hexdigest()

# Finally compare original MD5 with freshly calculated
if original_md5 == md5_returned:
print "MD5 verified."
else:
print "MD5 verification failed!."

Please look at the post Python: Generating a MD5 checksum of a file. It explains in detail a couple of ways how it can be achieved efficiently.

Best of luck.

Generating an MD5 checksum of a file

You can use hashlib.md5()

Note that sometimes you won't be able to fit the whole file in memory. In that case, you'll have to read chunks of 4096 bytes sequentially and feed them to the md5 method:

import hashlib
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()

Note: hash_md5.hexdigest() will return the hex string representation for the digest, if you just need the packed bytes use return hash_md5.digest(), so you don't have to convert back.

Bat file to only generate a MD5 checksum for a file

Using the FCIV tool you have shown in your question, you seem to be outputting your result in base64 in an XML, when that isn't what you've asked.

You may be able to still use it like this to get the MD5 of a file called new.xml:

For /F "EOL=/" %A In ('FCIV new.xml') Do @Echo(%A

This assumes MD5 is still the default output of FCIV and it is ran from a cmd.exe window which correctly knows/accesses the location of FCIV.exe

[Edit /]

An extended example in batch file format.

@Echo Off
Set "MyFile=new.xml"
Set "MD5="
For /F "EOL=/" %%A In ('FCIV "%MyFile%"') Do Set "MD5=%%A"
If Defined MD5 Echo(%MD5%
Timeout -1

The word new.xml could even be replaced with %~1 and you could then just drag and drop a file onto it to output it's hash.

How to generate an MD5 checksum for a file in Android?

Convert the file content into string & use the below method:

public static String getMD5EncryptedString(String encTarget){
MessageDigest mdEnc = null;
try {
mdEnc = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
System.out.println("Exception while encrypting to md5");
e.printStackTrace();
} // Encryption algorithm
mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
while ( md5.length() < 32 ) {
md5 = "0"+md5;
}
return md5;
}

Note that this simple approach is suitable for smallish strings, but will not be efficient for large files. For the latter, see dentex's answer.

generating md5 checksum recursively and printing it in a text file with md5 , path and file size

This executes md5sum together with wc -c, which gets the number of bytes, saves the results into variables and concatenates it in a single echo print for every file:

find your/path -type f -exec bash -c 'md=$(md5sum "$0"); siz=$(wc -c <"$0"); echo ${md} ${siz}b' {} \;


Related Topics



Leave a reply



Submit