private static bool CheckForTestPackNds() { bool ret = true; string testpackPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), Mk2sfWorker.TESTPACK_PATH); if (!File.Exists(testpackPath)) { ret = false; MessageBox.Show(String.Format(ConfigurationManager.AppSettings["Form_Make2sf_ErrorMessageTestpackMissing"], Path.GetFileName(testpackPath), Path.GetDirectoryName(testpackPath)), String.Format(ConfigurationManager.AppSettings["Form_Make2sf_ErrorMessageTestpackMissingHeader"], Path.GetFileName(testpackPath))); } else { using (FileStream fs = File.OpenRead(testpackPath)) { if (!ChecksumUtil.GetCrc32OfFullFile(fs).Equals(Mk2sfWorker.TESTPACK_CRC32)) { ret = false; MessageBox.Show(String.Format(ConfigurationManager.AppSettings["Form_Make2sf_ErrorMessageTestpackCrc32"], Path.GetFileName(testpackPath), Path.GetDirectoryName(testpackPath), Mk2sfWorker.TESTPACK_CRC32), String.Format(ConfigurationManager.AppSettings["Form_Make2sf_ErrorMessageTestpackCrc32Header"], Path.GetFileName(testpackPath))); } } } return(ret); }
// recalculates the checksum and then modifies the internal bytes in the ROM so it contains // the valid checksum in the ROM header. // // NOTE: this new checksum is [currently] never saved with the project file / serialized (since we don't // store the potentially copyrighted ROM bytes in the project file). it should just be used for // testing/verification purposes. (that is why this is protected, it's not part of the normal API) public void FixChecksum() { var rawRomBytesCopy = Data.RomBytes.CreateListRawRomBytes(); ChecksumUtil.UpdateRomChecksum(rawRomBytesCopy, Data.RomMapMode, GetRomSize()); Data.RomBytes.SetBytesFrom(rawRomBytesCopy, 0); }
public void GetDatFileChecksums( ref Crc32 pChecksum, ref CryptoStream pMd5CryptoStream, ref CryptoStream pSha1CryptoStream) { using (FileStream fs = File.OpenRead(this.filePath)) { // Reserved Section ChecksumUtil.AddChunkToChecksum( fs, (int)RESERVED_SECTION_OFFSET, (int)this.reservedSectionLength, ref pChecksum, ref pMd5CryptoStream, ref pSha1CryptoStream); // Compressed Program this.addDecompressedProgramChecksum(fs, ref pChecksum, ref pMd5CryptoStream, ref pSha1CryptoStream); } // Libs string[] libPaths = this.GetLibPathArray(); foreach (string f in libPaths) { using (FileStream lfs = File.OpenRead(f)) { Xsf libXsf = new Xsf(); libXsf.Initialize(lfs, f); libXsf.GetDatFileChecksums(ref pChecksum, ref pMd5CryptoStream, ref pSha1CryptoStream); libXsf = null; } } }
private string getFileNameForChecksum(string pFileName, string pHashKey) { string checksumKey; string newFileName; string ret; using (FileStream fs = File.OpenRead(pFileName)) { checksumKey = String.Format("{0}_{1}_{2}", ChecksumUtil.GetCrc32OfFullFile(fs), ChecksumUtil.GetMd5OfFullFile(fs), ChecksumUtil.GetSha1OfFullFile(fs)); } if (this.fileNameHash[pHashKey].ContainsKey(checksumKey)) { ret = this.fileNameHash[pHashKey][checksumKey]; } else { newFileName = this.fileNameHash[pHashKey].Count.ToString("X8"); this.fileNameHash[pHashKey].Add(checksumKey, newFileName); ret = newFileName; } return(ret); }
private static void SendLocationAcknowledge(string status, string @event, string checksum, MessageInput input) { if (Char.IsLower(status[0])) { string response = $"$EAVACK,{@event},{checksum}"; response = $"{response}*{ChecksumUtil.NMEA(response.ToByteArray())}\r\n"; input.NetworkStream.Write(StringUtil.ConvertStringToByteArray(response)); } }
/** 解析单次消息内容 */ public void parseMessage(ByteBuffer socketbuffer) { int versionInfo = socketbuffer.readByte(); bool encryption = ((versionInfo & 8) != 0); bool crc = ((versionInfo & 4) != 0); bool compress = ((versionInfo & 2) != 0); //if(!MiniConnectManager.IsRobot) //MonoBehaviour.print ("length=>" + length + " versionInfo=>" + versionInfo + " encryption=>" + encryption + " crc=>" + crc + " compress=>" + compress); ByteBuffer data = new ByteBuffer(length - 1); data.write(socketbuffer.toArray(), 0, length - 1); //为下次数据处理做判断 if (socket.Available >= 2) { byte[] b = new byte[2]; socket.Receive(b, SocketFlags.None); length = ByteKit.readUnsignedShort(b, 0); } else { length = 0; } if (encryption) { data = encryptionCode(data, _receiveChallengeCode); } if (compress) { byte[] bb = ZIPUtil.Decompress(data.toArray()); data = new ByteBuffer(bb); } if (crc) { int crcValue = data.readInt(); ByteBuffer data1 = new ByteBuffer(); data1.writeBytes(data.toArray(), 0, (data.top - data.position)); int nowCrc = (int)ChecksumUtil.Adler32(data1); if (crcValue != nowCrc) { MonoBehaviour.print("crc is err,crcValue" + crcValue + ",nowCrc=" + nowCrc); return; } } ErlKVMessage message = new ErlKVMessage(null); message.bytesRead(data); if (_portHandler != null) // _portHandler可以是DataAccess或者ErlTransmitPort,如果要保存funcUid就要设置为DataAccess { _portHandler.erlReceive(this, message); } }
public void GetChecksum_ShouldReturnKnown() { using FileStream file = File.Create("checksum_temp"); try { byte[] fileContents = new byte[] { 12, 16, 32, 15, 123, 12, 254, 216, 10, 2, 2, 8 }; byte[] checksum = new byte[] { 210, 169, 152, 14, 138, 233, 2, 65, 240, 8, 94, 86, 215, 40, 74, 167, 23, 196, 23, 199, 203, 110, 90, 174, 241, 159, 155, 77, 51, 190, 185, 118 }; file.Write(fileContents, 0, fileContents.Length); file.Close(); Assert.True(ChecksumUtil.ValidateChecksum("checksum_temp", checksum)); } finally { File.Delete("checksum_temp"); } }
/** 发送方法 * @param data 数据 * @param isEncryption 是否加密 * @param isCrc 是否crc * @param isCompress 是否压缩 * @param kv kv类型 为0表示消息为二进制数据,为1表示消息为KeyValue类型,key为字符串,Value为标准格式的数据 * */ public void sendErl(ByteBuffer data, int encryption, int crc, int compress, int kv) { //没有得到pk码,一般出现在连接有,但是接不到后台消息 if (_sendChallengeCode == null || _sendChallengeCode.Length < 0) { return; } _encryption = encryption; _crc = crc; _compress = compress; _kv = kv; int crcValue = 0; ByteBuffer data1 = new ByteBuffer(); if (_compress == COMPRESS && data.length() >= 64) // 根据参数和数据长度判断是否执行压缩 { byte[] bb = ZIPUtil.Compress(data.toArray()); data = new ByteBuffer(bb); } else { _compress = 0; } if (_crc == 1 && _compress == 0) { crcValue = (int)ChecksumUtil.Adler32(data); data1.writeInt(crcValue); } else { _crc = 0; } data1.writeBytes(data.toArray()); if (_encryption == 1) { data1 = encryptionCode(data1, _sendChallengeCode); // 执行加密 } send(data1); _encryption = ENCRYPTION; _crc = CRC; _compress = COMPRESS; _kv = KV; }
private PsfPsyQAddresses getAdditionalDriverInfo(PsfPsyQAddresses existingValues, PsfStubMakerStruct stubMakerParameters, string driverPath) { string checksum; byte[] jumpAddress; PsfPsyQAddresses ret = existingValues; using (FileStream checksumStream = File.OpenRead(driverPath)) { checksum = ChecksumUtil.GetCrc32OfFullFile(checksumStream); jumpAddress = ParseFile.ParseSimpleOffset(checksumStream, 0x10, 0x04); } ret.DriverTextString = stubMakerParameters.DriverText; ret.ExeFileNameCrc = String.Format(" (int)\"{0}\", 0x{1},", Path.GetFileName(driverPath), checksum); ret.JumpPatchAddress = String.Format("0x{0}", BitConverter.ToUInt32(jumpAddress, 0).ToString("X8")); return(ret); }
protected CriAfs2Archive InitializeExternalAwbArchive() { CriAfs2Archive afs2 = null; string awbDirectory; string awbMask; string acbBaseFileName; string[] awbFiles; byte[] awbHashStored; byte[] awbHashCalculated; awbDirectory = Path.GetDirectoryName(this.SourceFile); // try format 1 acbBaseFileName = Path.GetFileNameWithoutExtension(this.SourceFile); awbMask = String.Format(AWB_FORMAT1, acbBaseFileName); awbFiles = Directory.GetFiles(awbDirectory, awbMask, SearchOption.TopDirectoryOnly); if (awbFiles.Length < 1) { // try format 2 awbMask = String.Format(AWB_FORMAT2, acbBaseFileName); awbFiles = Directory.GetFiles(awbDirectory, awbMask, SearchOption.TopDirectoryOnly); } if (awbFiles.Length < 1) { // try format 3 awbMask = String.Format(AWB_FORMAT3, acbBaseFileName); awbFiles = Directory.GetFiles(awbDirectory, awbMask, SearchOption.TopDirectoryOnly); } // file not found if (awbFiles.Length < 1) { throw new FileNotFoundException(String.Format("Cannot find AWB file. Please verify corresponding AWB file is named '{0}', '{1}', or '{2}'.", String.Format(AWB_FORMAT1, acbBaseFileName), String.Format(AWB_FORMAT2, acbBaseFileName), String.Format(AWB_FORMAT3, acbBaseFileName))); } if (awbFiles.Length > 1) { throw new FileNotFoundException(String.Format("More than one matching AWB file for this ACB. Please verify only one AWB file is named '{1}' or '{2}'.", String.Format(AWB_FORMAT1, acbBaseFileName), String.Format(AWB_FORMAT2, acbBaseFileName))); } // initialize AFS2 file using (FileStream fs = File.Open(awbFiles[0], FileMode.Open, FileAccess.Read, FileShare.Read)) { awbHashStored = this.StreamAwbHash; if (awbHashStored.Length == 0x10) // MD5, hash in newer format unknown { // validate MD5 checksum awbHashCalculated = ByteConversion.GetBytesFromHexString(ChecksumUtil.GetMd5OfFullFile(fs)); if (!ParseFile.CompareSegment(awbHashCalculated, 0, awbHashStored)) { throw new FormatException(String.Format("AWB file, <{0}>, did not match checksum inside ACB file.", Path.GetFileName(fs.Name))); } } afs2 = new CriAfs2Archive(fs, 0); } return(afs2); }
public bool ComputeIsChecksumValid() => ChecksumUtil.IsRomChecksumValid(Data.RomBytes.CreateListRawRomBytes(), Data.RomMapMode, GetRomSize());
// looks at the actual bytes present in the ROM and calculates their checksum // this is unrelated to any stored/cached checksums in the Project file. public ushort ComputeChecksum() => (ushort)ChecksumUtil.ComputeChecksumFromRom(Data.RomBytes.CreateListRawRomBytes());
private void rebuildFile(string pFilePath, RebuildSetsStruct pRebuildSetsStruct, AuditingUtil pAuditingUtil, DoWorkEventArgs ea) { ArrayList pDestinationFiles = new ArrayList(); bool isFileLibrary = false; bool hasMultipleExtensions = false; bool fileMovedOrScannedOk = true; Type formatType = null; if (FormatUtil.IsZipFile(pFilePath)) { string tempDir = System.IO.Path.GetTempPath() + Path.GetFileNameWithoutExtension(pFilePath); CompressionUtil.ExtractAllFilesFromZipFile(pFilePath, tempDir); tempDirsForDeletion.Add(tempDir); // Add in case of cancel button RebuildSetsStruct zipRebuildSetsStruct = pRebuildSetsStruct; zipRebuildSetsStruct.pSourceDir = tempDir; zipRebuildSetsStruct.totalFiles = Directory.GetFiles(tempDir, "*.*", SearchOption.AllDirectories).Length; maxFiles += zipRebuildSetsStruct.totalFiles; this.rebuildSets(zipRebuildSetsStruct, pAuditingUtil, 0, ea); if (!CancellationPending) { Directory.Delete(tempDir, true); tempDirsForDeletion.Remove(tempDir); } } else { using (FileStream fs = File.OpenRead(pFilePath)) { formatType = FormatUtil.getObjectType(fs); fs.Seek(0, SeekOrigin.Begin); // CRC32 string crc32Value = String.Empty; Crc32 crc32Generator = new Crc32(); // @TODO - Change to file streams? Out of memory errors on repeat runs /* * // MD5 * string md5FileName = Path.GetTempFileName(); * MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider(); * //MemoryStream md5MemoryStream = new MemoryStream(); * FileStream md5MemoryStream = new FileStream(md5FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); * CryptoStream md5CryptoStream = new CryptoStream(md5MemoryStream, md5Hash, CryptoStreamMode.Write); * * // SHA1 * string sha1FileName = Path.GetTempFileName(); * SHA1CryptoServiceProvider sha1Hash = new SHA1CryptoServiceProvider(); * //MemoryStream sha1MemoryStream = new MemoryStream(); * FileStream sha1MemoryStream = new FileStream(sha1FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); * CryptoStream sha1CryptoStream = new CryptoStream(sha1MemoryStream, sha1Hash, CryptoStreamMode.Write); */ if ((pAuditingUtil.HasVgmtSizeFlag) && (formatType != null)) { try { IFormat vgmData = (IFormat)Activator.CreateInstance(formatType); vgmData.Initialize(fs, pFilePath); isFileLibrary = vgmData.IsFileLibrary(); hasMultipleExtensions = vgmData.HasMultipleFileExtensions(); // vgmData.getDatFileCrc32(pFilePath, ref libHash, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream, false, pStreamInput); vgmData.GetDatFileCrc32(ref crc32Generator); vgmData = null; } catch (EndOfStreamException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } catch (System.OutOfMemoryException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } catch (IOException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } } else { // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } // @TODO Add MD5/SHA1 to make checksum hash correct String(CRC32 + MD5 + SHA1) crc32Value = crc32Generator.Value.ToString("X8"); pDestinationFiles = (ArrayList)pAuditingUtil.ChecksumHash[crc32Value]; if (pDestinationFiles != null) { if (!pRebuildSetsStruct.ScanOnly) { fileMovedOrScannedOk = this.moveFile(pRebuildSetsStruct.pDestinationDir, pDestinationFiles, fs, pFilePath, pRebuildSetsStruct.pOverwriteExisting, pRebuildSetsStruct.pCompressOutput, hasMultipleExtensions); } pAuditingUtil.AddChecksumToCache(crc32Value); } else { pAuditingUtil.AddUnknownFile(pFilePath); } } /* * md5CryptoStream.Close(); * md5CryptoStream.Dispose(); * sha1CryptoStream.Close(); * sha1CryptoStream.Dispose(); * * md5MemoryStream.Close(); * md5MemoryStream.Dispose(); * sha1MemoryStream.Close(); * sha1MemoryStream.Dispose(); * * File.Delete(md5FileName); * File.Delete(sha1FileName); */ // Remove Source only if copied if (fileMovedOrScannedOk && pDestinationFiles != null && pRebuildSetsStruct.pRemoveSource && File.Exists(pFilePath)) { if (!isFileLibrary) { File.Delete(pFilePath); } else // Add to List for deletion later { libFilesForDeletion.Add(pFilePath); } } } // (FormatUtil.IsZipFile(pFilePath)) }
public void CheckSumString_ShouldReturnKnown() { byte[] checksum = new byte[] { 210, 169, 152, 14, 138, 233, 2, 65, 240, 8, 94, 86, 215, 40, 74, 167, 23, 196, 23, 199, 203, 110, 90, 174, 241, 159, 155, 77, 51, 190, 185, 118 }; Assert.Equal("D2A9980E8AE90241F0085E56D7284AA717C417C7CB6E5AAEF19F9B4D33BEB976", ChecksumUtil.ChecksumToString(checksum)); }
protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pExamineChecksumGeneratorStruct, DoWorkEventArgs e) { ExamineChecksumGeneratorStruct examineChecksumGeneratorStruct = (ExamineChecksumGeneratorStruct)pExamineChecksumGeneratorStruct; string crc32; string md5; string sha1; string vgmtCrc32 = "Not implemented for this format."; string vgmtMd5 = "Not implemented for this format."; string vgmtSha1 = "Not implemented for this format."; string checksumKey; Type formatType = null; IFormat vgmData = null; using (FileStream fs = File.OpenRead(pPath)) { crc32 = ChecksumUtil.GetCrc32OfFullFile(fs); md5 = ChecksumUtil.GetMd5OfFullFile(fs); sha1 = ChecksumUtil.GetSha1OfFullFile(fs); if (examineChecksumGeneratorStruct.CheckForDuplicates) { checksumKey = String.Format("{0}/{1}/{2}", crc32, md5, sha1); this.addChecksumToHash(checksumKey, pPath, true); } if (examineChecksumGeneratorStruct.DoVgmtChecksums) { formatType = FormatUtil.getObjectType(fs); if (formatType != null) { vgmData = (IFormat)Activator.CreateInstance(formatType); vgmData.Initialize(fs, pPath); } } } if (vgmData != null) { Crc32 crc32Generator = new Crc32(); MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider(); FileStream md5FileStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write); CryptoStream md5CryptoStream = new CryptoStream(md5FileStream, md5Hash, CryptoStreamMode.Write); SHA1CryptoServiceProvider sha1Hash = new SHA1CryptoServiceProvider(); FileStream sha1FileStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write); CryptoStream sha1CryptoStream = new CryptoStream(sha1FileStream, sha1Hash, CryptoStreamMode.Write); vgmData.GetDatFileChecksums(ref crc32Generator, ref md5CryptoStream, ref sha1CryptoStream); md5CryptoStream.FlushFinalBlock(); sha1CryptoStream.FlushFinalBlock(); vgmtCrc32 = crc32Generator.Value.ToString("X8"); vgmtMd5 = ParseFile.ByteArrayToString(md5Hash.Hash); vgmtSha1 = ParseFile.ByteArrayToString(sha1Hash.Hash); if (examineChecksumGeneratorStruct.CheckForDuplicates) { checksumKey = String.Format("{0}/{1}/{2}", vgmtCrc32, vgmtMd5, vgmtSha1); this.addChecksumToHash(checksumKey, pPath, false); } md5FileStream.Close(); md5FileStream.Dispose(); sha1FileStream.Close(); sha1FileStream.Dispose(); md5CryptoStream.Close(); md5CryptoStream.Dispose(); sha1CryptoStream.Close(); sha1CryptoStream.Dispose(); } this.outputBuffer.AppendFormat("<{0}>{1}", pPath, Environment.NewLine); this.outputBuffer.AppendFormat("CRC32: {0}{1}", crc32, Environment.NewLine); this.outputBuffer.AppendFormat("MD5: {0}{1}", md5, Environment.NewLine); this.outputBuffer.AppendFormat("SHA1: {0}{1}", sha1, Environment.NewLine); if (examineChecksumGeneratorStruct.DoVgmtChecksums) { this.outputBuffer.AppendFormat("CRC32 (VGMT): {0}{1}", vgmtCrc32, Environment.NewLine); this.outputBuffer.AppendFormat("MD5 (VGMT): {0}{1}", vgmtMd5, Environment.NewLine); this.outputBuffer.AppendFormat("SHA1 (VGMT): {0}{1}", vgmtSha1, Environment.NewLine); } this.outputBuffer.AppendLine(); }