ruby 和 C# 之间 SHA 哈希值的差异

发布于 2024-07-24 08:19:02 字数 970 浏览 10 评论 0原文

我正在开发一个使用一些 REST Web 服务的应用程序。 技术文档说我应该在请求中传递某些字符串的 SHA256 哈希值。

在示例请求(在文档中)中,有一个字符串:

hn-Rw2ZHYwllUYkklL5Zo_7lWJVkrbShZPb5CD1expires=1893013926label[0]=any/somestatistics=1d,2d,7d,28d,30d,31d,lifetimestatus=upl,livetitle=a

执行后:

digest = Digest::SHA256.digest(string_to_sign)
signature = Base64::encode64(digest).chomp.gsub(/=+$/, '')

结果为哈希:

YRYuN2zO+VvxISNp/vKQM5Cl6Dpzoin7mNES0IZJ06U

此示例使用 ruby​​ 语言,因为该文档适用于 ruby​​ 开发人员。

我正在用 C# 开发我的应用程序,对于完全相同的字符串,当我执行:

byte[] rawHash = sha256.ComputeHash(rawRequest, 0, rawRequest.Length);
string friendlyHash = Convert.ToBase64String(rawHash);

并删除尾随的“=”符号时,我得到:

Vw8pl/KxnjcEbyHtfNiMikXZdIunysFF2Ujsow8hyiw

,因此应用程序无法执行,导致签名不匹配错误。

我尝试在哈希之前将字符串转换为字节数组时更改编码,但没有任何改变。

有任何想法吗?

I'm developing an application, that makes use of some REST web services.
It's technical documentation says that I should pass SHA256 hash of some string in the request.

In an example request (in the documentation) a string:

hn-Rw2ZHYwllUYkklL5Zo_7lWJVkrbShZPb5CD1expires=1893013926label[0]=any/somestatistics=1d,2d,7d,28d,30d,31d,lifetimestatus=upl,livetitle=a

After executing:

digest = Digest::SHA256.digest(string_to_sign)
signature = Base64::encode64(digest).chomp.gsub(/=+$/, '')

results in a hash:

YRYuN2zO+VvxISNp/vKQM5Cl6Dpzoin7mNES0IZJ06U

This example is in ruby, as the documentation is for ruby developers.

I'm developing my application in C# and for the exactly same string, when I execute:

byte[] rawHash = sha256.ComputeHash(rawRequest, 0, rawRequest.Length);
string friendlyHash = Convert.ToBase64String(rawHash);

and remove the trailing "=" signs, I get:

Vw8pl/KxnjcEbyHtfNiMikXZdIunysFF2Ujsow8hyiw

and therefore, the application fails to execute resulting in an signature mismatch error.

I've tried changing the encoding while converting the string to a byte array preceding the hashing and nothing changed.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吻风 2024-07-31 08:19:02

根据文档此处,您缺少一个 - (即破折号)你的字符串。 似乎 Acrobat 有助于从文档中的复制粘贴中删除它...

这是我拼凑在一起的一些代码,它们获得与示例相同的值(如果您修剪了最后的 =)

    string s = "hn-Rw2ZH-YwllUYkklL5Zo_7lWJVkrbShZPb5CD1expires=1893013926label[0]=any/somestatistics=1d,2d,7d,28d,30d,31d,lifetimestatus=upl,livetitle=a";

    SHA256Managed sh = new SHA256Managed();
    byte[] request = System.Text.UTF8Encoding.UTF8.GetBytes(s);
    sh.Initialize();
    byte[] b4bbuff = sh.ComputeHash(request, 0, request.Length);

    string b64 = Convert.ToBase64String(b4bbuff);

Based on the document here, you are missing a - (that is a dash) in your string. Seems that Acrobat helpfully removes it in a copy paste from the document...

Here is some code that I splatted together that gets the same value as the example (well it would if you trimmed the final =)

    string s = "hn-Rw2ZH-YwllUYkklL5Zo_7lWJVkrbShZPb5CD1expires=1893013926label[0]=any/somestatistics=1d,2d,7d,28d,30d,31d,lifetimestatus=upl,livetitle=a";

    SHA256Managed sh = new SHA256Managed();
    byte[] request = System.Text.UTF8Encoding.UTF8.GetBytes(s);
    sh.Initialize();
    byte[] b4bbuff = sh.ComputeHash(request, 0, request.Length);

    string b64 = Convert.ToBase64String(b4bbuff);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文