Search code examples
macosbashclipboardchecksum

Fastest way to compare sha1 checksum in clipboard with sha1 of local file using OS X bash


I'm thinking maybe it's combining shasum and diff with a pipe or something...

I want to know the fastest way to compare an sha1 checksum copied from a website to my clipboard against the sha1 checksum of a local file which I've downloaded from the same site to verify its integrity.

For example, I have the sha1 string 94f7ee8a067ac57c6d35523d99d1f0097f8dc5cc in my clipboard from the Raspberry Pi NOOBS download page and I want to compare it against the checksum for the NOOBS_v1_9_0.zip file using the Terminal app, and I don't want to have to create a new file from the clipboard contents.

I think it's bash 3.2 (It's OS X 10.11.4)


Solution

  • You could use a command something like this in bash:

    if [[ $(pbpaste) == $(shasum file | awk '{print $1}') ]]; then echo 'matches'; fi
    

    Using that you could create a function like this (eg. add it to your ~/.bash_profile):

    shachk () { 
        if [[ $(pbpaste) == $(shasum "$@" | awk '{print $1}') ]]; then echo 'match'; fi ;
    }
    

    So on the commandline you could simply input:

    $ shachk somefile
    

    Then it would compare it against the hash on your pasteboard.

    EDIT: A slightly improved version of the function which returns the file path, matches/failed, and colorizes the output.

    shachk() { 
        [[ $(pbpaste) == $(shasum "$@" | awk '{print $1}') ]] \
        && echo $1 == $(pbpaste) $'\e[1;32mMATCHES\e[0m' && return; \
        echo $1 != $(pbpaste) $'\e[1;31mFAILED\e[0m' ; 
    }