国产探花免费观看_亚洲丰满少妇自慰呻吟_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;            }        }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 惠水县| 介休市| 上林县| 怀柔区| 甘肃省| 海晏县| 揭西县| 云南省| 成安县| 克山县| 土默特右旗| 温宿县| 新竹市| 南华县| 邛崃市| 内乡县| 盐城市| 克山县| 安泽县| 渭南市| 阿荣旗| 平舆县| 阜阳市| 惠东县| 崇礼县| 镇远县| 故城县| 城固县| 米泉市| 彰化县| 民乐县| 大同县| 布拖县| 平果县| 盐城市| 师宗县| 长汀县| 仪征市| 定南县| 刚察县| 秦安县|