मैं पायथन में किसी फ़ाइल के MD5 चेकसम की गणना कैसे करूँ?


84

मैंने पायथन में एक कोड बनाया है जो एक फाइल में एक एमडी 5 के लिए जांच करता है और यह सुनिश्चित करता है कि एमडी 5 मूल से मेल खाता है।

यहाँ मैं विकसित किया है:

#Defines filename
filename = "file.exe"

#Gets MD5 from file 
def getmd5(filename):
    return m.hexdigest()

md5 = dict()

for fname in filename:
    md5[fname] = getmd5(fname)

#If statement for alerting the user whether the checksum passed or failed

if md5 == '>md5 will go here<': 
    print("MD5 Checksum passed. You may now close this window")
    input ("press enter")
else:
    print("MD5 Checksum failed. Incorrect MD5 in file 'filename'. Please download a    new copy")
    input("press enter") 
exit

लेकिन जब भी मैं कोड चलाता हूं, मुझे निम्न त्रुटि मिलती है:

Traceback (most recent call last):
File "C:\Users\Username\md5check.py", line 13, in <module>
 md5[fname] = getmd5(fname)
File "C:\Users\Username\md5check.py, line 9, in getmd5
  return m.hexdigest()
NameError: global name 'm' is not defined

क्या मुझे अपने कोड में कुछ भी याद नहीं है?

धन्यवाद।


जवाबों:


205

आपकी त्रुटि के संबंध में और आपके कोड में क्या कमी है। mएक ऐसा नाम है जिसे getmd5()फ़ंक्शन के लिए परिभाषित नहीं किया गया है।

कोई अपराध नहीं, मुझे पता है कि आप एक शुरुआत हैं, लेकिन आपका कोड सभी जगह है। आइए एक-एक करके आपके मुद्दों को देखें :)

सबसे पहले, आप hashlib.md5.hexdigest()सही तरीके से उपयोग नहीं कर रहे हैं । कृपया पायथन डॉक लाइब्रेरी में हैशलीब कार्यों पर स्पष्टीकरण देखें । उपलब्ध स्ट्रिंग के लिए MD5 को वापस करने का सही तरीका कुछ इस तरह है:

>>> import hashlib
>>> hashlib.md5("filename.exe").hexdigest()
'2a53375ff139d9837e93a38a279d63e5'

हालाँकि, आपको यहाँ एक बड़ी समस्या है। आप एक फ़ाइल नाम स्ट्रिंग पर MD5 की गणना कर रहे हैं , जहां वास्तविकता में MD5 की गणना फ़ाइल सामग्री के आधार पर की जाती है । आपको मूल रूप से फ़ाइल सामग्री को पढ़ना होगा और इसे MD5 से पाइप करना होगा। मेरा अगला उदाहरण बहुत कुशल नहीं है, लेकिन कुछ इस तरह है:

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

जैसा कि आप स्पष्ट रूप से देख सकते हैं कि दूसरा एमडी 5 हैश पहले वाले से बिल्कुल अलग है। इसका कारण यह है कि हम केवल फ़ाइल नाम के माध्यम से फ़ाइल की सामग्री को आगे बढ़ा रहे हैं।

एक सरल समाधान कुछ इस तरह हो सकता है:

# 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) 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!."

कृपया पोस्ट पायथन को देखें: किसी फ़ाइल का MD5 चेकसम उत्पन्न करना । यह विस्तार से बताता है कि यह कुशलता से कैसे प्राप्त किया जा सकता है।

शुभकामनाएँ।


1
वाह। मुझे बहुत शर्मिंदगी महसूस होती है। मुझे लगता है कि मैं जो कर रहा था उसके लिए मैंने गलत कोड डाल दिया, और इसके साथ बहुत सारी गलतियाँ जोड़ दीं। आपकी सहायता के लिए धन्यवाद। मैं हालांकि बैच और लुआ के लिए अधिक उपयोग किया जाता हूं। इसलिए अजगर मेरे लिए उपयुक्त है।
user2344996

19
आपको फ़ाइल को बाइनरी मोड में ओपन (फ़ाइल_नाम, 'आरबी') के साथ भी खोलना चाहिए, अन्यथा ओएस के न्यूलाइन / कैरिज रिटर्न रूपांतरण होने पर आपको समस्या हो सकती है। देखें mail.python.org/pipermail/tutor/2004-January/027634.html और stackoverflow.com/questions/3431825/...
twobeers

4
यदि आप द्विआधारी फ़ाइल पर कार्य कर रहे हैं, तो सुनिश्चित करें कि आपने इसे 'बी' मोड के साथ सही ढंग से पढ़ा है, अंत में मैं इसे इस तरह से उम्मीद के अनुसार काम करता हूं: hashlib.sha512 (खुला (fn, 'आरबी'))। (पढ़ें)। hexdigest ()
जेमी ली

9

पायथन 3.8+ में आप कर सकते हैं

import hashlib

with open("your_filename.png", "rb") as f:
    file_hash = hashlib.md5()
    while chunk := f.read(8192):
        file_hash.update(chunk)

print(file_hash.digest())
print(file_hash.hexdigest())  # to get a printable str instead of bytes

पायथॉन 3.7 और उससे नीचे पर:

with open("your_filename.png", "rb") as f:
    file_hash = hashlib.md5()
    chunk = f.read(8192)
    while chunk:
        file_hash.update(chunk)
        chunk = f.read(8192)

print(file_hash.hexdigest())

यह एक बार में 8192 (या 2ds) बाइट्स को f.read()कम मेमोरी का उपयोग करने के लिए एक बार में सभी के बजाय एक बार पढ़ता है ।


उपयोग करने पर विचार hashlib.blake2bकरने के बजाय md5(बस की जगह md5के साथ blake2bऊपर स्निपेट में)। यह क्रिप्टोग्राफिक रूप से सुरक्षित और एमडी 5 से तेज है।


0

आप बाइनरी डेटा पढ़कर और उपयोग करके किसी फ़ाइल के चेकसम की गणना कर सकते हैं hashlib.md5().hexdigest()। ऐसा करने के लिए एक समारोह निम्नलिखित की तरह दिखेगा:

def File_Checksum_Dis(dirname):
    
    if not os.path.exists(dirname):
        print(dirname+" directory is not existing");
    
    for fname in os.listdir(dirname):
        if not fname.endswith('~'):
            fnaav = os.path.join(dirname, fname);
            fd = open(fnaav, 'rb');
            data = fd.read();
            fd.close();
        
            print("-"*70);
            print("File Name is: ",fname);          
            print(hashlib.md5(data).hexdigest())
            print("-"*70);
                
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.