字符串、字节数组、流之间的相互转换以及文件MD5的计算

时间:2022-06-01 18:05:08
  1 using System;
  2 using System.IO;
  3 using System.Security.Cryptography;
  4 using System.Text;
  5 
  6 namespace myMethod
  7 {
  8     class lgs
  9     {
 10         static void Main()
 11         {
 12             Console.ReadKey();
 13         }
 14 
 15         /// <summary>
 16         /// 使用不同的编码格式将  字符串 → 字节数组
 17         /// </summary>
 18         /// <returns></returns>
 19         static void StringToByteArray(string param, out byte[] result)
 20         {
 21             //result = Encoding.ASCII.GetBytes(param);
 22             //result = Encoding.UTF32.GetBytes(param);
 23             result = Encoding.UTF7.GetBytes(param);
 24         }
 25 
 26         /// <summary>
 27         /// 使用不同的编码格式将  字节数组 → 字符串
 28         /// </summary>
 29         /// <returns></returns>
 30         static void ByteArrayToString(byte[] param, out string result)
 31         {
 32             //result = Encoding.UTF7.GetString(param);
 33             //result = Encoding.Default.GetString(param);
 34             result = Encoding.ASCII.GetString(param);
 35         }
 36 
 37         /// <summary>
 38         /// 字符串转换为流
 39         /// </summary>
 40         static void StringToStream(string param, out MemoryStream result)
 41         {
 42             byte[] tmpArray = Convert.FromBase64String(param);  //相当于将字符串转换为等效的字节数组
 43             result = new MemoryStream(tmpArray);
 44         }
 45 
 46         /// <summary>
 47         /// 流转换了字符串
 48         /// </summary>
 49         static void StreamToString(MemoryStream param, out string result)
 50         {
 51             //方式一 :param.ToArray() 将流内容写入字节数组
 52             result = Encoding.Default.GetString(param.ToArray());
 53 
 54             //方式二:
 55             result = Convert.ToBase64String(param.ToArray());
 56         }
 57 
 58         /// <summary>
 59         /// 字节数组转换为流
 60         /// </summary>
 61         static void ByteArrayToStream(byte[] param, out MemoryStream result)
 62         {
 63             //方式一:
 64             //result = new MemoryStream(param);
 65 
 66             //方式二:
 67             result = new MemoryStream();
 68             result.Read(param, 0, param.Length);
 69         }
 70 
 71         /// <summary>
 72         /// 流转换为字节数组
 73         /// </summary>
 74         static void SteamToByteArray(MemoryStream param, out byte[] result)
 75         {
 76             //方式一:
 77             result = param.ToArray();
 78 
 79             //方式二:
 80             //param.Write(result, 0, (int)param.Length);
 81         }
 82 
 83         /// <summary>
 84         /// 计算文件的 MD5 值
 85         /// </summary>
 86         /// <param name="fileName">要计算 MD5 值的文件名和路径</param>
 87         /// <returns>MD5 值16进制字符串</returns>
 88         static string MD5File(string fileName)
 89         {
 90             return HashFile(fileName, "md5");
 91         }
 92 
 93         /// <summary>
 94         /// 计算文件的 sha1 值
 95         /// </summary>
 96         /// <param name="fileName">要计算 sha1 值的文件名和路径</param>
 97         /// <returns>sha1 值16进制字符串</returns>
 98         static string SHA1File(string fileName)
 99         {
100             return HashFile(fileName, "sha1");
101         }
102 
103         /// <summary>
104         /// 计算文件的哈希值
105         /// </summary>
106         /// <param name="fileName">要计算哈希值的文件名和路径</param>
107         /// <param name="algName">算法:sha1,md5</param>
108         /// <returns>哈希值16进制字符串</returns>
109         static string HashFile(string fileName, string algName)
110         {
111             if (!File.Exists(fileName))
112                 return string.Empty;
113 
114             FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
115             byte[] hashBytes = HashData(fs, algName);
116             fs.Close();
117             return ByteArrayToHexString(hashBytes);
118         }
119 
120         /// <summary>
121         /// 计算哈希值
122         /// </summary>
123         /// <param name="stream">要计算哈希值的 Stream</param>
124         /// <param name="algName">算法:sha1,md5</param>
125         /// <returns>哈希值字节数组</returns>
126         static byte[] HashData(Stream stream, string algName)
127         {
128             HashAlgorithm algorithm;
129             if (algName == null)
130             {
131                 throw new ArgumentNullException("algName 不能为 null");
132             }
133             if (string.Compare(algName, "sha1", true) == 0)
134             {
135                 algorithm = SHA1.Create();
136             }
137             else
138             {
139                 if (string.Compare(algName, "md5", true) != 0)
140                 {
141                     throw new Exception("algName 只能使用 sha1 或 md5");
142                 }
143                 algorithm = MD5.Create();
144             }
145             return algorithm.ComputeHash(stream);
146         }
147 
148         /// <summary>
149         /// 字节数组转换为16进制表示的字符串
150         /// </summary>
151         static string ByteArrayToHexString(byte[] buf)
152         {
153             // ToString(buf) --- 由以连字符分隔的十六进制对构成的字符串,其中每一对表示 value 中对应的元素;例如“7F-2C-4A”。
154             return BitConverter.ToString(buf).Replace("-", "");
155         }
156     }
157 }

 参考:https://www.oschina.net/code/snippet_2256220_53535