1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- namespace assetBundleUtility
- {
- using System.Collections.Generic;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
-
- public class MD5Utility
- {
- public static byte[] GetMD5(string str)
- {
- byte[] bytes = Encoding.UTF8.GetBytes(str);
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- byte[] md5Bytes = md5.ComputeHash(bytes);
- return md5Bytes;
- }
-
- public static byte[] GetMD5(FileStream fileStream)
- {
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- byte[] md5Bytes = md5.ComputeHash(fileStream);
- return md5Bytes;
- }
-
- public static string BytesToString(byte[] bytes)
- {
- StringBuilder stringBuilder = new StringBuilder();
- foreach (var md5Byte in bytes)
- {
- stringBuilder.Append(md5Byte.ToString("X2")); //X2表示转为大写
- }
- return stringBuilder.ToString();
- }
-
- public static List<string> GetAllMD5StringFromPath(List<string> pathes)
- {
- List<string> md5Strings = new List<string>();
- for (int i = 0; i < pathes.Count; i++)
- {
- string md5String = GetMD5StringFromPath(pathes[i]);
- md5Strings.Add(md5String);
- }
- return md5Strings;
- }
-
- public static string GetMD5StringFromPath(string path)
- {
- FileStream fileStream = new FileStream(path, FileMode.Open);
- byte[] bytes = GetMD5(fileStream);
- string md5Strings = BytesToString(bytes);
- fileStream.Close();
- return md5Strings;
- }
- }
- }
|