Aug 302012
 

An ill wind feeds my discontent it
grows with every lost moment and
steeped in deep anxiety it
grows the pit inside of me
it grows
the pit inside of me it grows the pit
consuming me

I lie awake, I pace the floor,
I watch the ever present door and
stare into the deep abyss
the hole
that ate the self I miss
the hole that ate the self
I miss the whole
I miss the wholeness

Selfless, lost, and still
not going far or fast
Perpetuation lasts and lasts
this yearning, churning, throbbing,
beating fast
I gasp!
breathe deep and let it pass
then start again
then let it pass
again and then

Again the thickening morass
my clouded mind, my faded past
The distant memories
obscured by present miseries and
blurred uncertain
treacheries I sense as apparitions hence

My aspect and my furrowed brow now
fearful to behold and yet
somehow all tempered by the very madness
working to conceal the sadness
stalking me and
still not talking

So I pull hard and
I strain in vein
I try not to complain
Insane, I give in to the pain and
then control myself again
and then again
control myself again
back where I started once again

 

 

Aug 252012
 

I needed to create MD5 hashes to populate a password database for apache. This seemed like a very simple thing. So, when I wanted an MD5 hash in hex for my JSP app I expected to find a utility ready and waiting inside java. No such luck. No problem, I thought — I’ll just “google” it.

I was surprised to find that there are lots of half-baked solutions out there posted on various discussion forums, but none of them were solid, simple, and self-explanatory; or at least they didn’t look like code I would be comfortable with. So I decided to write it up and make it easy to find just in case someone else has the same experience.

My solution breaks out into three tiny functions that might be re-used lots of places.

import java.util.*;
import java.security.*;

String HexForByte(byte b) {
    String Hex = Integer.toHexString((int) b & 0xff);
    boolean hasTwoDigits = (2 == Hex.length());
    if(hasTwoDigits) return Hex;
    else return "0" + Hex;
}

String HexForBytes(byte[] bytes) {
    StringBuffer sb = new StringBuffer();
    for(byte b : bytes) sb.append(HexForByte(b));
    return sb.toString();
}

String HexMD5ForString(String text) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    byte[] digest = md5.digest(text.getBytes());
    return HexForBytes(digest);
}

HexForByte(byte b) gives you a two digit hex string for any byte. This is important because Integer.toHexString() will give you only one digit if the input is less than 16. That can be a real problem if you are building hash strings. Another tricky bit in this function strips the sign from the byte before converting it to an integer. In java, every kind of number is signed so we have to watch out for that when making conversions.

HexForBytes(byte[] bytes) gives you a hex string for any array of bytes. Each byte will be correctly represented by precisely two hex digits. No more, no less.

Wrapping it all up, HexMD5ForString(String text) gives you an MD5 digest in hex of any string. According to the apache documentation this is what I will want to put into the database so that mod_auth_digest can authenticate users of my web app. To see what started all of this look here: http://httpd.apache.org/docs/2.4/misc/password_encryptions.html

With the code above in place I can now do something like:

HexMD5ForString( user + ":" + realm + ":" + password );

From the look of it, the Java code on the apache page looks like it will work; and it may be faster; but doing it my way the code is less obscure and yields a few extra utility functions that can be useful other places.