Archive for the ‘Programming’ Category

Base64 encoding is a convenient way to represent binary data as plain text. This allows for transport of said binary data over a medium that is traditionally more text friendly. In the general case, simply storing binary data as text is quite handy sometimes.

The most common ways to accomplish this are either Base64 encoding, or to HEX. Base64 encoding takes 3 bytes of binary data and turns it into 4 bytes of text. HEX encoding takes 1 byte of binary data, and turns it into 2 bytes of text. 3 bytes of binary data would be transformed into 6 bytes with HEX. While the difference between 4 and 6 bytes isn’t that bad, if you multiply that difference millions of times, the savings really become significant. Therefore, Base64 encoding is more efficient in storage/transport than HEX.

So, here’s my shot at a simple Base64 encoding and decoding in Java. I hammered it out in about 15 minutes; hopefully it’s not horrendously bad. There are tons of other tried/tested Base64 encoders out there; this was an academic exercise at best … for now anyway …

As always, I don’t want to do all the work for you. This is working code, NOT safe code. Please make sure you understand what is happening here, and that you do your own error checking. Please take a look at the CFNetwork docs for detailed info:

It’s pure java with no weird requirements or external libraries. It performs reasonably well compared to the stock Java implementation of PBKDF2WithHmacSHA1; sometimes it beats it, sometimes it doesn’t. I’m sure there are ways to optimize performance.

A little background: RFC 2898 defines (among other things) the PBKDF2 algorithm for generating encryption keys based on a given password. The details are unimportant, but it produces a salted password hash that is computationally impractical to crack given a sufficiently long salt over enough iterations. RFC 2989 “recommends” an iteration count of 1000 or greater (section “4.2 Iteration Count”). It also says that salts “should” be at least 8 octets (or 64 bits) long (section “4.1 Salt”).

When reading RFC’s, terminology is extremely important in implementation. The words “should” and “recommend” are very different from the word “must” (check out RFC 2119). Things that say “should” are not required to conform to the spec; however, “must” explicitly defines an absolute requirement.

In the world of cryptography, “should” things are very important. Your algorithm may not be as secure as you think if you ignore the “should” stuff, even though you have implemented everything as defined in the RFC. In the case of the PBKDF2 algorithm, simply don’t use salts less than 8 octets and less iterations then 1000. Simple as that. It’s not required by the RFC, it’s just smart.

From a developer’s standpoint, the question always looms: Do I give my users enough rope to hang themselves? When implementing PBKDF2, should I even allow salts of less than 64 bits or small iteration counts? For super secure systems, the answer probably should be “no”.

Now, as a general rule, it’s a bad idea attempt to code your own hashing algorithms. There are only a handful of people on the planet who have a firm enough grasp of modern cryptography to understand all the intricacies and implications of improperly coded cryptographic algorithms. I am not one of those people, and odds are, neither are you. So just don’t do it, especially if there’s a tried-and-true implementation available.

With that said, if you are going to attempt to code your own algorithms, there are certain test vectors that you can run through your code to make sure everything works properly. For PBKDF2 derived keys, those test vectors are defined in RFC 6070. If you are testing PBKDF2 with HmacSHA1, this is the set of data you use to test.

Sooooooooo… I’ve been playing around with PBKDF2 a bit, trying my hand at coding the algorithm in various languages, and testing the implementations that exist in various frameworks. I know I just told you not to do such things, but in fairness I’m not trying to generate the HMACs myself. PBKDF2 isn’t a hashing alogrithm, it takes a bunch of hashes and squarshes them together in such a way that would be extremely difficult and time consuming to crack the key through traditional means. And yes, I said “squarshes”.

Anyway, in testing I find that there are frameworks out there that enforce the recommended salt length specified in RFC 2898. They are incapable of running 5 out of the 6 test vectors in RFC 6070 because their salt is “salt” or “sa\0lt”; both of which are less than 64 bits in length. Like I said “UGH!!!”.

I know this is relatively minor. You can always implicitly test the validity of algorithms by running the same test data through multiple known/working implementations. It’s just the principle of the thing, ya know?

First, a disclaimer: I love cURL, but there are times when you simply do not want/need to use third-party libraries. This post has nothing to do with cURL. It has everything to do with making HTTP requests with the native API’s for doing HTTP requests on Windows and OS X.

If you are here, odds are that you’ve just spent the last few hours in a futile attempt to understand either the WinInet or CFNetwork documentation. In fairness, the documentation from Microsoft and Apple is decent enough … But let’s face it, you want a quick and dirty example and there are none.

The following snippets are complete working examples. They are simple HTTP GET requests from start to finish. They DO NOT take into consideration proxy servers or HTTP auth. They DO NOT show you how to do an HTTP post. Once you get the basics though, it’s really not that far of a leap to get the rest.

If anyone would like to dive into those subject in more detail, we certainly can. Let me know, and we can talk.

Note that there’s not a lot of error handling in the code below. Just take a look at the API docs for information about what these functions return, and how to respond to errors.

Anyway, that’s all mostly for my benefit. I usually end up having to write that stupid function from scratch once or twice a month…

You also may ask “why would you bother with the nuts-and-bolts like this when you could use some existing javascript library/framework?”. Well, there are people who use frameworks … and then there are the people who write the frameworks. There’s a lot more job security in the latter :)

Both blocks of code perform the exact same function, but the first is considered poor practice because the assignment of the variable “conn” occurs in-line with the conditional statement. So, if they operate the same way, why is one bad and the other is not? Additionally, why is the one that performs the same operation with fewer lines of code bad?

It all stems from the use of the “=” assignment operator in the conditional statement. Take a look at this code:

a = 0;
b = 1;
// using conditional operator "==" to check for equality
if( (a==b) ) print "a and b are equal";
else print "a and b are not equal";
print "a=".a." and b=".b;
// using the assignment operator "=" in the wrong way
if( (a=b) ) print "a and b are equal";
else print "a and b are not equal";
print "a=".a." and b=".b;
a=1;
b=0;
// using the assignment operator "=" in the wrong way with different results
if( (a=b) ) print "a and b are equal";
else print "a and b are not equal"
// This program will output the following:
//
// a and b are not equal
// a=0 and b=1
// a and b are equal
// a=1 and b=1
// a and b are not equal
// a=0 and b=0

In the example above, the output is unreliable because of the use of the assignment operator in the conditional. While any experienced programmer should be able to catch these problems right away, a novice may not see exactly what is happening here.

This leads me the conclusion that perhaps assignments in conditionals are not functionally bad, but they can be confusing. So, I would say that if you know what you are doing, and are confident that the people reading your code know what they are doing, these in-line conditionals may not be all bad. Just don’t teach them to anybody new to programming. It’s the whole “we’re professionals, don’t try this at home” principle.

But there’s even a problem with this line of thinking. I see these in-line assignments all the time in tutorials and various texts (teaching people programming). I can’t tell you the number of times I’ve taken boiler plate code from a book, and ended up with tons of compile time warnings complaining about assignment operators used in conditional statements.

I’ve worked on projects with people who would throw you out of the development team if you wrote code that looks like the first example above. Other times, I’ve seen every “while” loop in a program look like this.

I tend to believe that moderation and readability are the keys here. Use assignments like this where it makes sense; just don’t over do it simply because you can.

While browsing the classifieds, I’ve come to the conclusion that I’ve got to refresh the skill set a little and brush up on some things I haven’t dealt with in a long time.

I’m primarily a Java and PHP developer, and 7 solid years of commercial & enterprise development experience is enough to get my foot in a lot of doors. While my particular expertise is in fair demand, there are a few other areas that seem to be a bit more sought after right now:

.NET Framework

You cannot throw a rock at job listings without hitting an ad for a C# developer. I’ve done a bit of .NET development in the past, but it’s been a few years; there are also some areas of the .NET framework that I’ve never played with. The Kinect SDK is also looking pretty tempting on a lot of fronts. So, I guess I’m going to spending some time in Visual Studio in the near future.

C/C++

Really good C programmers are getting harder and harder to find, but the demand isn’t going away. I wish I could say that I’m one of the really good ones, but unfortunately I’m not there (yet). My first meaningful attempts at software development were all C and C++, but I haven’t been tasked with it too much on a professional level. However, I’ve recently been diving back into some crypto stuff I did for OS X a little while ago in C++. So maybe I can get back into the swing of things a bit more.

Mobile Development (Java/Android and Objective-C/iOS)

There’s not as big of a demand for mobile developers as you’d guess, but theres still a good number of opportunities out there. I’ve spent a lot of time in Blackberry simulators/debuggers, but that experience has proved to be almost useless. I’ve got several personal projects going right now for android; hopefully I’ll be hitting the market with some encryption stuff in the coming months. I’m technically and officially a registered iOS developer, but I don’t own an iOS device … guess I’d better get one at some point (the simulator is getting old).

Advanced SQL

There are TONS of database opportunities out there, and I work with databases A LOT. In the last year, there’s not a major RDBMS package out there that I haven’t worked on in some way or another. I’d say I’m reasonably proficient, but because I have to deal with a lot of different databases, my experience is a bit more generic than I’d like. In particular, I need to brush up on some specific Oracle and MS SQL topics.

I really do enjoy learning new things (and re-learning old things), so I think I’ve got an interesting few months ahead of me…

Try as I may to switch to another CMS, I keep falling back to WordPress. My last attempt was Drupal, which I quickly grew tired of. WordPress doesn’t do everything I would like it to do, but the things it does are amazing.

I’ve been working a bit on my theme to add some extra functionality. Here’s what we’ve gotten so far:

Three column format: My sidebar were getting a bit too long, so I split it in two.

Variable width layout: Because there are three columns, I needed a bit of extra horizontal space. I was getting tired of the fixed width/centered layout, so I implemented the variable width middle column to hold the main content.

HTML5: I’m using quite a few HTML5 specific tags in my theme, but everything should also be ok in older browsers too. It was a bit of a beast getting rid of the XHTML-ness that WordPress puts in automatically, but I’m getting there.