净C#到PHP铅与byte []数组和放大器; ComputeHash [英] .Net C# to PHP Pb with byte[] array & ComputeHash
问题描述
我在净C#这个功能,想用PHP做相同的。但我不明白,所有的C#的一部分,不知道我的code。
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();
}
我已经找到谁介绍了如何从一个字符串(谁是相同的)创建MD5哈希的为例此处的 http://en.csharp-online.net/Create_a_MD5_Hash_from_a_string
和发现的SOF后约字符串字节:字符串字节数组在PHP
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) :
- 第一步拨打MD5提供商。 PHP =>不需要
- 然后创建一个数组的byte []''阵'谁包含以字节为单位输入重新presentation。
- 然后连接code在MD5'阵'。
- 创建一个新的字符串。
- 然后创建一个阵'的byte []''ARRAY2谁包含数组
- 在随后追加的字符串再以小写字节presentation字符串。
- 返回字符串。
我错了?如果是(当然)你能解释一下我吗?
Am i wrong ? if yes (certainly) can you explain me ?
那么,它给我:回声getMD5Hash('1ca48ad63d48c3adfae0d7af77f27027');
So what it give me : "echo getMD5Hash('1ca48ad63d48c3adfae0d7af77f27027'); "
的print_r($ hex_ary)=
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
)
回声$ TheMD5;
echo $TheMD5;
=> 418868f1137ea82935bbd235133993cf
的print_r($ Sec_hex_ary);
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
)
和回声给我:3333333333333633333333363633333333333636363333333333333333333636
And the echo give me : 3333333333333633333333363633333333333636363333333333333333333636
你认为这是正确的?感谢您的帮助。
do you think this is correct ? Thanks for the help.
推荐答案
- 在PHP的字节数组是字符串,没有必要用实际阵列
- 如果您的字符串已经是EN codeD的UTF-8,有没有必要等同于
Encoding.UTF8.GetBytes(输入)
线;如果不是,使用的iconv
李转换> - 在PHP中的
MD5
已返回一个十六进制字符串,没有必要从原始输出构建一个
- 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 usingiconv
- PHP's
md5
already returns a hex string, no need to build one from the raw output
总之,相当于C#功能的PHP大概是:
Altogether, the PHP equivalent to that C# function is probably:
echo md5($input);
说了这么多,有可能是关于C#避开了我在这里,可能会改变结果错综复杂。
Having said that, there may be intricacies about C# that elude me here that may change the result.
这篇关于净C#到PHP铅与byte []数组和放大器; ComputeHash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!