• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Wrote my own checksum verifier

ardchoille

Android Expert
I needed a way to verify a checksum for a .ISO file I downloaded recently. I wasn't happy with the checksum verifiers I found (GUI, bloated, etc.) so I wrote my own. Simple, fast and runs great in Ubuntu.

Code:
#!/bin/bash
# Filename: csum.sh
# Version: 0.1
# Requires: md5sum, sha1sum, sed, bash
# Author: Ian MacGregor (aka ardchoille)
# License: GPL
# This script creates and verifies checksums
# with the option to save created checksum to file

mypath="$HOME"

getfilepath()
{
    myfile="/path/file"
    while [ ! -f $myfile ]
    do
        echo "Please enter the path and filename:"
        read myfile
        if [ ! -f $myfile ]
        then
            echo "The path you entered does not exist." 
            echo
        else
            return
        fi
    done
}

getsum()
{
    mysum=""
    while [ ! $mysum ]
    do
        echo "Please enter the expected checksum:"
        read mysum
        if [ ! $mysum ]
        then
            echo "You must provide a checksum to verify." 
            echo
        else
            return
        fi
    done
}

createmd5sum()
{
    getfilepath
    echo "Working..."
    cmd5sum=`md5sum "$myfile"`
    echo
    echo "MD5SUM report:"
    echo $cmd5sum
    echo
    echo "Save this MD5SUM report to $mypath/mymd5report?"
    echo "1" "Yes"
    echo "2" "No"
    echo "====================="
    echo "Please select 1 or 2"
    read mysavereport
    case $mysavereport in
    "1") echo $cmd5sum >> $mypath/mymd5report ; cmd5sum="" ; myfile="" ; mysavereport="" ; return ;;
    "2") cmd5sum="" ; myfile="" ; mysavereport="" ; return ;;
    *) echo "Please select 1 or 2" ;;
    esac
}

createsha1sum()
{
    getfilepath
    echo "Working..."
    csha1sum=`sha1sum "$myfile"`
    echo
    echo "SHA1SUM report:"
    echo $csha1sum
    echo
    echo "Save this SHA1SUM report to $mypath/mysha1report?"
    echo "1" "Yes"
    echo "2" "No"
    echo "====================="
    echo "Please select 1 or 2"
    read mysavereport
    case $mysavereport in
    "1") echo $csha1sum >> $mypath/mysha1report ; csha1sum="" ; myfile="" ; mysavereport="" ; return ;;
    "2") csha1sum="" ; myfile="" ; mysavereport="" ; return ;;
    *) echo "Please select 1 or 2" ;;
    esac
}

verifymd5sum()
{
    getfilepath
    echo
    getsum
    echo "Working..."
    checkfile=`md5sum "$myfile" | sed 's/ .*//'`
    echo
    if [ "$checkfile" = "$mysum" ]
    then
        echo "The expected md5sum matches the file's md5sum." 
    else
        echo "The checksums do not match." 
    fi
    echo
    echo "Press the Enter key for Main Menu..."
    read
    checkfile=""
    myfile=""
    mysum=""
    return
}

verifysha1sum()
{
    getfilepath
    echo
    getsum
    echo "Working..."
    checkfile=`sha1sum "$myfile" | sed 's/ .*//'`
    echo
    if [ "$checkfile" = "$mysum" ]
    then
        echo "The expected sha1sum matches the file's sha1sum." 
    else
        echo "The checksums do not match." 
    fi
    echo
    echo "Press the Enter key for Main Menu..."
    read
    checkfile=""
    myfile=""
    mysum=""
    return
}

while :
do
    clear
    echo "===================================="
    echo "Main Menu" 
    echo "===================================="
    echo "1" "Create an MD5 checksum for a file"
    echo "2" "Create an SHA1 checksum for a file"
    echo "3" "Verify a file by its MD5 checksum"
    echo "4" "Verify a file by its SHA1 checksum"
    echo "5" "Quit"
    echo "===================================="
    echo "Please select 1,2,3,4, or 5"
    read mych
    case $mych in
    "1") clear ; createmd5sum ;;
    "2") clear ; createsha1sum ;;
    "3") clear ; verifymd5sum ;;
    "4") clear ; verifysha1sum ;;
    "5") clear ; exit 0 ;;
    *) echo "Please select 1,2,3,4, or 5" ;;
    esac
done
 
But what is fun about just using a built in utility? We use Linux so we can tweak, even if a bit unnecessarily, right? :p

Anyway, the script looks good. I usually opt for md5sum, but to each his own. :)

-Nkk
 
Back
Top Bottom