tags:

views:

140

answers:

2

I was handed an software doc (or what passes for proper documentation nowadays). Much to my chagrin, it might as well have been gently used piece of toilet paper from the men's room. Needless to say, the contents of said doc left much to be desired.

So, I am left with a great mystery. This doc describes some sort of API; or so I'm told. How such a doc could describe an actual piece of software is beyond me, and beyond the scope of my current inquiry. Anyway, on to my question....

Once such code snippet in this doc is written in C#. Being a strictly open-source/Linux guy, I don't know C# for the life of me.

I'm on a PHP setup. In other words I need a PHP version of the following C# code:

string dateSince = "2010-02-01";
string siteID = "bash.org";
string sharedSecret = "12345"; // the same combination on my luggage!

using System.Security.Cryptography;

MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
byte[] dataBytes = System.Text.Encoding.ASCII.GetBytes(string.Format("{0}{1}{2}",   dateSince,  siteID, sharedSecret));
string result = BitConverter.ToString(x.ComputeHash(dataBytes));

... this code snippet appears to be incomplete. But here's what I think is going on:

1) concatenating dateSince, siteID, and sharedSecret. Stealing underpants.

2) ???

2a) converting that string into a ascii encoded byte array.

2b) taking the MD5 hash of that array.

3) PROFIT!!

This mysterious BitConverter object appears to be converting that MD5 hashed array, into a string of hexadecimal numbers. According to the aforementioned doc, the value of result should look something like: "6D-E9-9A-B6-73-D8-10-79-BC-4F-EE-51-A4-84-15-D8"

Any help is greatly appreciated!!


Forgot to include this earlier. Here's the PHP version of what I've written so far:

$date_since = "2010-02-01";
$site_id = "bash.org";
$shared_secret = "12345";

$initial_token = $date_since.$site_id.$shared_secret;

$ascii_version = array();
foreach($i=0; $i < strlen($initial_token); $i++) {
    $ascii_version[] = ord(substr($initial_token,$i,1));
}

$md5_version = md5(join("", $ascii_version));

$hexadecimal_bits = array();
foreach($i=0; $i < strlen($md5_version); $i++) {
   // @todo convert to hexadecimal here?
   $hexadecimal_bits[] = bin2hex(substr($md5_version,$i,1));
}

$result = join("-", $hexadecimal_bits);
A: 

This is what I see except that what it outputs won't input the dashes. Those would have to be injected some other way.

var $dateSince = "2010-02-01"; 
var $siteID = "bash.org"; 
var $sharedSecret = "12345"; // the same combination on my luggage! 

var $full_string = $dateSince . $siteID . $sharedSecret;

string result = md5($full_string);
Joel Etherton
+1  A: 

I think this will work for you. It looks like the MD5CryptoServiceProvider::ComputeHash method returns an array of 16 bytes, not a string of 32 characters like the normal PHP md5() function. However, PHP's md5() has a second optional parameter that forces "raw output", which does correspond to the output of ComputeHash().

$date_since = "2010-02-01";
$site_id = "bash.org";
$shared_secret = "12345";
$initial_token = $date_since.$site_id.$shared_secret;

//get the RAW FORMAT md5 hash
//corresponds to the output of MD5CryptoServiceProvider::ComputeHash
$str = md5($initial_token, true);
$len = strlen($str);
$hex = array();
for($i = 0; $i < $len; $i++) {
    //convert the byte to a hex string representation (left padded with zeros)
    $hex[] = str_pad(dechex(ord($str[$i])), 2, '0', STR_PAD_LEFT);
}
//dump output
echo implode("-",$hex);

//outputs fe-0d-58-fd-5f-3d-83-fe-0f-6a-02-b4-94-0c-aa-7b
zombat
Thanks for your explanation! It makes a lot more sense now. You're a great help zombat. I really appreciate it.
sayajay
Np, happy to help.
zombat