将 C# 自定义 getSHA512 函数转换为 Ruby
我想知道是否有人可以帮助我将此方法转换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 StackOverflow 上的所有代码片段一样,我始终假设使用的是最新版本的 Ruby。下面这个也适用于 Ruby 1.8:
请注意,在这种情况下,您必须知道并明确告诉 Ruby 字符串的编码。在 Ruby 1.9 中,Ruby 始终知道字符串的编码方式,并在需要时进行相应的转换。我选择 UTF-8 作为默认编码,因为它向后兼容 ASCII,是互联网上的标准编码,并且在其他方面也广泛使用。但是,例如 .NET 和 Java 都使用 UTF-16LE,而不是 UTF-8。如果您的字符串不是 UTF-8 或 ASCII 编码,则必须将编码名称传递到
sha512
方法中。题外话:9 行代码减少到 1 行。我喜欢 Ruby!
嗯,实际上这有点不公平。你可以这样写:
实际上只有 3 行(根据 StackOverflow 的布局分成 5 行),最重要的是摆脱了丑陋的 1950 年代风格的显式
for
循环,以获得漂亮的 1960 年代风格折叠(又名。reduce
又名。inject
又名。Aggregate
又名。inject:into:
…都是一样的)。可能有一种更优雅的方式来编写这个问题,但是 a) 我实际上不知道 C# 和 .NET,b) 这个问题是关于 Ruby 的。集中注意力,约尔格,集中注意力! :-)
Aaand … 找到了:
我知道在某个地方一定有一个与 Ruby 的
Enumerable#join
等价的东西,我只是找错了地方。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:
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:
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:
I knew there had to be an equivalent to Ruby's
Enumerable#join
somewhere, I just was looking in the wrong place.使用 Digest::SHA2班级。
Use the Digest::SHA2 class.