生成 PHP UTF-16 SHA1 哈希以匹配 C# 方法
我正在尝试在 PHP5 中复制一些 C# 代码,但遇到了一些困难。
C# 代码如下,重要的是要注意它不能更改:
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 代码如下:
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 编码的字符串,而不是传入字符串实际使用的编码。
我想让它工作,但我无法通过更改 c# 代码来实现它(尽管毫无疑问这将是最简单的修复)。
请提供一些建议吗?
好的,我已经按照 Artefacto 的建议更改了代码,但它仍然无法按预期工作。
PHP 代码现在如下所示:
$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=
但是,从 C# 代码来看,该值是这样的:
VPf7BhT1ksAfWbzeJw35g+bVKwY=
I'm trying to replicate some C# code in PHP5 and am having some difficulties.
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));
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
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.
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).
Please can any advise on some ideas?
OK, I have altered the code following Artefacto's advice, and it still isn't working as expected.
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/>";
And the outputed value of this method is:
1/Y5MCzI8vDJqc456YIicpwoyy0=
However, from the C# code, the value is this:
VPf7BhT1ksAfWbzeJw35g+bVKwY=
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,试试这段代码:
因为你错过了一个
sha1
调用。现在更新
此代码应该可以工作:
Well, try this code:
Because you miss one
sha1
call.Update
Now this code should work:
UnicodeEncoding< 的 无参数构造函数 的文档/代码> 说这个:
现在,
mb_convert_encoding
假定“UTF-16”为“UTF-16BE”(大端)。它也不提供 BOM。因此,您必须这样做:正如 Paja 指出的那样,您还错过了对
sha1.
The docs for the no-arg constructor of
UnicodeEncoding
say this:Now,
mb_convert_encoding
assumes "UTF-16" as "UTF-16BE" (big-endian). It also does not provide a BOM. Therefore, you must do instead:As Paja pointed out, you're also missing a call to
sha1
.这段代码应该可以工作...
This code should work...