C # achieve full application solution Hash

HASH file content based on the data obtained by the calculation by numerical logic, different files (even if the same file name) of the obtained HASH values ​​are different.

Hash

Through a certain hashing algorithm (typified MD5, SHA-1, etc.), the data is mapped to a longer period of short small data, this data is a hash value larger small data. His greatest feature is unique, once large data changes, even if it is a minor change, he will change the hash value. Similar to DNA, since it is DNA, it guarantees that no two data hash values ​​are identical.

Effect hash: the hash value, i.e., HASH value, contents of the file by a set of binary values ​​obtained encryption operation, the main purpose is for the file or verify the signature. Because of this characteristic, it is often used to determine whether two files are identical.

For example, downloading a file from the Internet, as long as the hash value of the hash value of the original file with the downloaded file comparison, if two files are identical, said exactly the same, there is no damage to the file download process. And if not, it indicates that the downloaded file with the original file different from the file has been corrupted during download. Hash is widely used, mainly used in:

  1. File checksum

We are more familiar checking algorithm parity and CRC check, check these two kinds of data and no ability to resist tampering, to some extent they are able to detect and correct channel errors in data transmission, but can not prevent malicious destruction of data.
"Digital fingerprint" MD5Hash algorithm features, making it one of the most widely used file integrity check and (Checksum) algorithm, many Unix systems provide computing md5checksum have command of.

  1. Uniquely identifies

    There are thousands of files, to give you a file, you want to find out if there is a hundred thousand files. A stupid way is to put each file out, and then follow the binary string one by comparison. But this It is bound to be relatively time-consuming operation. Files can be calculated using a hashing algorithm and then compare the hash values ​​are the same. Because of the presence of hash collision, you can then compare binary strings in files with the same hash value.

  2. digital signature

Hash algorithm is an important part of modern cryptographic systems. Since the operation speed asymmetric algorithms slower, so the digital signature of the agreement, a one-way hash function plays an important role. Hash value of, also known as "digital abstract" digital signatures, statistically can be considered to digitally sign documents themselves are equivalent. And such an agreement, there are other advantages.

  1. Hash table

Using a hash function in the hash table has been no stranger, and not repeat them.

  1. Load Balancing

For example, the addition of servers to a request to determine how the request should be routed to which router? Of course, we must ensure that the same request after the route to the same server. One way is to save a routing table relationships such as client and server IP number mapping, but if the client a lot of time is bound to look for very long. In this case, the unique identification information may be the client (eg: IP, username, etc.) hashed, and then modulo the number of the server, the server ID is obtained.

  1. Distributed Storage

When we have a lot of data, in order to increase the speed of read and write, generally choose to store data to a plurality of servers. We decided to store the file server to which it can operate hash algorithm modulo obtained.

However, if the data is more, to increase the server, the problem comes, for example, turned out to be 10 servers, has now become 15, then the hash value of the original document 16 is assigned to a number of server 6, is now No. 1 is assigned to a server, which means that all documents should be recalculated and re-hash value of non-paternity server for storage. Consistent hashing is this purpose.

Distributed Storage

 

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.

 

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:

1)对强行攻击的安全性:最显著和最重要的区别是SHA-1摘要比MD5摘要长32 位。使用强行技术,产生任何一个报文使其摘要等于给定报摘要的难度对MD5是2^128数量级的操作,而对SHA-1则是2^160数量级的操作。这样,SHA-1对强行攻击有更大的强度。

2)对密码分析的安全性:由于MD5的设计,易受密码分析的攻击,SHA-1显得不易受这样的攻击。

3)速度:在相同的硬件上,SHA-1的运行速度比MD5慢。

 

/// <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("-", "");
    }
}

 

[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)));
    }
}

Testing and output results

 

 


一路走来数个年头,感谢RDIFramework.NET框架的支持者与使用者,大家可以通过下面的地址了解详情。

RDIFramework.NET官方网站:http://www.rdiframework.net/

RDIFramework.NET官方博客:http://blog.rdiframework.net/

同时需要说明的,以后的所有技术文章以官方网站为准,欢迎大家收藏!

RDIFramework.NET框架由海南国思软件科技有限公司专业团队长期打造、一直在更新、一直在升级,请放心使用!

欢迎关注RDIFramework.net框架官方公众微信(微信号:guosisoft),及时了解最新动态。

扫描二维码立即关注

Micro Signal: guosisoft

Guess you like

Origin chinahuyong.iteye.com/blog/2443167