将 C# 自定义 getSHA512 函数转换为 Ruby

发布于 2024-09-07 05:27:52 字数 1076 浏览 5 评论 0原文

我想知道是否有人可以帮助我将此方法转换为 ruby​​,这可能吗?

public static string getSHA512(string str){
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] HashValue = null;
    byte[] MessageBytes = UE.GetBytes(str);
    System.Security.Cryptography.SHA512Managed SHhash = new System.Security.Cryptography.SHA512Managed();
    string strHex = "";
    HashValue = SHhash.ComputeHash(MessageBytes);
    foreach (byte b in HashValue){
        strHex += string.Format("{0:x2}", b);
    }
    return strHex;
}

提前致谢


更新:

我只是想澄清一下,不幸的是,它的方法不仅适用于 SHA512 生成,而且是一种自定义方法。我相信 Digest::SHA512.hexdigest 只是 SHHast 实例,但如果您仔细查找该方法,您会发现它与简单的哈希生成有点不同。 遵循两个函数的结果。

# in C#
getSHA512("hello") => "5165d592a6afe59f80d07436e35bd513b3055429916400a16c1adfa499c5a8ce03a370acdd4dc787d04350473bea71ea8345748578fc63ac91f8f95b6c140b93"

# in Ruby
Digest::SHA512.hexdigest("hello") || Digest::SHA2 => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043" 

I was wondering if someone could help me to get this method converted to ruby, is this possible at all?

public static string getSHA512(string str){
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] HashValue = null;
    byte[] MessageBytes = UE.GetBytes(str);
    System.Security.Cryptography.SHA512Managed SHhash = new System.Security.Cryptography.SHA512Managed();
    string strHex = "";
    HashValue = SHhash.ComputeHash(MessageBytes);
    foreach (byte b in HashValue){
        strHex += string.Format("{0:x2}", b);
    }
    return strHex;
}

Thanks in advance


UPDATE:

I just would like to make it clear that unfortunately it's method is not just for SHA512 generation but a custom one. I believe that the Digest::SHA512.hexdigest would be just the SHHast instance, but if you carefully look for the method you can see that it differs a bit from a simple hash generation.
Follows the result of both functions.

# in C#
getSHA512("hello") => "5165d592a6afe59f80d07436e35bd513b3055429916400a16c1adfa499c5a8ce03a370acdd4dc787d04350473bea71ea8345748578fc63ac91f8f95b6c140b93"

# in Ruby
Digest::SHA512.hexdigest("hello") || Digest::SHA2 => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043" 

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

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

发布评论

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

评论(2

泪眸﹌ 2024-09-14 05:27:52
require 'digest/sha2'

class String
  def sha512
    Digest::SHA2.new(512).hexdigest(encode('UTF-16LE'))
  end
end

'hello'.sha512 # => '5165d592a6afe59f80d07436e35bd…5748578fc63ac91f8f95b6c140b93'

与 StackOverflow 上的所有代码片段一样,我始终假设使用的是最新版本的 Ruby。下面这个也适用于 Ruby 1.8:

require 'iconv'
require 'digest/sha2'

class String
  def sha512(src_enc='UTF-8')
    Digest::SHA2.new(512).hexdigest(Iconv.conv(src_enc, 'UTF-16LE', self))
  end
end

'hello'.sha512 # => '5165d592a6afe59f80d07436e35bd…5748578fc63ac91f8f95b6c140b93'

请注意,在这种情况下,您必须知道并明确告诉 Ruby 字符串的编码。在 Ruby 1.9 中,Ruby 始终知道字符串的编码方式,并在需要时进行相应的转换。我选择 UTF-8 作为默认编码,因为它向后兼容 ASCII,是互联网上的标准编码,并且在其他方​​面也广泛使用。但是,例如 .NET 和 Java 都使用 UTF-16LE,而不是 UTF-8。如果您的字符串不是 UTF-8 或 ASCII 编码,则必须将编码名称传递到 sha512 方法中。


题外话:9 行代码减少到 1 行。我喜欢 Ruby!

嗯,实际上这有点不公平。你可以这样写:

var messageBytes = new UnicodeEncoding().GetBytes(str);
var hashValue = new System.Security.Cryptography.SHA512Managed().
    ComputeHash(messageBytes);
return hashValue.Aggregate<byte, string>("",
    (s, b) => s += string.Format("{0:x2}", b)
);

实际上只有 3 行(根据 StackOverflow 的布局分成 5 行),最重要的是摆脱了丑陋的 1950 年代风格的显式 for 循环,以获得漂亮的 1960 年代风格折叠(又名。reduce又名。inject又名。Aggregate又名。inject:into:…都是一样的)。

可能有一种更优雅的方式来编写这个问题,但是 a) 我实际上不知道 C# 和 .NET,b) 这个问题是关于 Ruby 的。集中注意力,约尔格,集中注意力! :-)

Aaand … 找到了:

return string.Join("", from b in hashValue select string.Format("{0:x2}", b));

知道在某个地方一定有一个与 Ruby 的 Enumerable#join 等价的东西,我只是找错了地方。

require 'digest/sha2'

class String
  def sha512
    Digest::SHA2.new(512).hexdigest(encode('UTF-16LE'))
  end
end

'hello'.sha512 # => '5165d592a6afe59f80d07436e35bd…5748578fc63ac91f8f95b6c140b93'

As with all my code snippets on StackOverflow, I always assume the latest version of Ruby. Here's one that also works with Ruby 1.8:

require 'iconv'
require 'digest/sha2'

class String
  def sha512(src_enc='UTF-8')
    Digest::SHA2.new(512).hexdigest(Iconv.conv(src_enc, 'UTF-16LE', self))
  end
end

'hello'.sha512 # => '5165d592a6afe59f80d07436e35bd…5748578fc63ac91f8f95b6c140b93'

Note that in this case, you have to know and tell Ruby about the encoding the string is in explicitly. In Ruby 1.9, Ruby always knows what encoding a string is in, and will convert it accordingly, when required. I chose UTF-8 as default encoding because it is backwards-compatible with ASCII, is the standard encoding on the internet and also otherwise widely used. However, for example both .NET and Java use UTF-16LE, not UTF-8. If your string is not UTF-8 or ASCII-encoded, you will have to pass in the encoding name into the sha512 method.


Off-Topic: 9 lines of code reduced to 1. I love Ruby!

Well, actually that is a little bit unfair. You could have written it something like this:

var messageBytes = new UnicodeEncoding().GetBytes(str);
var hashValue = new System.Security.Cryptography.SHA512Managed().
    ComputeHash(messageBytes);
return hashValue.Aggregate<byte, string>("",
    (s, b) => s += string.Format("{0:x2}", b)
);

Which is really only 3 lines (broken into 5 for StackOverflow's layout) and most importantly gets rid of that ugly 1950s-style explicit for loop for a nice 1960s-style fold (aka. reduce aka. inject aka. Aggregate aka. inject:into: … it's all the same).

There's probably an even more elegant way to write this, but a) I don't actually know C# and .NET and b) this question is about Ruby. Focus, Jörg, focus! :-)

Aaand … found it:

return string.Join("", from b in hashValue select string.Format("{0:x2}", b));

I knew there had to be an equivalent to Ruby's Enumerable#join somewhere, I just was looking in the wrong place.

花海 2024-09-14 05:27:52

使用 Digest::SHA2班级。

Use the Digest::SHA2 class.

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