Hash implemented in C # Application

In C #, Hash data to the MD5 or SHA1 manner, MD5 and SHA1 are Hash algorithm, MD5 is a 128-bit output, the output is a 160-bit SHA1, MD5 SHA1 faster than, higher strength than MD5 SHA1.

1.1 SHA-1 and MD5 comparison of

Because both derived by MD4, SHA-1 and MD5 is very similar to each other. Accordingly, their strength and other properties are also similar, but there are different following:

  • Security force attack: the most significant and most important difference is that SHA-1 digest MD5 digest longer than 32. Technology using force, a packet so that any given message digest equal difficulty MD5 digest of the order of 2 ^ 128 operation, while the SHA-1 is the order of 2 ^ 160 operations. Thus, SHA-1 with a pair of force attack greater strength.
  • Safety of cryptanalysis: Due to the design of MD5 vulnerable to cryptanalysis attacks, SHA-1 appear to be less vulnerable to such attacks.
  • Speed: on the same hardware, SHA-1 operation speed slower than MD5.
1.2 SHA-1 and MD5 is implemented in C #
/// <summary>
/// Hash辅助类
/// </summary>
public class HashHelper
{
    /// <summary>
    /// 计算文件的 MD5 值
    /// </summary>
    /// <param name="fileName">要计算 MD5 值的文件名和路径</param>
    /// <returns>MD5 值16进制字符串</returns>
    public static string MD5File(string fileName)
    {
        return HashFile(fileName, "md5");
    }

    /// <summary>
    /// 计算文件的 sha1 值
    /// </summary>
    /// <param name="fileName">要计算 sha1 值的文件名和路径</param>
    /// <returns>sha1 值16进制字符串</returns>
    public static string SHA1File(string fileName)
    {
        return HashFile(fileName, "sha1");
    }

    /// <summary>
    /// 计算文件的哈希值
    /// </summary>
    /// <param name="fileName">要计算哈希值的文件名和路径</param>
    /// <param name="algName">算法:sha1,md5</param>
    /// <returns>哈希值16进制字符串</returns>
    private static string HashFile(string fileName, string algName)
    {
        if (!System.IO.File.Exists(fileName))
        {
            return string.Empty;
        }

        System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        byte[] hashBytes = HashData(fs, algName);
        fs.Close();
        return ByteArrayToHexString(hashBytes);
    }

    /// <summary>
    /// 计算哈希值
    /// </summary>
    /// <param name="stream">要计算哈希值的 Stream</param>
    /// <param name="algName">算法:sha1,md5</param>
    /// <returns>哈希值字节数组</returns>
    private static byte[] HashData(System.IO.Stream stream, string algName)
    {
        System.Security.Cryptography.HashAlgorithm algorithm;
        if (algName == null)
        {
            throw new ArgumentNullException("algName 不能为 null");
        }

        if (string.Compare(algName, "sha1", true) == 0)
        {
            algorithm = System.Security.Cryptography.SHA1.Create();
        }
        else
        {
            if (string.Compare(algName, "md5", true) != 0)
            {
                throw new Exception("algName 只能使用 sha1 或 md5");
            }
            algorithm = System.Security.Cryptography.MD5.Create();
        }

        return algorithm.ComputeHash(stream);
    }

    /// <summary>
    /// 字节数组转换为16进制表示的字符串
    /// </summary>
    private static string ByteArrayToHexString(byte[] buf)
    {
        return BitConverter.ToString(buf).Replace("-", "");
    }
}
1.3SHA-1 and MD5 implemented in C # test cases
[TestClass]
public class HashHelperUnitTest
{
    [TestMethod]
    public void TestMethod1()
    {
        string fileName = @"D:\TempTest\RDIFramework.BizLogic.dll";
        Assert.AreEqual(0, 0);

        //01.计算文件的 MD5 值
        Console.WriteLine(string.Format("计算文件的 MD5 值:{0}", HashHelper.MD5File(fileName)));

        //02.计算文件的 sha1 值
        Console.WriteLine(string.Format("计算文件的 sha1 值:{0}", HashHelper.SHA1File(fileName)));
    }
}

Guess you like

Origin www.cnblogs.com/kadycui/p/11428827.html