Come tutti sapete ogni file ha un hash MD5.
In Linux esiste un comando che si chiama "md5sum". Visto che non tutti lo sanno usare perfettamente, ho fatto un piccolissimo (si fa per dire) script in Python che permette di usare facilmente il comando.
Codice:
Che ne dite?
Testato su: Ubuntu 8.10 Italiano - Python 2.5.2
In Linux esiste un comando che si chiama "md5sum". Visto che non tutti lo sanno usare perfettamente, ho fatto un piccolissimo (si fa per dire) script in Python che permette di usare facilmente il comando.
Codice:
Codice:
#!/usr/bin/env python
import os
import commands
import string
print "##### MD5SUM HELPER ####\nCreated by Roberto - http://www.informarts.org\nBreve spiegazione: Questa utility serve per sfruttare in modo semplice il comando 'md5sum' delle distribuzioni GNU/Linux."
print "\nInserisci una delle seguenti lettere:"
print "\tA. Genera l'hash MD5 di un solo file"
print "\tB. Controlla l'hash MD5 di un solo file"
print "\tC. Genera l'hash MD5 di piu' files"
print "\tD. Controlla l'hash MD5 di piu' files"
print "\tE. Esci"
scelta = raw_input ();
if (scelta.upper() == "A"):
file = raw_input ("\nInserisci il nome del file\n")
if file != "":
file = file.replace ("\\", "\\\\")
if os.path.exists (file):
t = commands.getstatusoutput("md5sum " + str (file))
t = t[1]
t = t.split (" ")
if (len(t) == 2):
hash = t[0]
file = t[1]
print "Hash del file "+str(file)+": "+hash
print "Vuoi salvare il file? (s/n)"
print "NB: Se in futuro si vorra' verificare l'hash, allora e' necessario salvare il file."
scelta = raw_input ()
if scelta == "s":
print "Inserisci il nome del file (sara' automaticamente salvato con il formato .md5)"
name = raw_input()
if name != "":
commands.getstatusoutput ("md5sum "+str(file)+" > "+str(name)+".md5")
print "File salvato con successo."
else:
print "Si e' verificato un errore durante la produzione dell'hash."
elif (scelta.upper() == "B"):
file = raw_input ("Inserisci il nome del file MD5 generato\n")
if file != "":
file = file.replace ("\\", "\\\\")
if os.path.exists (file):
t = commands.getstatusoutput ("md5sum -c "+str(file))
t = t[1]
if string.find (t, "OK") > 0 and string.find (t, "ATTENZIONE") < 0:
t2 = t.split (": ")
if len(t2) == 2:
print "L'hash del file "+str(t2[0])+" corrisponde a quello del file "+file
else:
print "Errore durante la verifica dell'hash"
elif string.find (t, "FALLITO") or string.find (t, "FAILED"):
t2 = t.split (": ")
if (len(t2) > 2):
print "L'hash del file "+str(t2[0])+" NON corrisponde a quello del file "+file
else:
print "Errore durante la verifica dell'hash."
else:
print "Stringa sconosciuta: "+str(t)
elif (scelta.upper() == "C"):
files = raw_input ("Inserisci i nomi dei files (separati da uno spazio)\n")
t = files.split(" ")
if len(t) > 1 and files != "":
for i in range (0, len (t)):
temp = t[i].replace ("\\", "\\\\")
if os.path.exists (temp) == False:
print "File "+temp+" non esistente"
noexists = 1
break
else:
noexists = 0
if noexists == 0:
command = commands.getstatusoutput ("md5sum "+str(files))
temp = command[1].split ("\n")
for d in range (0, len (temp)):
who = temp[d]
rt = who.split (" ")
if len (rt) == 2:
hash = rt[0]
filename = rt[1]
print "Hash del file "+filename+": "+hash
print "Vuoi salvare il risultato in un file? (s/n)"
print "NB: Se in seguito si vogliono verificare gli hash e' necessario salvare il risultato"
result = raw_input ()
if (result == "s"):
print "Inserisci il nome del file in cui salvare gli hash (sara' automaticamente aggiunta l'estensione .md5)"
fil = raw_input()
if fil != "":
commands.getstatusoutput ("md5sum "+str(files)+" > "+fil+".md5")
print "Hash salvati con successo nel file '"+fil+".md5'"
else:
print "Nomi dei files non validi"
elif (scelta.upper() == "D"):
file = raw_input ("Inserisci il nome del file .md5 generato dallo script\n")
if file != "":
file = file.replace ("\\", "\\\\")
if os.path.exists (file):
c = commands.getstatusoutput ("md5sum -c "+str(file))
c = c[1]
lista = c.split ("\n")
if lista > 1:
for i in range (0, len (lista)):
temp = lista[i].split (": ")
if len(temp) == 2:
file_ = temp[0]
result = temp[1]
if result == "OK":
print "Il file "+str(file_)+" corrisponde all'hash presente in "+file
elif result == "FALLITO" or result == "FAILED":
print "Il file "+str(file_)+" NON corrisponde all'hash presente in "+file
else:
print "File "+str(file_)+": hash sconosciuto"
elif (scelta.upper() == "E"):
x = 0
else:
print "Inserisci un parametro valido"
Che ne dite?
Testato su: Ubuntu 8.10 Italiano - Python 2.5.2