生成PHP UTF-16 SHA1哈希以匹配C#方法 [英] Generate a PHP UTF-16 SHA1 hash to match C# method

查看:57
本文介绍了生成PHP UTF-16 SHA1哈希以匹配C#方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PHP5中复制一些C#代码,并且遇到了一些困难.

I'm trying to replicate some C# code in PHP5 and am having some difficulties.

C#代码如下,需要注意的是它不能更改:

The C# code is as following, and it is important to note that it cannot be changed:

string s = strToHash;
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] bytes = encoding.GetBytes(s);
SHA1Managed managed = new SHA1Managed();     
bytes = encoding.GetBytes(Convert.ToBase64String(managed.ComputeHash(bytes)) + "Space");
return Convert.ToBase64String(managed.ComputeHash(bytes));

我编写的用于复制此代码的PHP代码如下:

The PHP code I've written to replicate this is as follows:

utfString = mb_convert_encoding($strToHash,"UTF-16");
hashTag = sha1($utfString,true); 
base64Tag = base64_encode($hashTag); 
encodedBase64Tag = mb_convert_encoding($base64Tag."Space","UTF-16");
base64EncodedAgain = base64_encode($encodedBase64Tag);

echo $base64EncodedAgain

但是,两个输出不匹配.我相信这是因为PHP中的SHA1方法适用于ASCII编码的字符串,而不是传入的字符串实际使用的编码.

However, the two outputs don't match up. I believe this is because the SHA1 method in PHP works on ASCII encoded strings, not the encoding actually used by the passed in string.

我想使它正常工作,并且我无法通过更改c#代码来实现它(尽管毫无疑问,这将是最简单的解决方法).

I would like to get this to work, and I can't achieve it by altering the c# code (although no-doubt that would be the easiest fix).

请问一些建议吗?

好的,我已按照Artefacto的建议更改了代码,但仍无法按预期工作.

OK, I have altered the code following Artefacto's advice, and it still isn't working as expected.

PHP代码现在看起来像这样:

The PHP Code now looks like this:

$utfString = "\xFF\xFE".mb_convert_encoding($strToHash,"UTF-16LE");
$hashTag = sha1($utfString,true);       
$base64Tag = base64_encode($hashTag);   
$encodedBase64Tag = "\xFF\xFE".mb_convert_encoding($base64Tag."Space","UTF-16LE");  
$hashedAgain = sha1($encodedBase64Tag,true);
$base64EncodedAgain = base64_encode($hashedAgain);

echo $base64EncodedAgain."<Br/>";

此方法的输出值为:

1/Y5MCzI8vDJqc456YIicpwoyy0 =

1/Y5MCzI8vDJqc456YIicpwoyy0=

但是,在C#代码中,值是这样的:

However, from the C# code, the value is this:

VPf7BhT1ksAfWbzeJw35g + bVKwY =

VPf7BhT1ksAfWbzeJw35g+bVKwY=

推荐答案

好,尝试以下代码:

$utfString = mb_convert_encoding($strToHash,"UTF-16");
$hashTag = sha1($utfString,true); 
$base64Tag = base64_encode($hashTag); 
$encodedBase64Tag = mb_convert_encoding($base64Tag."Space","UTF-16");
$base64EncodedAgain = base64_encode(sha1($encodedBase64Tag, true));

echo $base64EncodedAgain

因为您错过了一个 sha1 通话.

Because you miss one sha1 call.

现在此代码应该可以工作:

Now this code should work:

$utfString = mb_convert_encoding($strToHash,"UTF-16LE");
$hashTag = sha1($utfString,true);       
$base64Tag = base64_encode($hashTag);   
$encodedBase64Tag = mb_convert_encoding($base64Tag."Space","UTF-16LE");  
$hashedAgain = sha1($encodedBase64Tag,true);
$base64EncodedAgain = base64_encode($hashedAgain);
echo $base64EncodedAgain . "<br />";

这篇关于生成PHP UTF-16 SHA1哈希以匹配C#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆