How can I use a MD5 or SHA1 checksum to verify a downloaded file?

Q:How can I use an MD5 or SHA1 checksum to verify a downloaded file?
A:

I recently wrote an article about MD5 and SHA1 hashing algorithms and how they are commonly used around the internet. Creating an MD5 or SHA1 hash is relatively easy. There are a good many applications out there that can be used to generate all sorts of checksums. Marc Liyanage has one for the Mac and CNET has one for Windows.

Both of these utilities are really great at creating checksums from source files. However, more commonly, what I need to do is to very simply compare a checksum provided by a developer with the file that is sitting on my computer. Most of the utilities out there provide a way to view the checksums of files, but in my experience, they don’t provide a clean way of comparing them for you. You have to visually look at them and check to see that they are the same.

I would like to say that there are programs that will do this, but I thought it would be a fun project to try to do this in AppleScript. A big thanks goes out to Marco Wales of ESCAPESOFT. I used part of his checksum generation AppleScript as part of the basic structure of this tool and modified it to compare the input of the original checksum provided by the developer with the one generated by the local file.

Feel free to copy and paste the code below into the “Script Editor” application and save it as an “Application”.
to genChecksum(aFile)
-- select checksum
display dialog "Please select a checksum algorithm" buttons {"SHA1", "MD5", "Cancel"} default button {"Cancel"}
set res to button returned of result
if res is "Cancel" then
return
end if
-- select file, if not set
if aFile is "" then
set aFile to choose file with prompt "Generate " & res & " checksum for selected file"
end if
-- generate checksum
if res is "SHA1" then
do shell script "/usr/bin/openssl sha1 " & quoted form of POSIX path of aFile
else -- if res is "MD5" then
do shell script "md5 " & quoted form of POSIX path of aFile
end if
-- display checksum
display dialog result buttons {"OK"} default button {"OK"} with title res & " checksum"
end genChecksum

-- if app double-clicked
genChecksum("")

-- if file drag and dropped
on open aFile
genChecksum(aFile)
end open