What is the best way to make Calculate SHA1 as a context menu option in Mac OS X?

6

4

In order to calculate the SHA1 checksum of a downloaded file, I could type

/usr/bin/openssl sha1

in Terminal and then drag there the file which I want check. To make it simpler, one could enable a Context Menu item for this action.

What is the best way to create such item in Mac OS X 10.6? A detailed answer is appreciated, because I don't have good experience with AppleScript, etc.


Step by step

  1. Open Automator
  2. Create new service
  3. Choose to receive selected Files and Folders in Finder
  4. Add action Run Shell Script where your bash command is /usr/bin/openssl sha1 "$@" and you pass input as arguments

How can I get the output? Preferably in a Growl pop-up or a message window/dialog.

Andrei

Posted 2010-05-09T15:22:10.997

Reputation: 1 164

Answers

9

  1. Open Automator
  2. Create new service
  3. Choose to receive selected Files and Folders in Finder (note: this actually won't work too well on folders...)
  4. Add action Run Shell Script, set Shell to /bin/bash and Pass input to "as arguments", and enter this script:

    for file; do
        if [[ -d "$file" ]]; then
            echo "$(basename "$file") is a directory"
        else
            cd "$(dirname "$file")"
            /usr/bin/openssl sha1 "$(basename "$file")"
        fi
    done | tr "\n" "\r"
    
  5. Add action Run Applescript, and enter this script:

    on run {input, parameters}
        tell application "System Events"
            activate
            display dialog input buttons {"OK"} default button 1
        end tell
    end run
    
  6. Save the service with a descriptive name

Gordon Davisson

Posted 2010-05-09T15:22:10.997

Reputation: 28 538

3

I took Gordon's excellent answer as a starting point and embellished it a little bit. Posting those changes here in case anyone else might find them useful. My version calculates the MD5 as well as the SHA1 hashes (in a more readable format) and will also time out after 5 minutes if you forget to click the "OK" button instead of throwing an AppleScript error.

Shell Script

    for file; do
      if [[ -d "$file" ]]; then
        echo "$(basename "$file") is a directory"
      else
        cd "$(dirname "$file")"
        echo -e "$(basename "$file")\r"
        echo -n "MD5: "
        /usr/bin/openssl md5 "$(basename "$file")" | egrep -o [a-f0-9]{32}
        echo -n "SHA1: "
        /usr/bin/openssl sha1 "$(basename "$file")" | egrep -o [a-f0-9]{40}
      fi
    done | tr "\n" "\r"

AppleScript

    on run {input, parameters}
      with timeout of 360 seconds
        tell application "System Events"
          activate
          display dialog input buttons {"OK"} default button 1 with title "Cryptographic Hashes" giving up after 300 --seconds
        end tell
      end timeout
    end run

luckman212

Posted 2010-05-09T15:22:10.997

Reputation: 179

I'm including the SHA-256 by adding echo -n "SHA256: " \n shasum -a 256 "$(basename "$file")" | egrep -o [a-f0-9]{64} – henry – 2017-05-16T15:54:24.333