bcrypt_objc: Command Line Utilities in Objective-C

About a week ago, I advised Rogelio Gundino on Twitter that it was not only possible, but pretty fun to write command-line apps on OS X in Objective-C. I promised him that I’d make a blog post about it, and started drafting examples. The problem with sample code, of course, is that it’s sample code. There is a world of difference between:

  1. code written for an API presentation or tech demo
  2. code written to actually perform a useful task

Since I like to read (and write) the latter, I threw out three attempts at useful demonstration before I stumbled across Marco Arment’s clean-room implementation of the bcrypt cryptographic hash algorithm for PHP 5.3. Cryptography gets any developer nerd’s juices flowing, and I began to consider what it would take to port bcrypt to Objective-C.

It’s C All The Way Down

A nice side effect of Objective-C being a strict superset of ANSI C is that damn near any C code ever written is also Objective-C code that OS X and iOS developers can pick up and use without missing a beat (license restrictions notwithstanding, of course.) Instead of reinventing the bcrypt wheel, I was able to procure Ricardo Garcia’s public domain bcrypt C library from github to do the hard number crunching. This was a double-win: I didn’t have to reimplement a complex cryptographic algorithm by hand, and since it was written in C I was reasonably sure it would be more performant than anything I’d write myself.

Argumentative Parsing

When writing a command-line utility, you don’t have to worry about nibs, xibs, Interface Builder or writing AppKit/UIKit delegates. What you do have to concern yourself with is the proper parsing of command-line arguments. There are a ton of examples out there on how to do this in C. Since we’re working in Objective-C, however, I looked around for something that leveraged the language and environment. What I found was Dave Dribin’s ddcli command-line application framework. By writing a simple delegate object with the domain knowledge pertinent to your application’s arguments, you neatly avoid an unsightly main() function filled with if/else chains or long switch/case blocks while sticking with the creature comforts of Objective-C.

This Is Only A Test

I don’t really expect this project to go anywhere; I wrote it to see if I could pull it off and to provide compelling proof that Objective-C is not just for GUIs. That said, if you would like to grab the source and do something fun with it you can fork me on github.

Cheers, and happy coding!

This entry was posted in development and tagged , , , , , , , . Bookmark the permalink.

Comments are closed.