public static int CheckSumRead(string filename, bool testDeep, out byte[] crc, out byte[] bMD5, out byte[] bSHA1) { bMD5 = null; bSHA1 = null; crc = null; Stream ds=null; CRC32Hash crc32 = new CRC32Hash(); MD5 md5 = null; if (testDeep) md5 = MD5.Create(); SHA1 sha1 = null; if (testDeep) sha1 = SHA1.Create(); try { int errorCode = IO.FileStream.OpenFileRead(filename, out ds); if (errorCode != 0) return errorCode; long sizetogo = ds.Length; while (sizetogo > 0) { int sizenow = sizetogo > Buffersize ? Buffersize : (int)sizetogo; ds.Read(Buffer, 0, sizenow); crc32.TransformBlock(Buffer, 0, sizenow, null, 0); if (testDeep) md5.TransformBlock(Buffer, 0, sizenow, null, 0); if (testDeep) sha1.TransformBlock(Buffer, 0, sizenow, null, 0); sizetogo -= sizenow; } crc32.TransformFinalBlock(Buffer, 0, 0); if (testDeep) md5.TransformFinalBlock(Buffer, 0, 0); if (testDeep) sha1.TransformFinalBlock(Buffer, 0, 0); ds.Close(); } catch { if (ds != null) ds.Close(); return 0x17; } crc = crc32.Hash; if (testDeep) bMD5 = md5.Hash; if (testDeep) bSHA1 = sha1.Hash; return 0; }
public void LocalFileCheck() { if (FileStatus != ZipReturn.ZipUntested) return; try { Stream sInput = null; _zipFs.Seek((long)_dataLocation, SeekOrigin.Begin); switch (_compressionMethod) { case 8: sInput = new DeflateStream(_zipFs, CompressionMode.Decompress, true); break; case 0: sInput = _zipFs; break; } if (sInput == null) { FileStatus = ZipReturn.ZipErrorGettingDataStream; return; } CRC32Hash crc32 = new CRC32Hash(); MD5 lmd5 = System.Security.Cryptography.MD5.Create(); SHA1 lsha1 = System.Security.Cryptography.SHA1.Create(); ulong sizetogo = UncompressedSize; if (_buffer == null) _buffer = new byte[Buffersize]; while (sizetogo > 0) { int sizenow = sizetogo > Buffersize ? Buffersize : (int)sizetogo; sInput.Read(_buffer, 0, sizenow); crc32.TransformBlock(_buffer, 0, sizenow, null, 0); lmd5.TransformBlock(_buffer, 0, sizenow, null, 0); lsha1.TransformBlock(_buffer, 0, sizenow, null, 0); sizetogo = sizetogo - (ulong)sizenow; } crc32.TransformFinalBlock(_buffer, 0, 0); lmd5.TransformFinalBlock(_buffer, 0, 0); lsha1.TransformFinalBlock(_buffer, 0, 0); byte[] testcrc = crc32.Hash; md5 = lmd5.Hash; sha1 = lsha1.Hash; if (_compressionMethod == 8) { sInput.Close(); sInput.Dispose(); } FileStatus = ByteArrCompare(CRC, testcrc) ? ZipReturn.ZipGood : ZipReturn.ZipCRCDecodeError; } catch { FileStatus = ZipReturn.ZipDecodeError; } }