Verify file integrity after downloading

47 views Asked by At

I just have a quick question. I am currently working on a script which just downloads the .ipsw files from apple servers, moves them to a temp location with a random name. Now I have the part to try to verify if it downloaded correctly before renaming to the original file name and moving the file to its final destination. After that the scripts moves on the the next file and so on.

My code looks like that so far :

# Aktualisiere die Live-Prozentanzeige und Geschwindigkeitsanzeige
                current_time = datetime.now()
                elapsed_time = current_time - last_update_time
                if elapsed_time.total_seconds() >= 1:
                    speed = ((downloaded_size - last_downloaded_size) / elapsed_time.total_seconds()) / (1024 * 1024)
                    progress = downloaded_size / file_size * 100
                    print(f"Downloading {filename}: {progress:.1f}% - Speed: {speed:.2f} MB/s", end="\r")
                    logging.info(f"Downloading {filename}: {progress:.1f}% - Speed: {speed:.2f} MB/s")
                    last_downloaded_size = downloaded_size
                    last_update_time = current_time

    # Meldung nach erfolgreichem Download
    print("Erfolgreich heruntergeladen. MD5 Prüfung.....")
    #logging.info(f"File downloaded: {filename}. Verifying the file...")
    #print(f"File downloaded: {filename}. Verifying the file...")

    # MD5-Prüfung
    if filename in hashes and hashlib.md5(open(temp_path, 'rb').read()).hexdigest() == hashes[filename]:
        new_filename = filename
        if new_filename.endswith(".ipsw"):
            new_filename = new_filename[:-5]
        new_filename = new_filename + ".ipsw"
        output_path = os.path.join(output_directory, new_filename)
        os.rename(temp_path, output_path)
        logging.info(f"Downloaded: {filename}. Renamed to: {new_filename}. Moved to: {output_directory}")
        print(f"Downloaded: {filename}. Renamed to: {new_filename}. Moved to: {output_directory}")
        completed_files.append(filename)
        print("Geschafft???")
    else:
        os.remove(temp_path)
        logging.error(f"Failed (hash mismatch): {filename}")
        print(f"Failed (hash mismatch): {filename}")
        user_input = input("Hash mismatch. Do you want to continue? (y/n): ")
        if user_input.lower() == "n":
            break

The problem is I tried to read the hashes from a file which used the headers only. Therefore the hashes are incorrect and the script terminates.

Question:

Is there any way to get the full MD5-Hash from the endpoint URL via python and then calculate the MD5 from the downloaded file and compare them?

Bother with me please. I am new to this kind of stuff.

Thanks so much!

0

There are 0 answers