Wrote my own checksum verifier


  1. ardchoille

    ardchoille Well-Known Member

    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 (Text):
    1.  
    2. #!/bin/bash
    3. # Filename: csum.sh
    4. # Version: 0.1
    5. # Requires: md5sum, sha1sum, sed, bash
    6. # Author: Ian MacGregor (aka ardchoille)
    7. # License: GPL
    8. # This script creates and verifies checksums
    9. # with the option to save created checksum to file
    10.  
    11. mypath="$HOME"
    12.  
    13. getfilepath()
    14. {
    15.     myfile="/path/file"
    16.     while [ ! -f $myfile ]
    17.     do
    18.         echo "Please enter the path and filename:"
    19.         read myfile
    20.         if [ ! -f $myfile ]
    21.         then
    22.             echo "The path you entered does not exist."
    23.             echo
    24.         else
    25.             return
    26.         fi
    27.     done
    28. }
    29.  
    30. getsum()
    31. {
    32.     mysum=""
    33.     while [ ! $mysum ]
    34.     do
    35.         echo "Please enter the expected checksum:"
    36.         read mysum
    37.         if [ ! $mysum ]
    38.         then
    39.             echo "You must provide a checksum to verify."
    40.             echo
    41.         else
    42.             return
    43.         fi
    44.     done
    45. }
    46.  
    47. createmd5sum()
    48. {
    49.     getfilepath
    50.     echo "Working..."
    51.     cmd5sum=`md5sum "$myfile"`
    52.     echo
    53.     echo "MD5SUM report:"
    54.     echo $cmd5sum
    55.     echo
    56.     echo "Save this MD5SUM report to $mypath/mymd5report?"
    57.     echo "1" "Yes"
    58.     echo "2" "No"
    59.     echo "====================="
    60.     echo "Please select 1 or 2"
    61.     read mysavereport
    62.     case $mysavereport in
    63.     "1") echo $cmd5sum >> $mypath/mymd5report ; cmd5sum="" ; myfile="" ; mysavereport="" ; return ;;
    64.     "2") cmd5sum="" ; myfile="" ; mysavereport="" ; return ;;
    65.     *) echo "Please select 1 or 2" ;;
    66.     esac
    67. }
    68.  
    69. createsha1sum()
    70. {
    71.     getfilepath
    72.     echo "Working..."
    73.     csha1sum=`sha1sum "$myfile"`
    74.     echo
    75.     echo "SHA1SUM report:"
    76.     echo $csha1sum
    77.     echo
    78.     echo "Save this SHA1SUM report to $mypath/mysha1report?"
    79.     echo "1" "Yes"
    80.     echo "2" "No"
    81.     echo "====================="
    82.     echo "Please select 1 or 2"
    83.     read mysavereport
    84.     case $mysavereport in
    85.     "1") echo $csha1sum >> $mypath/mysha1report ; csha1sum="" ; myfile="" ; mysavereport="" ; return ;;
    86.     "2") csha1sum="" ; myfile="" ; mysavereport="" ; return ;;
    87.     *) echo "Please select 1 or 2" ;;
    88.     esac
    89. }
    90.  
    91. verifymd5sum()
    92. {
    93.     getfilepath
    94.     echo
    95.     getsum
    96.     echo "Working..."
    97.     checkfile=`md5sum "$myfile" | sed 's/ .*//'`
    98.     echo
    99.     if [ "$checkfile" = "$mysum" ]
    100.     then
    101.         echo "The expected md5sum matches the file's md5sum."
    102.     else
    103.         echo "The checksums do not match."
    104.     fi
    105.     echo
    106.     echo "Press the Enter key for Main Menu..."
    107.     read
    108.     checkfile=""
    109.     myfile=""
    110.     mysum=""
    111.     return
    112. }
    113.  
    114. verifysha1sum()
    115. {
    116.     getfilepath
    117.     echo
    118.     getsum
    119.     echo "Working..."
    120.     checkfile=`sha1sum "$myfile" | sed 's/ .*//'`
    121.     echo
    122.     if [ "$checkfile" = "$mysum" ]
    123.     then
    124.         echo "The expected sha1sum matches the file's sha1sum."
    125.     else
    126.         echo "The checksums do not match."
    127.     fi
    128.     echo
    129.     echo "Press the Enter key for Main Menu..."
    130.     read
    131.     checkfile=""
    132.     myfile=""
    133.     mysum=""
    134.     return
    135. }
    136.  
    137. while :
    138. do
    139.     clear
    140.     echo "===================================="
    141.     echo "Main Menu"
    142.     echo "===================================="
    143.     echo "1" "Create an MD5 checksum for a file"
    144.     echo "2" "Create an SHA1 checksum for a file"
    145.     echo "3" "Verify a file by its MD5 checksum"
    146.     echo "4" "Verify a file by its SHA1 checksum"
    147.     echo "5" "Quit"
    148.     echo "===================================="
    149.     echo "Please select 1,2,3,4, or 5"
    150.     read mych
    151.     case $mych in
    152.     "1") clear ; createmd5sum ;;
    153.     "2") clear ; createsha1sum ;;
    154.     "3") clear ; verifymd5sum ;;
    155.     "4") clear ; verifysha1sum ;;
    156.     "5") clear ; exit 0 ;;
    157.     *) echo "Please select 1,2,3,4, or 5" ;;
    158.     esac
    159. done
    160.  

    Advertisement
    :
    johnlgalt and alostpacket like this.
  2. johnlgalt

    johnlgalt Antidisestablishmentarian VIP Member

    alostpacket likes this.
  3. nkk

    nkk Well-Known Member

    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
    alostpacket and ardchoille like this.
  4. 9to5cynic

    9to5cynic Well-Known Member

    Awesome. Way to go. I'll try this next time I need to md5 anything.

    (Perhaps retire md5sum?)
  5. ardchoille

    ardchoille Well-Known Member

  6. Haggistech

    Haggistech Well-Known Member

    Cracking well done :)
Loading...

Share This Page