How to translate this code from PHP to PHP?

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 piece of code seems incomplete. But here I think what happens:

  • concatenation of dateSince, siteID and sharedSecret. Theft of cowards.

  • ???

  • converting this string to an ascii encoded byte array.

  • taking the MD5 hash of this array.

This mysterious BitConverter object apparently converts this MD5 array into a string of hexadecimal numbers. According to the above document, the result value should look something like this: "6D-E9-9A-B6-73-D8-10-79-BC-4F-EE-51-A4-84-15-D8"

Any help is much appreciated!


Forgot to turn it on before. Here's the PHP version of what I have 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);
+5
source share
2

, . , MD5CryptoServiceProvider:: ComputeHash 16 , 32 , PHP md5(). PHP md5() , "raw output", 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
+1

, , , , , . , - .

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);
0

All Articles