03
Oct
09

secure copy



Several times when I copied some files to my USB drive, they got corrupted some way, and their MD5 was different. To solve the unnecessary checks, I wrote a python script to perform them for me, and to copy the files again if they get corrupted. Here it is:


import shutil
import hashlib
import os
import sys

# utility function to get the md5 of a file
def get_md5_signature(file_path):
  m = hashlib.md5()
  try:
    h = open(file_path)
    while True:
      line = h.read(4096)
      if line == "":
        h.close()
        break
      m.update(line)
  except IOError:
    return "md5 error"
  return m.hexdigest()

# copy a file to a destination
# checks the md5 to see if the file's been successfully copied
def secure_copy(original_file,destination):
  final_destination = None
  if os.path.isdir(destination):
    final_destination = os.path.join(destination,os.path.basename(original_file))
  else:
    final_destination = original_file
  print "getting signature"
  original_sig = get_md5_signature(original_file)
  print "signature is %s" % original_sig
  while True:
    print "copying to %s" % final_destination
    shutil.copy(original_file,final_destination)
    print "copy is over. checking md5"
    new_sig = get_md5_signature(final_destination)
    print "final destination signature is %s" % new_sig
    if new_sig == original_sig:
      print "copy was successful"
      break
    else:
      print "copy was not successful. new signature is %s" % new_sig
      os.unlink(final_destination)
      
    
if __name__ == "__main__":
  if len(sys.argv) == 3:
    secure_copy(sys.argv[1],sys.argv[2])
  else:
    print "usage: secu.py original destination"

Advertisements

2 Responses to “secure copy”


  1. September 5, 2011 at 17:48

    Interesting idea but these days it would be more pythonic to write:

    _MD5_BLOCK = 4096

    def get_md5_signature(path, block=_MD5_BLOCK):
    m = hashlib.md5()
    with open(path, ‘rb’) as f:
    for chunk in iter(lambda: f.read(block), ”):
    m.update(chunk)
    return m.hexdigest()


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 223,857 hits

%d bloggers like this: