123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- namespace HPSocketCS.Extended
- {
-
-
-
- public enum AppState
- {
-
-
-
- Starting=0,
-
-
-
- Started=1,
-
-
-
- Stoping=2,
-
-
-
- Stoped=3,
-
-
-
- Error=4
- }
-
-
-
- public enum DataType
- {
-
-
-
- None=0,
-
-
-
- Array=1,
-
-
-
- List=2,
-
-
-
- Text=3,
-
-
-
- Serialization=4,
-
-
-
- File=5,
-
-
-
- ShowForm=6,
-
-
-
- PushMessage=7,
-
-
-
- SQLHelper=8,
- }
-
-
-
- public enum FileRequestType
- {
-
-
-
- Upload=0,
-
-
-
- Download=1
- }
-
-
-
- public enum TransferRequestCommand
- {
-
-
-
- TestCommand=-1,
-
-
-
- EmptyCommand=0,
-
-
-
- VitalInfo=1,
-
-
-
- VitalControl = 2,
-
-
-
- VitalControlOK=3,
-
-
-
- FileTransfer=1000,
-
-
-
- Message = 1001,
-
-
-
- OpenFileTransfer = 1002,
-
-
-
- CloseFileTransfer = 1003,
-
-
-
- UploadAvatar = 1004,
-
-
-
- DownloadAvatar = 1005,
-
-
-
- UploadFile = 1006,
-
-
-
- DownloadFile = 1007,
-
-
-
- GetFileInfo = 1008,
-
-
-
- GetbackupPath = 1009,
-
-
-
- GetSavePath = 1010,
-
-
-
- UploadCustomersAvatar = 1011,
-
-
-
- DownloadCustomersAvatar = 1012,
-
-
-
- GetServerSoftwareInfo = 1013,
-
-
-
- GetDataConne = 1014,
-
-
-
- GetSavePhotoPathList = 1015,
-
-
-
- ServerDateTime = 1016,
-
-
-
- DeleteFile = 1017,
-
-
-
- GenerateThumbnails = 1018,
-
-
-
- ImportPhotos = 1019,
-
-
-
- SelectPhotos = 1020,
-
-
-
- LookPhotos = 1021,
-
-
-
- UpdateOrder = 1022,
-
-
-
- GetUpdateClientSkinImage = 1023,
-
-
-
- DeleteOriginalPhoto = 1024,
-
-
-
- DeleteBackupPhoto = 1025,
-
-
-
- GetClientList = 1026,
-
-
-
- RefreshClientList = 1027,
-
-
-
- GetBigData = 1028,
-
-
-
- GeTFileList = 1029,
-
-
-
- GetDirectoryList = 1030,
-
-
-
- DeleteDirectory = 1031,
-
-
-
- GetBackupRecords=1032,
-
-
-
- GetMainShopBackupRecords=1033,
-
-
-
- MainStoreTestInfo=1034,
-
-
-
- SQLHelperData=1035,
-
-
-
- SerializationData=1036,
-
-
-
- InstallationDatabase=1037,
-
-
-
- UpgradeDatabase = 1038,
-
-
-
- RefreshCloudServerData=1039,
-
-
-
- CloudLoginIP=1040,
-
-
-
- SubmitSoftwareInfo=1041,
-
-
-
- GetMainStoreServerSoftwareInfo = 1042,
-
-
-
- BackupDatabase=1043,
-
-
-
- ClientOnlineConnection=1044,
- }
-
-
-
- public enum SQLHelperRequestType
- {
-
-
-
- QueryReturnDataSet= 0,
-
-
-
- ExecuteScalar=1,
-
-
-
- ExecuteNonQuery=2,
-
-
-
- RunProcedure=3,
-
-
-
- ExecuteSqlTran=4,
-
-
-
- ExistsSqlConn=5,
-
- }
- public delegate void DelegateExecuteThreadStart(ReturnResultObject argumentResult);
- public class CommonLibrary
- {
- public CommonLibrary() {
-
- }
-
-
-
- public static string BasePath
- {
- get
- {
- System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
-
-
-
-
- string processName = p.ProcessName.ToLower();
- string retPath = System.Windows.Forms.Application.StartupPath;
- if (processName == "aspnet_wp" || processName == "w3wp" || processName == "webdev.webserver" || processName == "iisexpress")
- {
- if (System.Web.HttpContext.Current != null)
- retPath = System.Web.HttpContext.Current.Server.MapPath("~/").TrimEnd(new char[] { '\\' });
- else
- {
- retPath = System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' });
- }
- }
- return retPath;
- }
- }
-
-
-
-
-
- public static string GetFullDirectoryPath(string directoryPath)
- {
- if (directoryPath.LastIndexOf("\\") != directoryPath.Length - 1)
- {
- directoryPath = directoryPath + "\\";
- }
- return directoryPath;
- }
-
-
-
-
- public static long GenerateId()
- {
- byte[] buffer = Guid.NewGuid().ToByteArray();
- return BitConverter.ToInt64(buffer, 0);
- }
- static string _ServerVersionNumber = "1.0";
-
-
-
- public static string ServerVersionNumber
- {
- get { return CommonLibrary._ServerVersionNumber; }
- set { CommonLibrary._ServerVersionNumber = value; }
- }
- static string _ClientMarkID = "ServerHosts";
-
-
-
- public static string ClientMarkID
- {
- get { return CommonLibrary._ClientMarkID; }
- set { CommonLibrary._ClientMarkID = value; }
- }
- static string _ClientMarkName = "服务器主机";
-
-
-
- public static string ClientMarkName
- {
- get { return CommonLibrary._ClientMarkName; }
- set { CommonLibrary._ClientMarkName = value; }
- }
- static string _LoginUsername = "未知用户";
-
-
-
- public static string LoginUsername
- {
- get { return CommonLibrary._LoginUsername; }
- set { CommonLibrary._LoginUsername = value; }
- }
- private static string _HardwareCode = "HardwareCode";
-
-
-
- public static string HardwareCode
- {
- get { return CommonLibrary._HardwareCode; }
- set { CommonLibrary._HardwareCode = value; }
- }
- #region 发送数据时的方法
-
-
-
-
-
-
-
-
-
-
- public static TcpHeadInfo GetProtocolHeader(IntPtr ConnId, IntPtr addedDataPtr, DataType dType, TransferRequestCommand requestCommand, Int64 transportID, bool isTail = false)
- {
- HPSocketCS.Extended.ClientInfo ci = null;
- try
- {
-
-
- if (addedDataPtr != IntPtr.Zero)
- {
- ci = (HPSocketCS.Extended.ClientInfo)Marshal.PtrToStructure(addedDataPtr, typeof(HPSocketCS.Extended.ClientInfo));
- }
- }
- catch { }
- TcpHeadInfo protocolHeader = new TcpHeadInfo();
- protocolHeader.ConnId = ConnId.ToInt32();
- protocolHeader.Hostname = System.Net.Dns.GetHostName();
- protocolHeader.MsgDataType = (int)dType;
- protocolHeader.RequestCommand = (int)requestCommand;
- protocolHeader.IntactSize = 0;
- protocolHeader.ServerNowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- protocolHeader.ClientMarkID = CommonLibrary.ClientMarkID;
- protocolHeader.ClientMarkName = CommonLibrary.ClientMarkName;
- protocolHeader.ServerVersionNumber = CommonLibrary.ServerVersionNumber;
- protocolHeader.LoginUsername = CommonLibrary.LoginUsername;
- protocolHeader.HardwareCode = CommonLibrary.HardwareCode;
- protocolHeader.TransportEnd = true;
- protocolHeader.TransportStart = true;
- protocolHeader.TransportID = transportID;
- protocolHeader.IsTail = isTail;
- if (ci != null)
- {
- protocolHeader.IPAddress = ci.IpAddress;
- protocolHeader.Port = ci.Port;
- }
- return protocolHeader;
- }
-
-
-
-
-
- public static void WriteLogs(string txt)
- {
- try
- {
- string fullPath = BasePath + "\\Logs\\MainLogs\\"+DateTime.Now.ToString("yyyy-MM-dd")+".txt";
- if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(fullPath)))
- {
- System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fullPath));
- }
- using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fullPath, true, Encoding.UTF8))
- {
- sw.WriteLine("【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "】" + txt);
- sw.Flush();
- sw.Close();
- }
- }
- catch { }
- }
-
-
-
-
-
-
-
- public static System.IO.BufferedStream AutoUnpackingAndAddPkgHeader(byte[] sendPackBytes, TcpHeadInfo pkgHeader, TcpTailInfo pkgTail, int maxPackSize)
- {
-
-
- byte[] packBytes = HPSocketCS.Extended.DataSetSerializerDeserialize.DataCompressionRetBytes(sendPackBytes);
- System.IO.MemoryStream retDataMstream = new System.IO.MemoryStream();
- System.IO.BufferedStream buffStream = new System.IO.BufferedStream(retDataMstream, Sdk.PacketBufferSize * 2);
- try
- {
-
-
-
-
-
- pkgHeader.IntactSize = packBytes.Length;
- pkgHeader.ServerNowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
-
- int pkgHeaderSize = Marshal.SizeOf(pkgHeader);
- int pkgTailSize = 0;
- if (pkgHeader.IsTail)
- {
- pkgTailSize = Marshal.SizeOf(pkgTail);
- }
-
- int AppendPakSize = pkgHeaderSize + pkgTailSize;
- if (packBytes.Length + AppendPakSize <= maxPackSize)
- {
- pkgHeader.TransportStart = true;
- pkgHeader.TransportEnd = true;
-
- byte[] pkgHeaderBytes = StructureToByte<TcpHeadInfo>(pkgHeader);
-
- byte[] sendBytes = null;
- if (pkgHeader.IsTail)
- {
- byte[] tailBytes = StructureToByte<TcpTailInfo>(pkgTail);
- sendBytes = CommonLibrary.GetSendBuffer(pkgHeaderBytes, packBytes, tailBytes);
- tailBytes = null;
- }
- else
- {
- sendBytes = CommonLibrary.GetSendBuffer(pkgHeaderBytes, packBytes, null);
- }
- buffStream.Write(sendBytes, 0, sendBytes.Length);
-
-
- sendBytes = null;
- pkgHeaderBytes = null;
- }
- else
- {
-
- int packSize = Convert.ToInt32(maxPackSize - AppendPakSize);
-
- int packCount = packBytes.Length / packSize;
- if (packBytes.Length % packSize > 0)
- {
- packCount = packCount + 1;
- }
- for (int i = 0; i < packCount; i++)
- {
- byte[] tempPackBytes = new byte[packSize];
- if (i == 0)
- {
- pkgHeader.TransportStart = true;
- pkgHeader.TransportEnd = false;
- }
- else
- {
- pkgHeader.TransportStart = false;
- pkgHeader.TransportEnd = false;
- }
- if (i + 1 == packCount)
- {
- pkgHeader.TransportEnd = true;
- tempPackBytes = new byte[packBytes.Length - packSize * i];
- }
- Array.Copy(packBytes, i * packSize, tempPackBytes, 0, tempPackBytes.Length);
-
- byte[] pkgHeaderBytes = StructureToByte<TcpHeadInfo>(pkgHeader);
-
- byte[] sendBytes = null;
- if (pkgHeader.IsTail)
- {
- byte[] tailBytes = StructureToByte<TcpTailInfo>(pkgTail);
- sendBytes = CommonLibrary.GetSendBuffer(pkgHeaderBytes, tempPackBytes, tailBytes);
- tailBytes = null;
- }
- else
- {
- sendBytes = CommonLibrary.GetSendBuffer(pkgHeaderBytes, tempPackBytes, null);
- }
- buffStream.Write(sendBytes, 0, sendBytes.Length);
-
-
- pkgHeaderBytes = null;
- sendBytes = null;
- }
- }
- }
- catch
- {
- }
- buffStream.Flush();
- buffStream.Position = 0;
- retDataMstream.Position = 0;
- Array.Clear(packBytes, 0, packBytes.Length);
- packBytes = null;
-
- return buffStream;
- }
-
-
-
- public static byte[] StructureToByte<T>(T structure)
- {
- int size = Marshal.SizeOf(typeof(T));
- byte[] buffer = new byte[size];
- IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.StructureToPtr(structure, bufferIntPtr, true);
- Marshal.Copy(bufferIntPtr, buffer, 0, size);
- }
- finally
- {
- Marshal.FreeHGlobal(bufferIntPtr);
- }
- return buffer;
- }
-
-
-
-
-
-
- public static byte[] GetSendBuffer(byte[] headerBytes, byte[] bodyBytes,byte[] tailBytes)
- {
-
- byte[] retBytes = null;
- try
- {
- int bufferSize = headerBytes.Length + bodyBytes.Length;
- if (tailBytes != null)
- {
- bufferSize= bufferSize + tailBytes.Length;
- }
- retBytes = new byte[bufferSize];
-
- Array.Copy(headerBytes, 0, retBytes, 0, headerBytes.Length);
-
-
- Array.Copy(bodyBytes, 0, retBytes, headerBytes.Length, bodyBytes.Length);
-
- if (tailBytes != null)
- {
-
- Array.Copy(tailBytes, 0, retBytes, headerBytes.Length + bodyBytes.Length, tailBytes.Length);
-
- }
- return retBytes;
- }
- finally
- {
- headerBytes = null;
- bodyBytes = null;
- tailBytes = null;
- }
- }
-
- #endregion
- #region 接收数据时的方法
-
-
-
- List<ReceiveData> ReceiveDataList = new List<ReceiveData>();
-
-
-
-
-
- public ReceiveData GetReceiveData(TcpHeadInfo tcpInfo)
- {
- ReceiveData retRData = null;
- lock (ReceiveDataList)
- {
- foreach (ReceiveData rData in ReceiveDataList)
- {
- if (rData.ReceiveRemoteClientInfo.ConnId == tcpInfo.ConnId && rData.ReceiveRemoteClientInfo.TransportID == tcpInfo.TransportID)
- {
- retRData = rData;
- break;
- }
- }
- return retRData;
- }
- }
-
-
-
-
- public void AddReceiveData(ReceiveData rData)
- {
- lock (ReceiveDataList)
- {
- this.ReceiveDataList.Add(rData);
- }
- }
-
-
-
-
- public void RemoveReceiveDataData(ReceiveData rData)
- {
- try
- {
- lock (ReceiveDataList)
- {
- ReceiveDataList.Remove(rData);
- }
- if (rData != null)
- {
- rData.Dispose();
-
-
-
- rData = null;
- }
- }
- catch { }
- finally
- {
- System.GC.Collect();
- }
- }
-
-
-
-
- public void ClearAllInvalidResource()
- {
- lock (ReceiveDataList)
- {
- int count = ReceiveDataList.Count;
- for (int i = 0; i < count; i++)
- {
- DispenseReceiveData(ReceiveDataList[i]);
- }
- ReceiveDataList.Clear();
- }
- }
- public void DispenseReceiveData(ReceiveData rData)
- {
- lock (ReceiveDataList)
- {
- ReceiveDataList.Remove(rData);
- if (rData != null)
- {
-
- rData.Dispose();
- rData = null;
- }
- }
- }
-
-
-
-
- public void ClearInvalidConnectionResource(IntPtr connId)
- {
- try
- {
- List<ReceiveData> retRDatalist = new List<ReceiveData>();
- lock (ReceiveDataList)
- {
- foreach (ReceiveData rData in ReceiveDataList)
- {
- if (rData.ReceiveRemoteClientInfo.ConnId == connId.ToInt32())
- {
- retRDatalist.Add(rData);
- }
- }
- for (int i = 0; i < retRDatalist.Count; i++)
- {
- DispenseReceiveData(retRDatalist[i]);
- }
- retRDatalist.Clear();
- }
- }
- catch
- {
- }
- finally
- {
- System.GC.Collect();
- }
- }
- #endregion
- #region 客户端接收到的数据缓存处理方法
- Dictionary<Int64, ReceiveCacheBuffer> _ServerToClientCacheBufferList = new Dictionary<Int64, ReceiveCacheBuffer>();
-
-
-
- public Dictionary<Int64, ReceiveCacheBuffer> ServerToClientCacheBufferList
- {
- get { return _ServerToClientCacheBufferList; }
- set { _ServerToClientCacheBufferList = value; }
- }
-
-
-
-
-
- public ReceiveCacheBuffer GetReceiveCacheBuffer(Int64 cid)
- {
- lock (_ServerToClientCacheBufferList)
- {
- ReceiveCacheBuffer retCacheBuffer = null;
- if (_ServerToClientCacheBufferList.TryGetValue(cid, out retCacheBuffer))
- {
- return retCacheBuffer;
- }
- else
- {
- return retCacheBuffer;
- }
- }
- }
-
-
-
-
-
- public void AddCacheBufferToList(Int64 cid, ReceiveCacheBuffer rCacheBuffer)
- {
- lock (_ServerToClientCacheBufferList)
- {
- if (!this._ServerToClientCacheBufferList.ContainsKey(cid))
- {
- this._ServerToClientCacheBufferList.Add(cid, rCacheBuffer);
- }
-
- }
- }
-
-
-
-
- public void RemoveServerToClientByteList(Int64 cid)
- {
- try
- {
- lock (_ServerToClientCacheBufferList)
- {
- try
- {
- ReceiveCacheBuffer rCacheBuffer = GetReceiveCacheBuffer(cid);
- if (rCacheBuffer != null)
- {
- rCacheBuffer.Dispose();
- rCacheBuffer = null;
- }
- }
- catch { }
- _ServerToClientCacheBufferList.Remove(cid);
- }
- }
- finally {
- System.GC.Collect();
- }
- }
-
-
-
- public void ClearAllServerToClientByteList()
- {
- try
- {
- lock (_ServerToClientCacheBufferList)
- {
- foreach (long key in _ServerToClientCacheBufferList.Keys)
- {
- try
- {
- _ServerToClientCacheBufferList[key].Dispose();
- }
- catch { }
- }
- _ServerToClientCacheBufferList.Clear();
- }
- }
- finally
- {
- System.GC.Collect();
- }
- }
- #endregion
-
-
-
-
-
-
- public static void SetBackgroundWorker(long sumCount, long currentCount, string msg = "", System.ComponentModel.BackgroundWorker backgroundWorker = null)
- {
- if (backgroundWorker != null)
- {
- try
- {
- int currentProgres = Convert.ToInt32(1000f / sumCount * currentCount);
-
- backgroundWorker.ReportProgress(currentProgres, msg);
- }
- catch { }
- }
- }
-
-
-
-
-
- public static string GetVCFileMD5(string filePath)
- {
- string retMd5Value = "";
- byte[] retByte = new byte[32];
- IntPtr bufferIntPtr_File = System.Runtime.InteropServices.Marshal.AllocHGlobal(32);
- if (Environment.Is64BitProcess)
- {
- if (VCMD5_X64.GetFileMD5(filePath, bufferIntPtr_File, 32) == 0)
- {
- System.Runtime.InteropServices.Marshal.Copy(bufferIntPtr_File, retByte, 0, 32);
- retMd5Value = Encoding.UTF8.GetString(retByte);
- }
- }
- else
- {
- if (VCMD5_X86.GetFileMD5(filePath, bufferIntPtr_File, 32) == 0)
- {
- System.Runtime.InteropServices.Marshal.Copy(bufferIntPtr_File, retByte, 0, 32);
- retMd5Value = Encoding.UTF8.GetString(retByte);
- }
- }
- System.Array.Clear(retByte, 0, retByte.Length);
- retByte = null;
- if (bufferIntPtr_File != IntPtr.Zero)
- {
- System.Runtime.InteropServices.Marshal.FreeHGlobal(bufferIntPtr_File);
- }
- return retMd5Value;
- }
-
-
-
-
-
- public static string GetVCByteMD5(byte[] bytes)
- {
- string retMd5Value = "";
- byte[] retByte = new byte[32];
- IntPtr lpBytes = System.Runtime.InteropServices.Marshal.AllocHGlobal(bytes.Length);
- System.Runtime.InteropServices.Marshal.Copy(bytes, 0, lpBytes, bytes.Length);
- IntPtr bufferIntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(32);
- if (Environment.Is64BitProcess)
- {
- if (VCMD5_X64.GetByteMD5(lpBytes, bytes.Length, bufferIntPtr) == 0)
- {
- System.Runtime.InteropServices.Marshal.Copy(bufferIntPtr, retByte, 0, 32);
- retMd5Value = Encoding.UTF8.GetString(retByte);
- }
- }
- else
- {
- if (VCMD5_X86.GetByteMD5(lpBytes, bytes.Length, bufferIntPtr) == 0)
- {
- System.Runtime.InteropServices.Marshal.Copy(bufferIntPtr, retByte, 0, 32);
- retMd5Value = Encoding.UTF8.GetString(retByte);
- }
- }
- if (bufferIntPtr != IntPtr.Zero)
- {
- System.Runtime.InteropServices.Marshal.FreeHGlobal(bufferIntPtr);
- }
- if (lpBytes != IntPtr.Zero)
- {
- System.Runtime.InteropServices.Marshal.FreeHGlobal(lpBytes);
- }
- return retMd5Value;
- }
-
-
-
-
-
- public static string GetVCStringMD5(string str)
- {
- string retMd5Value = "";
- byte[] retByteUtf8 = Encoding.UTF8.GetBytes(str);
- string Gb2313 = Encoding.GetEncoding("gb2312").GetString(retByteUtf8);
- byte[] retByte = new byte[32];
- IntPtr bufferIntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(32);
- if (Environment.Is64BitProcess)
- {
- if (VCMD5_X64.GetStringMD5(Gb2313, bufferIntPtr, 32) == 0)
- {
- System.Runtime.InteropServices.Marshal.Copy(bufferIntPtr, retByte, 0, 32);
- retMd5Value = Encoding.UTF8.GetString(retByte);
- }
- }
- else
- {
- if (VCMD5_X86.GetStringMD5(Gb2313, bufferIntPtr, 32) == 0)
- {
- System.Runtime.InteropServices.Marshal.Copy(bufferIntPtr, retByte, 0, 32);
- retMd5Value = Encoding.UTF8.GetString(retByte);
- }
- }
- if (bufferIntPtr != IntPtr.Zero)
- {
- System.Runtime.InteropServices.Marshal.FreeHGlobal(bufferIntPtr);
- }
- return retMd5Value;
- }
-
-
-
- static bool isFIPSError = false;
-
-
-
-
-
- public static string GetMD5HashFromFile(byte[] byets)
- {
- try
- {
- if (!isFIPSError)
- {
- try
- {
- System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] retVal = md5.ComputeHash(byets);
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < retVal.Length; i++)
- {
- sb.Append(retVal[i].ToString("x2"));
- }
- byets = null;
- return sb.ToString();
- }
- catch
- {
- isFIPSError = true;
- return GetVCByteMD5(byets);
-
- }
- }
- else
- {
- return GetVCByteMD5(byets);
- }
- }
- catch (Exception ex)
- {
- throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
- }
- }
-
-
-
-
-
- public static string GetMD5HashFromFile(string fileName)
- {
- try
- {
- if (!isFIPSError)
- {
- System.IO.FileStream file = null;
- try
- {
- file = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
- }
- catch (Exception ex)
- {
- throw new Exception("验证MD5时打开文件出失败:" + ex.Message);
- }
- try
- {
- System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] retVal = md5.ComputeHash(file);
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < retVal.Length; i++)
- {
- sb.Append(retVal[i].ToString("x2"));
- }
- System.Array.Clear(retVal, 0, retVal.Length);
- retVal = null;
- return sb.ToString();
- }
- catch
- {
- isFIPSError = true;
- return GetVCFileMD5(fileName);
-
- }
- finally
- {
- file.Close();
- file.Dispose();
- }
- }
- else
- {
- return GetVCFileMD5(fileName);
- }
- }
- catch (Exception ex)
- {
- throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
- }
- }
-
-
-
-
-
-
- public static bool VerifyMd5Hash(string md51, string md52)
- {
- if (md51.Trim().ToUpper() == md52.Trim().ToUpper())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- public class VCMD5_X86
- {
- [System.Runtime.InteropServices.DllImport("lyfzMD5_x86.dll")]
- public static extern int GetStringMD5(string lpString, IntPtr pResult, int length);
- [System.Runtime.InteropServices.DllImport("lyfzMD5_x86.dll")]
- public static extern int GetFileMD5(string lpFilepath, IntPtr pResult, int length);
- [System.Runtime.InteropServices.DllImport("lyfzMD5_x86.dll")]
- public static extern int GetByteMD5(IntPtr lpBytes, int nInputlen, IntPtr pResult);
- }
- public class VCMD5_X64
- {
- [System.Runtime.InteropServices.DllImport("lyfzMD5_x64.dll")]
- public static extern int GetStringMD5(string lpString, IntPtr pResult, int length);
- [System.Runtime.InteropServices.DllImport("lyfzMD5_x64.dll")]
- public static extern int GetFileMD5(string lpFilepath, IntPtr pResult, int length);
- [System.Runtime.InteropServices.DllImport("lyfzMD5_x64.dll")]
- public static extern int GetByteMD5(IntPtr lpBytes, int nInputlen, IntPtr pResult);
- }
- }
|