C # MD5 Digest algorithm and hash algorithm,
MD5Message-Digest Algorithm 5 (Information-Digest Algorithm 5) is used to ensure the integrity and consistency of information transmission. It is one of the widely used Hash Algorithms in computers (also translated digest algorithms and hash algorithms)
The MD5 algorithm has the following features:
1. Compression: Data of any length, the calculated MD5 value length is fixed.
2. Easy Calculation: it is easy to calculate the MD5 value from the original data.
3. Anti-modification: Any modification to the original data, even if only one byte is modified, the MD5 value obtained is very different.
4. Weak anti-collision: it is very difficult to find a data with the same MD5 value (that is, forged data) because the original data and its MD5 value are known.
5. Strong anti-collision: it is very difficult to find two different data so that they have the same MD5 value.
Obtain the file MD5 value through the file path
public static string GetMD5HashFromFile(string fileName) { try { FileStream file = new FileStream(fileName, FileMode.Open); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } catch (Exception ex) { throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message); } }
Obtain the MD5 value through the stream path
public static string GetMd5Hash(Stream input) { MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); byte[] data = md5Hasher.ComputeHash(input); input.Seek(0, SeekOrigin.Begin); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); }
Test results show that the MD5 value is obtained through the file stream, and there is no difference in changing the file suffix.
String MD5 Value
public static string GetMd5Hash(string input) { // Create a new instance of the MD5CryptoServiceProvider object. MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); }
String MD5 Value Comparison
// Verify a hash against a string. the md5 value is case insensitive public static bool VerifyMd5Hash (string input, string hash) {// Hash the input. string hashOfInput = getMd5Hash (input); // Create a StringComparer an compare the hashes. stringComparer comparer = StringComparer. ordinalIgnoreCase; if (0 = comparer. compare (hashOfInput, hash) {return true;} else {return false ;}}