C# get md5,renamed file and can not change file’s md5

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace ConsoleApplication13
{
    class Program
    {
        static void Main(string[] args)
        {
            string imgPath1 = @"..\..\Images\lj.jpg";
            string imgPath2 = @"..\..\Images\lj2.jpg";
            string imgPath3 = @"..\..\Images\lj3.jpg";
            string imgPath4 = @"..\..\Images\lj4.jpg";
            string md51 = GetMD5(imgPath1);
            string md52 = GetMD5(imgPath2);
            string md53 = GetMD5(imgPath3);
            string md54 = GetMD5(imgPath4);
            Console.WriteLine($"path:{imgPath1},md51:{md51}");
            Console.WriteLine($"path:{imgPath2},md52:{md52}");
            Console.WriteLine($"path:{imgPath3},md53:{md53}");
            Console.WriteLine($"path:{imgPath4},md54:{md54}");
            Console.ReadLine();
        }

        static string GetMD5(string sourceFile)
        {
            StringBuilder md5Builder = new StringBuilder();
            if (File.Exists(sourceFile))
            {
                using (MD5 md5Hash = MD5.Create())
                {
                    using(FileStream fs=File.Open(sourceFile,FileMode.Open))
                    {
                        byte[] md5Bytes = md5Hash.ComputeHash(fs);
                        for (int i = 0; i < md5Bytes.Length; i++)
                        {
                            string sortedByte = md5Bytes[i].ToString("x2");
                            if (!string.IsNullOrEmpty(sortedByte))
                            {
                                md5Builder.Append(sortedByte);
                            }
                        }
                    }                      
                }
            }
            return md5Builder.ToString();
        }
    }
}

 

原文链接: https://www.cnblogs.com/Fred1987/p/12009960.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C# get md5,renamed file and can not change file's md5

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/402083

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:35
下一篇 2023年4月19日 上午9:35

相关推荐