Hash string in c#

I have a problem when trying get a hash string in c#.

I already tried a few websites, but most of them are using files to get the hash. Others that are for strings are a bit too complex. I found examples for Windows authentication for web like this:

FormsAuthentication.HashPasswordForStoringInConfigFile(tbxPassword.Text.Trim(), "md5")

I need to use a hash to make a string that contains a filename more secure. How can I do that?

Example:

string file  = "username";
string hash = ??????(username);

Should I use another hashing algorithm and not "md5"?

232563 次浏览

我觉得你要找的不是散列,而是加密。使用散列,您将无法从“ hash”变量检索原始文件名。你可以加密,而且很安全。

有关.NET 中加密的更多信息,请参见 用 VB.NET 实现 ASP.NET 中的 AES

I don't really understand the full scope of your question, but if all you need is a hash of the string, then it's very easy to get that.

只需使用 GetHashCode 方法。

像这样:

string hash = username.GetHashCode();
using System.Security.Cryptography;


public static byte[] GetHash(string inputString)
{
using (HashAlgorithm algorithm = SHA256.Create())
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}


public static string GetHashString(string inputString)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in GetHash(inputString))
sb.Append(b.ToString("X2"));


return sb.ToString();
}

Additional Notes

  • 由于 MD5和 SHA1是 过时又没有安全感算法,因此此解决方案使用 SHA256。或者,您可以使用注释中指出的 地穴密码
  • 另外,考虑“ 腌制”您的散列和使用经过验证的加密算法,正如在注释中指出的那样。

获取用于密码存储的哈希字符串的最快方法是以下代码:

    internal static string GetStringSha256Hash(string text)
{
if (String.IsNullOrEmpty(text))
return String.Empty;


using (var sha = new System.Security.Cryptography.SHA256Managed())
{
byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
byte[] hash = sha.ComputeHash(textData);
return BitConverter.ToString(hash).Replace("-", String.Empty);
}
}

备注:

  • 如果经常调用该方法,则应将 sha变量的创建重构为类字段;
  • 输出为编码的十六进制字符串;
//Secure & Encrypte Data
public static string HashSHA1(string value)
{
var sha1 = SHA1.Create();
var inputBytes = Encoding.ASCII.GetBytes(value);
var hash = sha1.ComputeHash(inputBytes);
var sb = new StringBuilder();
for (var i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}

如果性能不是主要问题,也可以使用以下任何一种方法:
(如果希望散列字符串大写,请将 "x2"替换为 "X2"。)

public static string SHA256ToString(string s)
{
using (var alg = SHA256.Create())
return string.Join(null, alg.ComputeHash(Encoding.UTF8.GetBytes(s)).Select(x => x.ToString("x2")));
}

或:

public static string SHA256ToString(string s)
{
using (var alg = SHA256.Create())
return alg.ComputeHash(Encoding.UTF8.GetBytes(s)).Aggregate(new StringBuilder(), (sb, x) => sb.Append(x.ToString("x2"))).ToString();
}

The shortest and fastest way ever. Only 1 line!

    public static string StringSha256Hash(string text) =>
string.IsNullOrEmpty(text) ? string.Empty : BitConverter.ToString(new System.Security.Cryptography.SHA256Managed().ComputeHash(System.Text.Encoding.UTF8.GetBytes(text))).Replace("-", string.Empty);

这里的所有哈希代码示例都过时了。在.NET 5中,提供了一种新的散列数据的方法,这种方法速度快一倍,内存分配为零。很酷吧?您只需要在您喜欢的散列算法类上使用新的静态 HashData(byte[])

byte[] buffer = Encoding.UTF8.GetBytes(input);
byte[] digest = SHA256.HashData(buffer);

微软有一个 分析仪中的新规则来检测遗留使用并用新的 API 替换它们。