1 using System; 2 using System.Text; 3 using System.IO; 4 using System.Security.Cryptography; 5 6 static void Main(string[] args) 7 { 8 CompareFileGetBytes("lyf.txt"); 9 Console.ReadLine(); 10 } 11 12 static void CompareFileGetBytes(string fileFullName) 13 { 14 byte[] fileReadAllBytes = File.ReadAllBytes(fileFullName); 15 string fileReadAllBytesMd5 = GetBytesMd5(fileReadAllBytes); 16 17 string utf8Md5 = string.Empty; 18 using (StreamReader reader = new StreamReader(fileFullName)) 19 { 20 string textResult = reader.ReadToEnd(); 21 byte[] utf8Bytes = Encoding.UTF8.GetBytes(textResult); 22 utf8Md5 = GetBytesMd5(utf8Bytes); 23 } 24 25 string utf32Md5 = string.Empty; 26 using (StreamReader utf32Reader = new StreamReader(fileFullName)) 27 { 28 string textResult = utf32Reader.ReadToEnd(); 29 byte[] utf32Bytes = Encoding.UTF32.GetBytes(textResult); 30 utf32Md5 = GetBytesMd5(utf32Bytes); 31 } 32 33 34 Console.WriteLine($"fileReadAllBytesMd5:{fileReadAllBytesMd5},utf8Md5:{utf8Md5}"); 35 36 if (string.Equals(fileReadAllBytesMd5, utf8Md5)) 37 { 38 Console.WriteLine($"{nameof(fileReadAllBytesMd5)} is equal with {nameof(utf8Md5)}!"); 39 } 40 else 41 { 42 Console.WriteLine($"{nameof(fileReadAllBytesMd5)} is not equal with {nameof(utf8Md5)}!"); 43 } 44 45 Console.WriteLine($"utf8Md5:{utf8Md5},utf32Md5:{utf32Md5}"); 46 if (string.Equals(utf8Md5, utf32Md5)) 47 { 48 Console.WriteLine($"{nameof(utf8Md5)} is equals with {nameof(utf32Md5)}"); 49 } 50 else 51 { 52 Console.WriteLine($"{nameof(utf8Md5)} is not equals with {nameof(utf32Md5)}"); 53 } 54 } 55 56 static string GetBytesMd5(byte[] bytesData) 57 { 58 StringBuilder md5Builder = new StringBuilder(); 59 using(MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider()) 60 { 61 byte[] md5Bytes = md5.ComputeHash(bytesData); 62 for(int i=0;i<md5Bytes.Length;i++) 63 { 64 md5Builder.Append(md5Bytes[i].ToString("x2")); 65 } 66 } 67 return md5Builder.ToString(); 68 }
I had validated that different encoding mode can generate different result,they are not identical.
Besides,the File.ReadAllBytes may based on UTF8 because they render the identical result!
C# compare different Encoding pattern between UTF8 and UTF32 based on Md5