Home | Developer Blog

Tidbit - Calculating file checksums

I’m going to start posting what I’m calling tidbits - morsel sized pieces of code for common tasks on Mac OS X.

The first of these is computing a file checksum (MD5 to be precise). Apple provides the functionality in CommonCrypto/CommonDigest.h. This uses routines in libSystem and avoids loading the entire of OpenSSL to simply compute a checksum. The code is really simple:

#include <CommonCrypto/CommonDigest.h>

NSString* fileName = @"PATHTOFILE";
unsigned char md5[16];

NSData* data = [NSData dataWithContentsOfFile:fileName];
CC_MD5([data bytes], [data length], md5);

// md5 now contains the md5 digest for the file contents

Trivially easy. The CommonDigest.h header contains functions for MD2, MD4, MD5, and SHA1.

Safari Keyboard Shortcuts

Safari has a surprising number of keyboard shortcuts.

Some of the more useful shortcuts:

Command-F            Find (Search)
Command-G            Find Next (Search)
Command-L            Jump to the Address Bar
Command-Option-F     Jump to the Search Bar
Command-T            Open new tab
Command-W            Close current tab or window
Command-Shift-Left   Go to previous tab
Command-Shift-Right  Go to next tab
Command-D            Bookmark current page

This local URL will give you a comprehensive list of all the keyboard shortcuts supported in Safari:

file:///Applications/Safari.app/Contents/Resources/Shortcuts.html

Pretty neat.