0

i have this function in .Net C# and want to do the same in PHP. But i don't understand all C# part and not sure about my code.

public static string getMD5Hash(string input)
{
    MD5 mD = new MD5CryptoServiceProvider();
    byte[] array = Encoding.UTF8.GetBytes(input);
    array = mD.ComputeHash(array);
    StringBuilder stringBuilder = new StringBuilder();
    int i = 0;
    byte[] array2 = array;
    int length = array2.Length;
    while (i < length)
    {
        stringBuilder.Append(array2[i].ToString("x2").ToLower());
        i++;
    }
    return stringBuilder.ToString();
}

i have found an exemple who describe how to create MD5 Hash from a string (who is the same) here : http://en.csharp-online.net/Create_a_MD5_Hash_from_a_string

And found the SoF post about String to Byte : String to byte array in php

So here what i've done just to make some test :

function getMD5Hash($input){


for($i = 0; $i < strlen($input); $i++){

    $char = substr($input,$i,1);
    $hex_ary[] = sprintf("%02X", ord($char));}


$TheMD5=md5(implode('',$hex_ary));

for($i = 0; $i < strlen($TheMD5); $i++){

    $char = substr($TheMD5,$i,1);
    $Sec_hex_ary[] .= sprintf("%02X", ord($char));}


$StringBuilder ='';
$i = 0;
$Thelength = count($Sec_hex_ary);

while ($i < $Thelength)
{
    $stringBuilder .= strtolower(sprintf("%02X", ord($Sec_hex_ary[$i])));
    $i++;
}

return $stringBuilder;}

So what i understand (i hope good) :

  1. First call the md5 provider. PHP => Not Needed
  2. Then Create an array 'byte[]' 'array' who contain 'input' in bytes representation.
  3. Then Encode 'array' in md5.
  4. Create a new string.
  5. Then Create a array 'byte[]' 'array2' who contain 'array'
  6. Then Append the String with the String representation of bytes in lowercase.
  7. return the String.

Am i wrong ? if yes (certainly) can you explain me ?

So what it give me : "echo getMD5Hash('1ca48ad63d48c3adfae0d7af77f27027'); "

Print_r($hex_ary) =

Array
(
    [0] => 31
    [1] => 63
    [2] => 61
    [3] => 34
    [4] => 38
    [5] => 61
    [6] => 64
    [7] => 36
    [8] => 33
    [9] => 64
    [10] => 34
    [11] => 38
    [12] => 63
    [13] => 33
    [14] => 61
    [15] => 64
    [16] => 66
    [17] => 61
    [18] => 65
    [19] => 30
    [20] => 64
    [21] => 37
    [22] => 61
    [23] => 66
    [24] => 37
    [25] => 37
    [26] => 66
    [27] => 32
    [28] => 37
    [29] => 30
    [30] => 32
    [31] => 37
)

echo $TheMD5;

=> 418868f1137ea82935bbd235133993cf

print_r($Sec_hex_ary);

Array
(
    [0] => 34
    [1] => 31
    [2] => 38
    [3] => 38
    [4] => 36
    [5] => 38
    [6] => 66
    [7] => 31
    [8] => 31
    [9] => 33
    [10] => 37
    [11] => 65
    [12] => 61
    [13] => 38
    [14] => 32
    [15] => 39
    [16] => 33
    [17] => 35
    [18] => 62
    [19] => 62
    [20] => 64
    [21] => 32
    [22] => 33
    [23] => 35
    [24] => 31
    [25] => 33
    [26] => 33
    [27] => 39
    [28] => 39
    [29] => 33
    [30] => 63
    [31] => 66
)

And the echo give me : 3333333333333633333333363633333333333636363333333333333333333636

do you think this is correct ? Thanks for the help.

Community
  • 1
  • 1
Cetic
  • 51
  • 1
  • 3

1 Answers1

1
  • PHP's "byte arrays" are strings, no need to use actual arrays
  • if your string is already encoded in UTF-8, there's no necessary equivalent to the Encoding.UTF8.GetBytes(input) line; if it is not, convert it using iconv
  • PHP's md5 already returns a hex string, no need to build one from the raw output

Altogether, the PHP equivalent to that C# function is probably:

echo md5($input);

Having said that, there may be intricacies about C# that elude me here that may change the result.

deceze
  • 510,633
  • 85
  • 743
  • 889