计算UTF-8编码字符串的MD5
有谁知道如何在 Ruby 中重现这个 C# 算法?
HashAlgorithm algorithm = MD5.Create();
Encoding encoding = new UTF8Encoding();
var sb = new StringBuilder();
foreach (var element in algorithm.ComputeHash(encoding.GetBytes(password)))
{
sb.Append(element.ToString("X2"));
}
return sb.ToString();
将密码转换为 UTF-8 后,它会计算密码的 MD5 哈希值。 哈希值表示为 32 个十六进制数字的序列,例如“E4D909C290D0FB1CA068FFADDF22CBD0”。
示例:
“übergeek”
→ “1049165D5C22F27B9545F6B3A0DB07E0”
“Гειασου”
→ “9B2C16CACFE1803F137374A7E96F083F”
Does anyone know how to reproduce this C# algorithm in Ruby?
HashAlgorithm algorithm = MD5.Create();
Encoding encoding = new UTF8Encoding();
var sb = new StringBuilder();
foreach (var element in algorithm.ComputeHash(encoding.GetBytes(password)))
{
sb.Append(element.ToString("X2"));
}
return sb.ToString();
It calculates the MD5 hash of the password after converting it to UTF-8.
The hash is represented as a sequence of 32 hexadecimal digits, e.g., "E4D909C290D0FB1CA068FFADDF22CBD0".
Examples:
"übergeek"
→ "1049165D5C22F27B9545F6B3A0DB07E0"
"Γεια σου"
→ "9B2C16CACFE1803F137374A7E96F083F"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您所需要的只是 Digest::MD5 ,它将生成您提供的任何字符串的十六进制摘要。虽然 Ruby 1.8 在 ISO-Latin1 和 UTF-8 字符集之间的区别有些微妙,但 Ruby 1.9 在这里提供了更多的控制,包括转换。但是,如果字符串以 UTF8 形式提供,Ruby 1.8 通常会保留它,将其视为简单的字节流。
Maybe all you need is Digest::MD5 which will produce hexdigests of any string you give it. While Ruby 1.8 is somewhat subtle in its distinction between ISO-Latin1 and UTF-8 character sets, Ruby 1.9 provides much more control here, including conversion. If the string is supplied as UTF8, though, Ruby 1.8 generally leaves it alone, treating it as a simple byte stream.
你需要这样的东西
You'd need something like this