It’s becoming more and more important that downloaded files be checked to make sure they haven’t been tampered with. That’s done by generating checksums on downloaded files and making sure that the checksums match what the provider says they should be.

I recently ran across a post by Jacob Salmela where he used Hazel to automatically generate file checksums to validate that downloaded files were valid. I loved the idea but it proved cumbersome for my own usage. What I really wanted was to be able to trigger it on a file-by-file basis.

I made slight changes to Jacob’s code to support filenames with spaces and to add a SHA-512 checksum, but it’s mostly unchanged. The code computes checksums on a file and throws up a dialog box with all of the checksums.

To create the file service for yourself, open Automator (/Application/Automator.app) and create a new service (File -> New then choose Service). Then set the service to only work on ‘files or folders’.

Next, add a shell script action. Search by typing ‘shell’ in the ‘Name’ field and then drag ‘Run Shell Script’ to the right hand window. Make sure that ‘/bin/sh’ is selected as the ‘Shell’ and that ‘as arguments’ is selected as the ‘Pass input’ value. Then paste the below code into the big box. Save it as ‘Validate File’, and you’re done.

Now whenever you need to validate a file, right-click on the file and choose ‘Validate File’ from the Services menu. Now you can compare the generated checksums to the checksums published on the site you got the file from.

name=$(basename "$1")
osascript <<EOT
display dialog "
MD5:
$(md5 "$1" | awk '{print $NF}')

SHA1:
$(openssl sha1 "$1" | awk '{print $NF}')

SHA256:
$(openssl dgst -sha256 "$1" | awk '{print $NF}')

SHA512:
$(openssl dgst -sha512 "$1" | awk '{print $NF}')" with title "$(echo $name)" with icon Caution
EOT