C#比较MD5编码

纯粹为了记录,方便以后拷贝

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CompareBytesMd5
{
    class Program
    {
        public static string CalculateMD5(string filePath)
        {
            try
            {
                var stream = File.OpenRead(filePath);
                var res = CalculateMD5(stream);
                stream.Close();
                return res;
            }
            catch (Exception ex)
            {
                Console.WriteLine("xxxxxxxxxxxxxxxxx Error");
            }
            return "";
        }
        public static string CalculateMD5(Stream stream)
        {
            using (var md5Obj = System.Security.Cryptography.MD5.Create())
            {
                StreamReader sr = new StreamReader(stream, Encoding.UTF8);
                string s = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                s = s.Replace("\r\n", "\n");
                byte[] bytes = Encoding.UTF8.GetBytes(s);
                var hash = md5Obj.ComputeHash(bytes);
                var strMD5 = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
                return strMD5;
            }
        }

        static void Main(string[] args)
        {
            string srcPath = "E:/Workspace/ro/config/Table/ServerBytes";
            string dstPath = "E:/Workspace/ro/config/Assets/Editor/Table";
            // string dstPath = "E:/Workspace/ro/config/Assets/Resources/Table";
            var srcFiles = Directory.GetFiles(srcPath, "*.bytes");
            var dstFiles = Directory.GetFiles(dstPath, "*.bytes");

            Dictionary<string, string> fileName2Md54Src = new Dictionary<string, string>();
            Dictionary<string, string> fileName2Md54Dst = new Dictionary<string, string>();

            foreach (var file in srcFiles)
            {
                if (!file.EndsWith(".bytes")) continue;
                var pureName = Path.GetFileNameWithoutExtension(file);
                var md5 = CalculateMD5(file);
                fileName2Md54Src.Add(pureName, md5);
            }

            foreach (var file in dstFiles)
            {
                if (!file.EndsWith(".bytes")) continue;
                var pureName = Path.GetFileNameWithoutExtension(file);
                var md5 = CalculateMD5(file);
                fileName2Md54Dst.Add(pureName, md5);
            }


            List<string> notExistItems = new List<string>();
            List<string> notMatchItems = new List<string>();
            foreach (var pairItem in fileName2Md54Src)
            {
                if (!fileName2Md54Dst.ContainsKey(pairItem.Key))
                {
                    notExistItems.Add(pairItem.Key);
                    continue;
                }

                if (pairItem.Value != fileName2Md54Dst[pairItem.Key])
                {
                    notMatchItems.Add(pairItem.Key);
                }
            }

            //if (notExistItems.Count > 0)
            //{
            //    Console.WriteLine("xxxxxxxxxxxxxxxxxx notExistItems:");
            //    foreach (var item in notExistItems)
            //    {
            //        Console.WriteLine(item);
            //    }
            //}

            if (notMatchItems.Count > 0)
            {
                Console.WriteLine("xxxxxxxxxxxxxxxxxx notMatchItems:");
                foreach (var item in notMatchItems)
                {
                    Console.WriteLine($"fileName:{item} srcMd5:{fileName2Md54Src[item]}, dstMd5:{fileName2Md54Dst[item]}");
                }
            }


            Console.WriteLine("Hello");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值