translating Java encoding/hashing code to Ruby in a rails ap

Good Day to all!

I am trying desperately to solve the following problem and need the help of a Rails/Ruby guru. I am receiving data via a POST request from a remote web application. The information is composed of 5 fields of data plus a sixth field that's the encoded version of the first 5 bits plus a shared secret. The problem I am having is converting the sample code I was given (Java) to Ruby so that I can re-hash the data in my Rails app to confirm the validity of the data.

The allDataElements string is the same for both sets of code - a string of key, value pairs from the POST plus the shared secret

"userID=123456&timeStamp=04/21/2008&firstName=myfirstname&lastName=mylastname&firm=G mycompanyName&sharedSecret=SOMEREALLYCRYPTICTEXT"

Once I hash this text I should be able to compare it to the hashed value I was passed in the POST request.

Here's the Java code:     //Generate hash from profile data in hashmap object     MessageDigest md = MessageDigest.getInstance("MD5");     byte mashedHash = md.digest(allDataElements.getBytes("UTF-8"));     BASE64Encoder encoder = new BASE64Encoder();     //Encode to pass as a printable String to The Hartford website     yourhashedKey = encoder.encode(mashedHash);

Here's my ruby code:

    yourhashedKey = = Base64.encode64(Digest::MD5.digest(allDataElements))

The return value from my code does not match what I receive from the Java code. I believe it has to do with teh UTF-8 parameter in the getBytes function. Can someone help? I'm desperate!!!!

Thank you for your help.

Once I hash this text I should be able to compare it to the hashed
value I was passed in the POST request.

Here's the Java code:    //Generate hash from profile data in hashmap object    MessageDigest md = MessageDigest.getInstance("MD5");    byte mashedHash = md.digest(allDataElements.getBytes("UTF-8"));    BASE64Encoder encoder = new BASE64Encoder();    //Encode to pass as a printable String to The Hartford website    yourhashedKey = encoder.encode(mashedHash);

Here's my ruby code:

   yourhashedKey = = Base64.encode64(Digest::MD5.digest(allDataElements))

Does it work if all of your data is just plain ascii? in that case the
utf-8 ness wouldn't make any difference. How is allDataElements computed ? (both ruby & java )

Fred

Fred,

Thanks for getting back to me on this one. Unfortunately I don't have any insight/ability to test out what happens in the Java code as it's on another company's system. I have a request into them to try it without the UTF-8 part so I'll have to wait and see on that front.

However, here's how the allDataElements string is built. Basically, I just string together the 5 key/value pairs as if it were a GET request and then tack on the sharedSecret value. Here's the sample Java code I received (sytax errors and all). I have verified that only the variables listed in allDataElements are included in the hash even though there are other variables listed in the sample code.

  String secretKey = ""; //key we share   String userID = ""; //HIFP user id   String timeStamp = ""; //timeStamp must be in MM/DD/YYYY   String firstName = "";   String lastName = "";   String firm = "";   String emailAddress = "";   String address1 = "";   String address2 = "";   String city = "";   String state = "";   String zip = "";   String Phone = "";   String hashedKey = "";

  String allDataElements = "userID=" + userID                          + "&timeStamp=" + timeStamp                          + "&firstName=" + firstName);}                          + "&lastName=" + lastName);}                          + "&firm=" + firm);}                          + "&sharedSecret=" + secretKey); try   {     //Generate hash from profile data in hashmap object     MessageDigest md = MessageDigest.getInstance("MD5");     byte mashedHash = md.digest(allDataElements.getBytes("UTF-8"));     BASE64Encoder encoder = new BASE64Encoder();     //Encode to pass as a printable String to The Hartford website     yourhashedKey = encoder.encode(mashedHash);   }   catch (Exception e)   {     System.out.println("EMIvul.jsp - Exception: " + e.toString() );     yourhashedKey = null;   }

Somehow I need to turn this into Ruby. I had thought I would be fine with Base64.encode64(Digest::MD5.digest(allDataElements)) as I have done this type of thing from C# to Ruby before and it seemed to work out. The only difference is the getBytes("UTF-8") part.

Thank you for your help. I sincerely appreciate any insight you may have to offer.

Frederick Cheung wrote:

Everyone,

So, it turns out that the sample code from the client was wrong. Even though I had questioned the naming of the 'firmName' variable as 'firm' I had not tried changing the variable name until just now. I have notified the client that they should update their sample code.

I thank Fred for his generous willingness to help and I hope that someone finds the code useful down the road as it does turn out that:

    MessageDigest md = MessageDigest.getInstance("MD5");     byte mashedHash = md.digest(allDataElements.getBytes("UTF-8"));     BASE64Encoder encoder = new BASE64Encoder();     //Encode to pass as a printable String to The Hartford website     yourhashedKey = encoder.encode(mashedHash);

can be translated into ruby as

yourhashedKey = Base64.encode64(Digest::MD5.digest(allDataElements)) You probably want to formalize the code more, but you get the idea.

Best,

Jason