View Single Post
  #1   (View Single Post)  
Old 15th January 2010
s0xxx's Avatar
s0xxx s0xxx is offline
Package Pilot
 
Join Date: May 2008
Posts: 192
Default Script to check md5 hash on FreeBSD

Recently I needed to check some md5 hashes of some files on NetBSD. I started toying with awk, and just glanced on a cksum(1) man page. There was the "-c" option staring at me, which is being used to check md5 hashes from a file. The option was not actually available until NetBSD 4.0. Reading further on the net, I discovered that FreeBSD does not have that option (OpenBSD cksum has the "-c" option). One could get the option by installing md5sum (from sysutils/coreutils), but it is not in base system. To cut the long story short, here is the code that someone might find useful:
Code:
#!/bin/sh
# awk code for: cksum -c MD5
        md5 "$1" | \
        awk '
         NR==FNR{
                a[$2]=$4
                next
        }
        ($2 in a){
                if(a[$2] == $4)
                        print "OK: " $2
                else
                        print "ERROR: " $2
        }' - "$2"
Example:
Code:
$ md5 file.iso >CKSUM.MD5          
$ cat CKSUM.MD5                       
MD5 (file.iso) = b76561047748615d9a6a40da795fac3d
$ ./md5_check.sh file.iso
OK: (file.iso)
$
Script could be useful for those that don't have gmd5sum (i.e. GNU md5) installed, and they want to check the md5 hash of a FreeBSD iso file they just downloaded.
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn.
If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD

Last edited by s0xxx; 17th January 2010 at 12:10 AM.
Reply With Quote