如何在C#中使用SHA1或MD5?(哪一个在性能和安全性方面更好)

时间:2009-11-18 13:58:40

标签: c# .net authentication md5 sha1

在C#中我们如何自动使用SHA1?是SHA1比MD5更好吗?(我们使用哈希来获取用户名和密码,需要速度进行身份验证)

7 个答案:

答案 0 :(得分:35)

不确定您的意思是自动,但您应该使用SHA256及更高版本。用你的哈希总是 use a Saltcode)。一个侧面说明,经过一段时间后,使用硬化哈希比使用基于速度的简单哈希函数要好得多。即:在几百次迭代中进行散列,或使用已经证实的散列函数,例如bcrypt(我相信下面会提到)。在.NET中使用SHA256哈希函数的代码示例如下:

byte[] data = new byte[DATA_SIZE];
byte[] result;

using(SHA256 shaM = new SHA256Managed()) {
    result = shaM.ComputeHash(data);
}

使用SHA256可以帮助您,可以在MSDN找到。


关于SHA1“破解”的旁注:Putting the cracking of SHA-1 in perspective

答案 1 :(得分:29)

SHA1比MD5强,所以如果您有选择,最好使用它。这是一个例子:

public static string CalculateSHA1(string text, Encoding enc)
{
    byte[] buffer = enc.GetBytes(text);
    SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
    return BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
}

答案 2 :(得分:11)

两者都太快而无法直接使用。使用Key Enhance来“减慢”密码散列程序。速度是不幸的密码安全的敌人。

慢到多慢?将密码哈希从〜微秒减慢到几百毫秒不会对应用程序的感知性能产生负面影响......但会使密码破解速度慢十几倍。

查看此文章了解详情: http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html

  

问题是MD5很快。它的现代竞争对手也是如此,例如SHA1和SHA256。速度是现代安全散列的设计目标,因为散列几乎是每个密码系统的构建块,并且通常在每个数据包或每个消息的基础上执行需求。

     

速度正是您在密码哈希函数中不想要的。

...剪辑...

  

密码攻击游戏会在破解密码X时得分。对于彩虹表,该时间取决于您的桌子需要多大以及搜索速度有多快。对于增量破解程序,时间取决于您运行密码哈希函数的速度。

那就是说,使用BCrypt。最近开发了SCrypt,但我怀疑是否存在任何稳定(或生产就绪)库。从理论上讲,SCrypt声称要改进BCrypt。不推荐使用“构建自己的”,但是数千次迭代MD5 / SHA1 / SHA256应该可以解决问题(即:Key Enhance)。

如果您不了解它们,请务必阅读Rainbow Tables。基本安全的东西。

答案 3 :(得分:10)

来自MSDN

byte[] data = new byte[DATA_SIZE];
byte[] result; 

SHA1 sha = new SHA1CryptoServiceProvider(); 
// This is one implementation of the abstract class SHA1.
result = sha.ComputeHash(data);

答案 4 :(得分:1)

使用SHA1或SHA2 MD5算法存在问题。

http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5%28v=vs.85%29.aspx

答案 5 :(得分:1)

我想用这些东西。

MD5,SHA1 / 256/384/512,带有可选的编码参数。

Othere HashAlgorithms。感谢Darin Dimitrov。

public static string MD5Of(string text)
{
    return MD5Of(text, Encoding.Default);
}
public static string MD5Of(string text, Encoding enc)
{
    return HashOf<MD5CryptoServiceProvider>(text, enc);
}
public static string SHA1Of(string text)
{
    return SHA1Of(text, Encoding.Default);
}
public static string SHA1Of(string text, Encoding enc)
{
    return HashOf<SHA1CryptoServiceProvider>(text, enc);
}

public static string SHA384Of(string text)
{
    return SHA384Of(text, Encoding.Default);
}
public static string SHA384Of(string text, Encoding enc)
{
    return HashOf<SHA384CryptoServiceProvider>(text, enc);
}

public static string SHA512Of(string text)
{
    return SHA512Of(text, Encoding.Default);
}
public static string SHA512Of(string text, Encoding enc)
{
    return HashOf<SHA512CryptoServiceProvider>(text, enc);
}

public static string SHA256Of(string text)
{
    return SHA256Of(text, Encoding.Default);
}
public static string SHA256Of(string text, Encoding enc)
{
    return HashOf<SHA256CryptoServiceProvider>(text, enc);
}

public static string HashOf<TP>(string text, Encoding enc)
    where TP: HashAlgorithm, new()
{
    var buffer = enc.GetBytes(text);
    var provider = new TP();
    return BitConverter.ToString(provider.ComputeHash(buffer)).Replace("-", "");
}

答案 6 :(得分:0)

MD5性能更好,SHA1更安全。你可以从这个比较中得到一个想法

enter image description here

相关问题