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

2021-01-25 18:14

阅读:499

标签:names   ext   oid   mode   file   read   line   build   ons   

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 )
                        {
                            string sortedByte = md5Bytes[i].ToString("x2");
                            if (!string.IsNullOrEmpty(sortedByte))
                            {
                                md5Builder.Append(sortedByte);
                            }
                        }
                    }                      
                }
            }
            return md5Builder.ToString();
        }
    }
}

 

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

标签:names   ext   oid   mode   file   read   line   build   ons   

原文地址:https://www.cnblogs.com/Fred1987/p/12009960.html


评论


亲,登录后才可以留言!