生成 PHP UTF-16 SHA1 哈希以匹配 C# 方法

发布于 2024-09-14 21:15:19 字数 1534 浏览 3 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟─花易冷 2024-09-21 21:15:19

好吧,试试这段代码:

$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 调用。

现在更新

此代码应该可以工作:

$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 />";

Well, try this code:

$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

Because you miss one sha1 call.

Update

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 />";
眼中杀气 2024-09-21 21:15:19

UnicodeEncoding< 的 无参数构造函数 的文档/代码> 说这个:

此构造函数创建一个使用小端字节顺序的实例,提供 Unicode 字节顺序标记,并且在检测到无效编码时不会引发异常。

现在,mb_convert_encoding 假定“UTF-16”为“UTF-16BE”(大端)。它也不提供 BOM。因此,您必须这样做:

$utfString = "\xFF\xFE" . mb_convert_encoding($strToHash,"UTF-16LE");
/* ...*/
$encodedBase64Tag = "\xFF\xFE" . mb_convert_encoding($base64Tag."Space","UTF-16LE");

正如 Paja 指出的那样,您还错过了对 sha1.

The docs for the no-arg constructor of UnicodeEncoding say this:

This constructor creates an instance that uses the little endian byte order, provides a Unicode byte order mark, and does not throw an exception when an invalid encoding is detected.

Now, mb_convert_encoding assumes "UTF-16" as "UTF-16BE" (big-endian). It also does not provide a BOM. Therefore, you must do instead:

$utfString = "\xFF\xFE" . mb_convert_encoding($strToHash,"UTF-16LE");
/* ...*/
$encodedBase64Tag = "\xFF\xFE" . mb_convert_encoding($base64Tag."Space","UTF-16LE");

As Paja pointed out, you're also missing a call to sha1.

顾铮苏瑾 2024-09-21 21:15:19
  1. 这段代码应该可以工作...

    $utfString = mb_convert_encoding($strToHash,"UTF-16LE");
    $hashTag = sha1($utfString,true);
    $base64Tag = base64_encode($hashTag);
    回显 $base64Tag;
    
  1. This code should work...

    $utfString = mb_convert_encoding($strToHash,"UTF-16LE");
    $hashTag = sha1($utfString,true);
    $base64Tag = base64_encode($hashTag);
    echo $base64Tag;
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文