Thursday, February 26, 2009

MD5 hash, Java vs Ruby

I needed to create a small program that would create a MD5 hexadecimal hash for a password. First I implemented this using Java.

import java.security.MessageDigest;
public class TestMD5 {
public static void main (String [] args) {
try {
String passwd = "password";
MessageDigest md =
MessageDigest.getInstance("MD5");
byte[] hash = md.digest(passwd.getBytes());
StringBuffer hexstr = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(
hash[i] & 0xFF);
if (hex.length() == 1) {
hexstr.append('0');
}
hexstr.append(hex);
}
System.out.println(hexstr.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}


Note: java.lang.Integer.toHexString does not add leading zeros. Without the length check after this function the hash string will not always be 32 charaters long.


Since I have started working with Ruby on Rails, I was curious to see how I would implement this in Ruby.

#!/usr/bin/ruby
require 'digest/md5'
passwd = "password"
digest = Digest::MD5.hexdigest(passwd)
puts digest


In this case Ruby wins for having to write less code to get the same result.

Wednesday, February 11, 2009

iPhone Dev dreams

I have had my iPhone for several months, and one of my favorite aspects of the iPhone is the large number of applications available at the App Store in iTunes. As a developer I was excited to learn how to develop applications for the iPhone. I went to the iPhone DevCenter on the Apple Developer connection web site and started doing some research. After some reading and watching a few the dev videos I was eager to download the SDK and play around with the iPhone simulator.

Wait I can't download the SDK !!





I don't have Mac OS X 10.5 Leopard Nooooooooooo!!

Ok maybe not that bad but, I was disappointed. Looks like my iPhone dev days will have to wait until I get a new Mac or whenever I get around to installing OS X on my Ubuntu/XP computer.

Tuesday, February 10, 2009

Source Code in blog ... again

A few posts after I outlined how to post source code in Blogger, it stopped working. Syntaxhighlighter released a new version and I don't really have time to figure out how to get it to work again.

I found a post that outlines how to use VIM to export the code and highlighting to html. After adding the CSS to the Blogger template produces the result below which I am much happier with.

public class HelloWorld()
public static void main(String [] args) {
System.out.println("hello world!");
}
}

VIM command:
:runtime syntax/2html.vim

Add to $HOME/.vimrc
syntax on
" Conversion HTML (:help 2html.vim)

let g:html_use_css = 1
let g:html_use_encoding = "utf8"
let g:use_xhtml = 1