Saturday, February 9, 2008

NSData Base64 extension

Encoding/decoding Base64 NSData seems to be a common problem that many Cocoa developers run into. I ended up writing my own category extension to the NSData -- but what I should have done is went to google and done a search for "NSData base64" where I would have found the exact code I was looking for.

Specifically: Dave Dribin's explaination of how to use the OpenSSL to do Base64 encoding/decoding. Incidentally using the OpenSSL library was the route I took as well -- yes, I am kicking myself for not taking the one second it takes to do a google search. I actually stumbled upon the base64 NSData Dave Dribin discusses at the beginning of his post and I was also put off by the lack of any licensing discussion.

On the other hand, I can justify reinventing the wheel because now the OpenSSL block I/O system isn't as mysterious as it once was. Only by actually implementing something with it can you really get how to use a library like this... and no, reading the documentation doesn't really help -- in fact, you'll probably get further reading the header files than the nearly non-existent online OpenSSL documentation. So with a grasp of the basics of the BIO system it's not such a far leap to add fancy encryption to NSData -- which is exactly what I had to do when I needed to encrypt and decrypt data.