gpt4 book ai didi

c# - Ruby 中的 SHA256 Base64 哈希

转载 作者:行者123 更新时间:2023-12-05 09:18:55 24 4
gpt4 key购买 nike

我们正在尝试在 ruby​​ 中实现 SHA256 Base64 哈希,它不会像在 C# 中那样返回预期的结果。

下面是我们的 C# 示例代码。

    public static string HashSHA256ToBase64(string phrase)
{
if (phrase == null)
return null;
var encoder = new UTF8Encoding();
var sha256Hasher = new SHA256CryptoServiceProvider();
var hashedDataBytes = sha256Hasher.ComputeHash(encoder.GetBytes(phrase));

return Convert.ToBase64String(hashedDataBytes);
}

为此,我们需要用 ruby​​ 编写等效代码。为此,我们正在尝试如下。

Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), "", phrase))

它提供的结果与 C# 不同。

测试短语:V2dcZBpzzglD1ynW5ZAyFocs9wtpR624wlla9gujw0I=RquZ/QzazPM=

预期结果:utXwt733s9FmiSM69o2zGOm0IT42FjthbB0oquIuPak=

谁能帮我用等效的 ruby​​ 代码来解决这个问题

最佳答案

下面的代码解决了这个问题

Digest::SHA256.base64digest(phrase)

关于c# - Ruby 中的 SHA256 Base64 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42840023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com