欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# compare different Encoding pattern between UTF8 and UTF32 based on Md5

程序员文章站 2022-07-28 15:49:51
I had validated that different encoding mode can generate different result,they are not identical. Besides,the File.ReadAllBytes may based on UTF8 bec ......
 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!