public static byte[] makeCrc16(byte[] ccc) { CRC16Util crc16 = new CRC16Util(); crc16.reset(); crc16.update(ccc); byte[] test = intToByte(crc16.getCrcValue()); return(test); }
public static string GetHashCodeFromFile(string fileName, FileCodeHashType hashtype) { string rst = string.Empty; byte[] retVal = null; try { switch (hashtype) { case FileCodeHashType.MD5: case FileCodeHashType.SHA1: using (FileStream file = new FileStream(fileName, System.IO.FileMode.Open)) { switch (hashtype) { case FileCodeHashType.MD5: MD5 md5 = new MD5CryptoServiceProvider(); retVal = md5.ComputeHash(file); rst = GetCodeByByteArray(retVal); break; case FileCodeHashType.SHA1: SHA1 sha1 = new SHA1CryptoServiceProvider(); retVal = sha1.ComputeHash(file); rst = GetCodeByByteArray(retVal); break; } file.Close(); } break; case FileCodeHashType.CRC16: rst = CRC16Util.GetFileCRC16(fileName); break; case FileCodeHashType.CRC32: rst = CRC32Util.GetFileCRC32(fileName); break; } return(rst); } catch (Exception ex) { throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message); } }
public byte[] PackProtobuf(proto_util protoUtil) { if (msg == null) { msg = RecycleMS.CreateInstance(); if (data != null) { msg.Write(data, 0, data.Length); } } mAskUniqueID = Convert.ToUInt16(protoUtil.AskUniqueID()); //Debug.LogError("mAskUniqueID:" + mAskUniqueID); ushort packageLen = Convert.ToUInt16(NetConstant.cPackageHeadSize + msg.Length); byte[] bytes = msg.ToArray(); RecycleMS packageCrc = RecycleMS.CreateInstance(); proto_util.writeUShort(packageCrc, mAskUniqueID); proto_util.EncryptPackage(packageCrc, bytes); byte[] crcBytes = packageCrc.ToArray(); packageCrc.Release(); RecycleMS package = RecycleMS.CreateInstance(); package.SetLength(packageLen); proto_util.writeUShort(package, Convert.ToUInt16(msg.Length)); proto_util.writeUShort(package, this.mCMDID); proto_util.writeUShort(package, mAskUniqueID); proto_util.writeUShort(package, CRC16Util.ConCRC(crcBytes, crcBytes.Length)); proto_util.EncryptPackage(package, bytes); byte[] packBytes = package.ToArray(); package.Release();//package在后面的代码不再使用,则释放 return(packBytes); }