zoukankan      html  css  js  c++  java
  • 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;
                }
            }
  • 相关阅读:
    从路径中拆分出文件名和后缀
    屏幕中判断必输
    根据tcode查找增强的程序
    IDOC练习(二、接收端配置)
    ORACLE 绑定变量用法总结
    Oracle数据类型之number
    总结:整理 oracle异常错误处理
    ISNUMBER函数的创建以及函数创建思路。
    oracle 绑定变量 bind variable(2)
    oracle 绑定变量(bind variable)(1)
  • 原文地址:https://www.cnblogs.com/tongyi/p/4274092.html
Copyright © 2011-2022 走看看