在 C# 中使用 SHA1 算法进行散列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17292366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hashing with SHA1 Algorithm in C#
提问by Merve Kaya
I want to hash given byte[]array with using SHA1Algorithm with the use of SHA1Managed.
The byte[]hash will come from unit test.
Expected hash is 0d71ee4472658cd5874c5578410a9d8611fc9aef(case sensitive).
我想byte[]使用SHA1Algorithm 和SHA1Managed.
该byte[]散列将来自单元测试。
预期的哈希是0d71ee4472658cd5874c5578410a9d8611fc9aef(区分大小写)。
How can I achieve this?
我怎样才能做到这一点?
public string Hash(byte [] temp)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
}
}
回答by Grant Thomas
You can "compute the value for the specified byte array" using ComputeHash:
您可以使用以下命令“计算指定字节数组的值” ComputeHash:
var hash = sha1.ComputeHash(temp);
If you want to analyse the result in string representation, then you will need to format the bytes using the {0:X2}format specifier.
如果要分析字符串表示形式的结果,则需要使用{0:X2}格式说明符格式化字节。
回答by John Gathogo
public string Hash(byte [] temp)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(temp);
return Convert.ToBase64String(hash);
}
}
EDIT:
编辑:
You could also specify the encoding when converting the byte array to string as follows:
您还可以在将字节数组转换为字符串时指定编码,如下所示:
return System.Text.Encoding.UTF8.GetString(hash);
or
或者
return System.Text.Encoding.Unicode.GetString(hash);
回答by Mitch
For those who want a "standard" text formatting of the hash, you can use something like the following:
对于那些想要散列的“标准”文本格式的人,您可以使用以下内容:
static string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
// can be "x2" if you want lowercase
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
}
This will produce a hash like 0C2E99D0949684278C30B9369B82638E1CEAD415.
这将产生一个像0C2E99D0949684278C30B9369B82638E1CEAD415.
Or for a code golfed version:
或者对于代码高尔夫版本:
static string Hash(string input)
{
var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input));
return string.Concat(hash.Select(b => b.ToString("x2")));
}
回答by oPless
I'll throw my hat in here:
我会把我的帽子扔在这里:
(as part of a static class, as this snippet is two extensions)
(作为静态类的一部分,因为这个片段是两个扩展)
//hex encoding of the hash, in uppercase.
public static string Sha1Hash (this string str)
{
byte[] data = UTF8Encoding.UTF8.GetBytes (str);
data = data.Sha1Hash ();
return BitConverter.ToString (data).Replace ("-", "");
}
// Do the actual hashing
public static byte[] Sha1Hash (this byte[] data)
{
using (SHA1Managed sha1 = new SHA1Managed ()) {
return sha1.ComputeHash (data);
}
回答by sky-dev
This is what I went with. For those of you who want to optimize, check out https://stackoverflow.com/a/624379/991863.
这就是我一起去的。对于那些想要优化的人,请查看https://stackoverflow.com/a/624379/991863。
public static string Hash(string stringToHash)
{
using (var sha1 = new SHA1Managed())
{
return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(stringToHash)));
}
}
回答by alireza amini
Fastest way is this :
最快的方法是这样的:
public static string GetHash(string input)
{
return string.Join("", (new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input))).Select(x => x.ToString("X2")).ToArray());
}
For Small character output use x2in replace of of X2
对于小字符输出使用x2代替X2

