国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# MD5摘要算法、哈希算法

2019-11-17 02:34:58
字體:
來源:轉載
供稿:網友

C# md5摘要算法、哈希算法

MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于確保信息傳輸完整一致。是計算機廣泛使用的雜湊算法之一(又譯摘要算法、哈希算法)

MD5算法具有以下特點:

1、壓縮性:任意長度的數據,算出的MD5值長度都是固定的。

2、容易計算:從原數據計算出MD5值很容易。

3、抗修改性:對原數據進行任何改動,哪怕只修改1個字節,所得到的MD5值都有很大區別。

4、弱抗碰撞:已知原數據和其MD5值,想找到一個具有相同MD5值的數據(即偽造數據)是非常困難的。

5、強抗碰撞:想找到兩個不同的數據,使它們具有相同的MD5值,是非常困難的。

通過文件路徑得到文件MD5值

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

通過流路徑得到MD5值

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

測試發現:通過文件流去得到MD5值,改變了文件的后綴名是沒有區別的。

字符串MD5值

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

字符串MD5值對比

       // Verify a hash against a string. md5值不區分大小寫       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;            }        }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 资中县| 安义县| 金川县| 金山区| 延边| 淳安县| 寿宁县| 石首市| 扎囊县| 江川县| 康保县| 公主岭市| 广州市| 平阴县| 香港 | 射洪县| 左云县| 丹凤县| 夹江县| 仁布县| 清河县| 台山市| 砚山县| 平泉县| 桦甸市| 汉中市| 平果县| 金昌市| 韶关市| 乌鲁木齐市| 剑河县| 尤溪县| 驻马店市| 蕉岭县| 普宁市| 平湖市| 和静县| 长泰县| 阿拉善盟| 富川| 西乡县|