public bool?VerifyMediaImage() { byte[] calculated; if (mapVersion >= 3) { Sha1Context sha1Ctx = new Sha1Context(); for (uint i = 0; i < totalHunks; i++) { sha1Ctx.Update(GetHunk(i)); } calculated = sha1Ctx.Final(); } else { Md5Context md5Ctx = new Md5Context(); for (uint i = 0; i < totalHunks; i++) { md5Ctx.Update(GetHunk(i)); } calculated = md5Ctx.Final(); } return(expectedChecksum.SequenceEqual(calculated)); }
public void Hashes() { Environment.CurrentDirectory = DataFolder; Assert.Multiple(() => { foreach (TapeImageTestExpected test in Tests) { string testFile = test.TestFile; bool exists = File.Exists(testFile); Assert.True(exists, $"{testFile} not found"); // ReSharper disable once ConditionIsAlwaysTrueOrFalse // It arrives here... if (!exists) { continue; } var filtersList = new FiltersList(); IFilter filter = filtersList.GetFilter(testFile); filter.Open(testFile); var image = Activator.CreateInstance(_plugin.GetType()) as IMediaImage; Assert.NotNull(image, $"Could not instantiate filesystem for {testFile}"); bool opened = image.Open(filter); Assert.AreEqual(true, opened, $"Open: {testFile}"); if (!opened) { continue; } ulong doneSectors = 0; var ctx = new Md5Context(); while (doneSectors < image.Info.Sectors) { byte[] sector; if (image.Info.Sectors - doneSectors >= SECTORS_TO_READ) { sector = image.ReadSectors(doneSectors, SECTORS_TO_READ); doneSectors += SECTORS_TO_READ; } else { sector = image.ReadSectors(doneSectors, (uint)(image.Info.Sectors - doneSectors)); doneSectors += image.Info.Sectors - doneSectors; } ctx.Update(sector); } Assert.AreEqual(test.MD5, ctx.End(), $"Hash: {testFile}"); } }); }
public void Md5EmptyInstance() { byte[] data = new byte[1048576]; FileStream fs = new FileStream(Path.Combine(Consts.TestFilesRoot, "checksums", "empty"), FileMode.Open, FileAccess.Read); fs.Read(data, 0, 1048576); fs.Close(); fs.Dispose(); IChecksum ctx = new Md5Context(); ctx.Update(data); byte[] result = ctx.Final(); Assert.AreEqual(ExpectedEmpty, result); }
public void Md5RandomInstance() { byte[] data = new byte[1048576]; var fs = new FileStream(Path.Combine(Consts.TEST_FILES_ROOT, "Checksum test files", "random"), FileMode.Open, FileAccess.Read); fs.Read(data, 0, 1048576); fs.Close(); fs.Dispose(); IChecksum ctx = new Md5Context(); ctx.Update(data); byte[] result = ctx.Final(); Assert.AreEqual(_expectedRandom, result); }
public void EmptyInstance() { byte[] data = new byte[1048576]; var fs = new FileStream(Path.Combine(Consts.TEST_FILES_ROOT, "Checksum test files", "empty"), FileMode.Open, FileAccess.Read); fs.Read(data, 0, 1048576); fs.Close(); fs.Dispose(); IChecksum ctx = new Md5Context(); ctx.Update(data); byte[] result = ctx.Final(); result.Should().BeEquivalentTo(_expectedEmpty); }
public void Test() { for (int i = 0; i < _testFiles.Length; i++) { string location = Path.Combine(Consts.TEST_FILES_ROOT, "Media image formats", "DiskCopy 4.2", _testFiles[i]); IFilter filter = new LZip(); filter.Open(location); IMediaImage image = new DiscImages.DiskCopy42(); Assert.AreEqual(true, image.Open(filter), _testFiles[i]); Assert.AreEqual(_sectors[i], image.Info.Sectors, _testFiles[i]); Assert.AreEqual(_sectorSize[i], image.Info.SectorSize, _testFiles[i]); Assert.AreEqual(_mediaTypes[i], image.Info.MediaType, _testFiles[i]); // How many sectors to read at once const uint sectorsToRead = 256; ulong doneSectors = 0; var ctx = new Md5Context(); while (doneSectors < image.Info.Sectors) { byte[] sector; if (image.Info.Sectors - doneSectors >= sectorsToRead) { sector = image.ReadSectors(doneSectors, sectorsToRead); doneSectors += sectorsToRead; } else { sector = image.ReadSectors(doneSectors, (uint)(image.Info.Sectors - doneSectors)); doneSectors += image.Info.Sectors - doneSectors; } ctx.Update(sector); } Assert.AreEqual(_md5S[i], ctx.End(), _testFiles[i]); } }
public void Test() { for (int i = 0; i < testfiles.Length; i++) { string location = Path.Combine(Consts.TestFilesRoot, "images", "2mg", testfiles[i]); IFilter filter = new LZip(); filter.Open(location); IMediaImage image = new DiscImages.Apple2Mg(); Assert.AreEqual(true, image.Open(filter), testfiles[i]); Assert.AreEqual(sectors[i], image.Info.Sectors, testfiles[i]); Assert.AreEqual(sectorsize[i], image.Info.SectorSize, testfiles[i]); Assert.AreEqual(mediatypes[i], image.Info.MediaType, testfiles[i]); // How many sectors to read at once const uint SECTORS_TO_READ = 256; ulong doneSectors = 0; Md5Context ctx = new Md5Context(); while (doneSectors < image.Info.Sectors) { byte[] sector; if (image.Info.Sectors - doneSectors >= SECTORS_TO_READ) { sector = image.ReadSectors(doneSectors, SECTORS_TO_READ); doneSectors += SECTORS_TO_READ; } else { sector = image.ReadSectors(doneSectors, (uint)(image.Info.Sectors - doneSectors)); doneSectors += image.Info.Sectors - doneSectors; } ctx.Update(sector); } Assert.AreEqual(md5S[i], ctx.End(), testfiles[i]); } }
public void Test() { Environment.CurrentDirectory = Path.Combine(Consts.TEST_FILES_ROOT, "Media image formats", "CDRWin"); for (int i = 0; i < _testFiles.Length; i++) { IFilter filter = new ZZZNoFilter(); filter.Open(_testFiles[i]); IOpticalMediaImage image = new CdrWin(); Assert.AreEqual(true, image.Open(filter), _testFiles[i]); Assert.AreEqual(_sectors[i], image.Info.Sectors, _testFiles[i]); Assert.AreEqual(_mediaTypes[i], image.Info.MediaType, _testFiles[i]); Assert.AreEqual(_tracks[i], image.Tracks.Count, _testFiles[i]); // How many sectors to read at once const uint sectorsToRead = 256; int trackNo = 0; foreach (Track currentTrack in image.Tracks) { Assert.AreEqual(_trackSessions[i][trackNo], currentTrack.TrackSession, _testFiles[i]); Assert.AreEqual(_trackStarts[i][trackNo], currentTrack.TrackStartSector, _testFiles[i]); Assert.AreEqual(_trackEnds[i][trackNo], currentTrack.TrackEndSector, _testFiles[i]); Assert.AreEqual(_trackPregaps[i][trackNo], currentTrack.TrackPregap, _testFiles[i]); if (image.Info.ReadableSectorTags.Contains(SectorTagType.CdTrackFlags)) { Assert.AreEqual(_trackFlags[i][trackNo], image.ReadSectorTag(currentTrack.TrackSequence, SectorTagType.CdTrackFlags)[0], _testFiles[i]); } trackNo++; } foreach (bool @long in new[] { false, true }) { var ctx = new Md5Context(); foreach (Track currentTrack in image.Tracks) { ulong sectors = (currentTrack.TrackEndSector - currentTrack.TrackStartSector) + 1; ulong doneSectors = 0; while (doneSectors < sectors) { byte[] sector; if (sectors - doneSectors >= sectorsToRead) { sector = @long ? image.ReadSectorsLong(doneSectors, sectorsToRead, currentTrack.TrackSequence) : image.ReadSectors(doneSectors, sectorsToRead, currentTrack.TrackSequence); doneSectors += sectorsToRead; } else { sector = @long ? image.ReadSectorsLong(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence) : image.ReadSectors(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence); doneSectors += sectors - doneSectors; } ctx.Update(sector); } } Assert.AreEqual(@long ? _longMd5S[i] : _md5S[i], ctx.End(), _testFiles[i]); } if (image.Info.ReadableSectorTags.Contains(SectorTagType.CdSectorSubchannel)) { var ctx = new Md5Context(); foreach (Track currentTrack in image.Tracks) { ulong sectors = (currentTrack.TrackEndSector - currentTrack.TrackStartSector) + 1; ulong doneSectors = 0; while (doneSectors < sectors) { byte[] sector; if (sectors - doneSectors >= sectorsToRead) { sector = image.ReadSectorsTag(doneSectors, sectorsToRead, currentTrack.TrackSequence, SectorTagType.CdSectorSubchannel); doneSectors += sectorsToRead; } else { sector = image.ReadSectorsTag(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence, SectorTagType.CdSectorSubchannel); doneSectors += sectors - doneSectors; } ctx.Update(sector); } } Assert.AreEqual(_subchannelMd5S[i], ctx.End(), _testFiles[i]); } } }
public void Test() { // How many sectors to read at once const uint sectorsToRead = 256; Environment.CurrentDirectory = Path.Combine(Consts.TEST_FILES_ROOT, "Media image formats", "MagicISO", "Cuesheet"); IFilter[] filters = new IFilter[_testFiles.Length]; for (int i = 0; i < _testFiles.Length; i++) { filters[i] = new ZZZNoFilter(); filters[i].Open(_testFiles[i]); } IOpticalMediaImage[] images = new IOpticalMediaImage[_testFiles.Length]; for (int i = 0; i < _testFiles.Length; i++) { images[i] = new CdrWin(); Assert.AreEqual(true, images[i].Open(filters[i]), $"Open: {_testFiles[i]}"); } for (int i = 0; i < _testFiles.Length; i++) { Assert.AreEqual(_sectors[i], images[i].Info.Sectors, $"Sectors: {_testFiles[i]}"); } for (int i = 0; i < _testFiles.Length; i++) { Assert.AreEqual(_mediaTypes[i], images[i].Info.MediaType, $"Media type: {_testFiles[i]}"); } for (int i = 0; i < _testFiles.Length; i++) { Assert.AreEqual(_tracks[i], images[i].Tracks.Count, $"Tracks: {_testFiles[i]}"); } for (int i = 0; i < _testFiles.Length; i++) { images[i].Tracks.Select(t => t.TrackSession).Should(). BeEquivalentTo(_trackSessions[i], $"Track session: {_testFiles[i]}"); } for (int i = 0; i < _testFiles.Length; i++) { images[i].Tracks.Select(t => t.TrackStartSector).Should(). BeEquivalentTo(_trackStarts[i], $"Track start: {_testFiles[i]}"); } for (int i = 0; i < _testFiles.Length; i++) { images[i].Tracks.Select(t => t.TrackEndSector).Should(). BeEquivalentTo(_trackEnds[i], $"Track end: {_testFiles[i]}"); } for (int i = 0; i < _testFiles.Length; i++) { images[i].Tracks.Select(t => t.TrackPregap).Should(). BeEquivalentTo(_trackPregaps[i], $"Track pregap: {_testFiles[i]}"); } for (int i = 0; i < _testFiles.Length; i++) { int trackNo = 0; foreach (Track currentTrack in images[i].Tracks) { if (images[i].Info.ReadableSectorTags.Contains(SectorTagType.CdTrackFlags)) { Assert.AreEqual(_trackFlags[i][trackNo], images[i].ReadSectorTag(currentTrack.TrackSequence, SectorTagType.CdTrackFlags) [0], $"Track flags: {_testFiles[i]}, track {currentTrack.TrackSequence}"); } trackNo++; } } foreach (bool @long in new[] { false, true }) { for (int i = 0; i < _testFiles.Length; i++) { var ctx = new Md5Context(); foreach (Track currentTrack in images[i].Tracks) { ulong sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1; ulong doneSectors = 0; while (doneSectors < sectors) { byte[] sector; if (sectors - doneSectors >= sectorsToRead) { sector = @long ? images[i]. ReadSectorsLong(doneSectors, sectorsToRead, currentTrack.TrackSequence) : images[i]. ReadSectors(doneSectors, sectorsToRead, currentTrack.TrackSequence); doneSectors += sectorsToRead; } else { sector = @long ? images[i].ReadSectorsLong(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence) : images[i].ReadSectors(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence); doneSectors += sectors - doneSectors; } ctx.Update(sector); } } Assert.AreEqual(@long ? _longMd5S[i] : _md5S[i], ctx.End(), $"{(@long ? "Long hash" : "Hash")}: {_testFiles[i]}"); } } for (int i = 0; i < _testFiles.Length; i++) { if (images[i].Info.ReadableSectorTags.Contains(SectorTagType.CdSectorSubchannel)) { var ctx = new Md5Context(); foreach (Track currentTrack in images[i].Tracks) { ulong sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1; ulong doneSectors = 0; while (doneSectors < sectors) { byte[] sector; if (sectors - doneSectors >= sectorsToRead) { sector = images[i].ReadSectorsTag(doneSectors, sectorsToRead, currentTrack.TrackSequence, SectorTagType.CdSectorSubchannel); doneSectors += sectorsToRead; } else { sector = images[i].ReadSectorsTag(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence, SectorTagType.CdSectorSubchannel); doneSectors += sectors - doneSectors; } ctx.Update(sector); } } Assert.AreEqual(_subchannelMd5S[i], ctx.End(), $"Subchannel hash: {_testFiles[i]}"); } } }
public bool?VerifyMediaImage() { if (_discImage.DiscHashes.Count == 0) { return(null); } // Read up to 1 MiB at a time for verification const int verifySize = 1024 * 1024; long readBytes; byte[] verifyBytes; IFilter[] filters = _discImage.Tracks.OrderBy(t => t.Sequence).Select(t => t.TrackFile.DataFilter). Distinct().ToArray(); if (_discImage.DiscHashes.TryGetValue("sha1", out string sha1)) { var ctx = new Sha1Context(); foreach (IFilter filter in filters) { Stream stream = filter.GetDataForkStream(); readBytes = 0; verifyBytes = new byte[verifySize]; while (readBytes + verifySize < stream.Length) { stream.Read(verifyBytes, 0, verifyBytes.Length); ctx.Update(verifyBytes); readBytes += verifyBytes.LongLength; } verifyBytes = new byte[stream.Length - readBytes]; stream.Read(verifyBytes, 0, verifyBytes.Length); ctx.Update(verifyBytes); } string verifySha1 = ctx.End(); AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated SHA1: {0}", verifySha1); AaruConsole.DebugWriteLine("CDRWin plugin", "Expected SHA1: {0}", sha1); return(verifySha1 == sha1); } if (_discImage.DiscHashes.TryGetValue("md5", out string md5)) { var ctx = new Md5Context(); foreach (IFilter filter in filters) { Stream stream = filter.GetDataForkStream(); readBytes = 0; verifyBytes = new byte[verifySize]; while (readBytes + verifySize < stream.Length) { stream.Read(verifyBytes, 0, verifyBytes.Length); ctx.Update(verifyBytes); readBytes += verifyBytes.LongLength; } verifyBytes = new byte[stream.Length - readBytes]; stream.Read(verifyBytes, 0, verifyBytes.Length); ctx.Update(verifyBytes); } string verifyMd5 = ctx.End(); AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated MD5: {0}", verifyMd5); AaruConsole.DebugWriteLine("CDRWin plugin", "Expected MD5: {0}", md5); return(verifyMd5 == md5); } if (_discImage.DiscHashes.TryGetValue("crc32", out string crc32)) { var ctx = new Crc32Context(); foreach (IFilter filter in filters) { Stream stream = filter.GetDataForkStream(); readBytes = 0; verifyBytes = new byte[verifySize]; while (readBytes + verifySize < stream.Length) { stream.Read(verifyBytes, 0, verifyBytes.Length); ctx.Update(verifyBytes); readBytes += verifyBytes.LongLength; } verifyBytes = new byte[stream.Length - readBytes]; stream.Read(verifyBytes, 0, verifyBytes.Length); ctx.Update(verifyBytes); } string verifyCrc = ctx.End(); AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated CRC32: {0}", verifyCrc); AaruConsole.DebugWriteLine("CDRWin plugin", "Expected CRC32: {0}", crc32); return(verifyCrc == crc32); } foreach (string hash in _discImage.DiscHashes.Keys) { AaruConsole.DebugWriteLine("CDRWin plugin", "Found unsupported hash {0}", hash); } return(null); }
public static BenchmarkResults Do(int bufferSize, int blockSize) { BenchmarkResults results = new BenchmarkResults { Entries = new Dictionary <string, BenchmarkEntry>(), MinMemory = long.MaxValue, MaxMemory = 0, SeparateTime = 0 }; MemoryStream ms = new MemoryStream(bufferSize); Random rnd = new Random(); DateTime start; DateTime end; start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Writing block {0} of {1} with random data.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; rnd.NextBytes(tmp); ms.Write(tmp, 0, blockSize); } EndProgress(); end = DateTime.Now; results.FillTime = (end - start).TotalSeconds; results.FillSpeed = bufferSize / 1048576.0 / (end - start).TotalSeconds; ms.Seek(0, SeekOrigin.Begin); long mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Reading block {0} of {1}.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); } EndProgress(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.ReadTime = (end - start).TotalSeconds; results.ReadSpeed = bufferSize / 1048576.0 / (end - start).TotalSeconds; #region Adler32 IChecksum ctx = new Adler32Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with Adler32.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("Adler32", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion Adler32 #region Fletcher16 ctx = new Fletcher16Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with Fletcher-16.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("Fletcher16", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion Fletcher16 #region Fletcher32 ctx = new Fletcher32Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with Fletcher-32.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("Fletcher32", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion Fletcher32 #region CRC16 ctx = new Crc16Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with CRC16.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("CRC16", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion CRC16 #region CRC32 ctx = new Crc32Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with CRC32.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("CRC32", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion CRC32 #region CRC64 ctx = new Crc64Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with CRC64.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("CRC64", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion CRC64 #region MD5 ctx = new Md5Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with MD5.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("MD5", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion MD5 #if !NETSTANDARD2_0 #region RIPEMD160 ctx = new Ripemd160Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with RIPEMD160.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("RIPEMD160", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion RIPEMD160 #endif #region SHA1 ctx = new Sha1Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with SHA1.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("SHA1", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion SHA1 #region SHA256 ctx = new Sha256Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with SHA256.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("SHA256", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion SHA256 #region SHA384 ctx = new Sha384Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with SHA384.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("SHA384", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion SHA384 #region SHA512 ctx = new Sha512Context(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with SHA512.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("SHA512", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion SHA512 #region SpamSum ctx = new SpamSumContext(); ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Checksumming block {0} of {1} with SpamSum.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); ctx.Update(tmp); } EndProgress(); ctx.End(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.Entries.Add("SpamSum", new BenchmarkEntry { TimeSpan = (end - start).TotalSeconds, Speed = bufferSize / 1048576.0 / (end - start).TotalSeconds }); results.SeparateTime += (end - start).TotalSeconds; #endregion SpamSum #region Entropy ulong[] entTable = new ulong[256]; ms.Seek(0, SeekOrigin.Begin); mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } start = DateTime.Now; InitProgress(); for (int i = 0; i < bufferSize / blockSize; i++) { UpdateProgress("Entropying block {0} of {1}.", i + 1, bufferSize / blockSize); byte[] tmp = new byte[blockSize]; ms.Read(tmp, 0, blockSize); foreach (byte b in tmp) { entTable[b]++; } } EndProgress(); end = DateTime.Now; mem = GC.GetTotalMemory(false); if (mem > results.MaxMemory) { results.MaxMemory = mem; } if (mem < results.MinMemory) { results.MinMemory = mem; } results.EntropyTime = (end - start).TotalSeconds; results.EntropySpeed = bufferSize / 1048576.0 / (end - start).TotalSeconds; #endregion Entropy /* #region Multitasking * start = DateTime.Now; * Checksum allChecksums = new Checksum(); * InitProgress(); * for(int i = 0; i < bufferSize / blockSize; i++) * { * UpdateProgress("Checksumming block {0} of {1} with all algorithms at the same time.", i + 1, * bufferSize / blockSize); * byte[] tmp = new byte[blockSize]; * ms.Read(tmp, 0, blockSize); * * allChecksums.Update(tmp); * } * * EndProgress(); * * allChecksums.End(); * end = DateTime.Now; * mem = GC.GetTotalMemory(false); * if(mem > results.MaxMemory) results.MaxMemory = mem; * if(mem < results.MinMemory) results.MinMemory = mem; * * results.TotalTime = (end - start).TotalSeconds; * results.TotalSpeed = bufferSize / 1048576.0 / results.TotalTime; #endregion */ results.SeparateSpeed = bufferSize / 1048576.0 / results.SeparateTime; return(results); }
public void Hashes() { // How many sectors to read at once const uint sectorsToRead = 256; Environment.CurrentDirectory = Path.Combine(Consts.TEST_FILES_ROOT, "Media image formats", "UltraISO", "Alcohol"); Assert.Multiple(() => { for (int i = 0; i < _testFiles.Length; i++) { var filter = new ZZZNoFilter(); filter.Open(_testFiles[i]); var image = new DiscImages.Alcohol120(); bool opened = image.Open(filter); Assert.AreEqual(true, opened, $"Open: {_testFiles[i]}"); Md5Context ctx; foreach (bool @long in new[] { false, true }) { ctx = new Md5Context(); foreach (Track currentTrack in image.Tracks) { ulong sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1; ulong doneSectors = 0; while (doneSectors < sectors) { byte[] sector; if (sectors - doneSectors >= sectorsToRead) { sector = @long ? image.ReadSectorsLong(doneSectors, sectorsToRead, currentTrack.TrackSequence) : image.ReadSectors(doneSectors, sectorsToRead, currentTrack.TrackSequence); doneSectors += sectorsToRead; } else { sector = @long ? image.ReadSectorsLong(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence) : image.ReadSectors(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence); doneSectors += sectors - doneSectors; } ctx.Update(sector); } } Assert.AreEqual(@long ? _longMd5S[i] : _md5S[i], ctx.End(), $"{(@long ? "Long hash" : "Hash")}: {_testFiles[i]}"); } if (!image.Info.ReadableSectorTags.Contains(SectorTagType.CdSectorSubchannel)) { continue; } ctx = new Md5Context(); foreach (Track currentTrack in image.Tracks) { ulong sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1; ulong doneSectors = 0; while (doneSectors < sectors) { byte[] sector; if (sectors - doneSectors >= sectorsToRead) { sector = image.ReadSectorsTag(doneSectors, sectorsToRead, currentTrack.TrackSequence, SectorTagType.CdSectorSubchannel); doneSectors += sectorsToRead; } else { sector = image.ReadSectorsTag(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence, SectorTagType.CdSectorSubchannel); doneSectors += sectors - doneSectors; } ctx.Update(sector); } } Assert.AreEqual(_subchannelMd5S[i], ctx.End(), $"Subchannel hash: {_testFiles[i]}"); } }); }
public void Hashes() { Environment.CurrentDirectory = Environment.CurrentDirectory = DataFolder; Assert.Multiple(() => { Parallel.For(0L, Tests.Length, (i, state) => { string testFile = Tests[i].TestFile; bool exists = File.Exists(testFile); Assert.True(exists, $"{testFile} not found"); // ReSharper disable once ConditionIsAlwaysTrueOrFalse // It arrives here... if (!exists) { return; } var filtersList = new FiltersList(); IFilter filter = filtersList.GetFilter(testFile); filter.Open(testFile); var image = Activator.CreateInstance(_plugin.GetType()) as IOpticalMediaImage; Assert.NotNull(image, $"Could not instantiate filesystem for {testFile}"); bool opened = image.Open(filter); Assert.AreEqual(true, opened, $"Open: {testFile}"); if (!opened) { return; } Md5Context ctx; if (image.Info.XmlMediaType == XmlMediaType.OpticalDisc) { foreach (bool @long in new[] { false, true }) { ctx = new Md5Context(); foreach (Track currentTrack in image.Tracks) { ulong sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1; ulong doneSectors = 0; while (doneSectors < sectors) { byte[] sector; if (sectors - doneSectors >= SECTORS_TO_READ) { sector = @long ? image.ReadSectorsLong(doneSectors, SECTORS_TO_READ, currentTrack.TrackSequence) : image.ReadSectors(doneSectors, SECTORS_TO_READ, currentTrack.TrackSequence); doneSectors += SECTORS_TO_READ; } else { sector = @long ? image.ReadSectorsLong(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence) : image.ReadSectors(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence); doneSectors += sectors - doneSectors; } ctx.Update(sector); } } Assert.AreEqual(@long ? Tests[i].LongMD5 : Tests[i].MD5, ctx.End(), $"{(@long ? "Long hash" : "Hash")}: {testFile}"); } if (!image.Info.ReadableSectorTags.Contains(SectorTagType.CdSectorSubchannel)) { return; } ctx = new Md5Context(); foreach (Track currentTrack in image.Tracks) { ulong sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1; ulong doneSectors = 0; while (doneSectors < sectors) { byte[] sector; if (sectors - doneSectors >= SECTORS_TO_READ) { sector = image.ReadSectorsTag(doneSectors, SECTORS_TO_READ, currentTrack.TrackSequence, SectorTagType.CdSectorSubchannel); doneSectors += SECTORS_TO_READ; } else { sector = image.ReadSectorsTag(doneSectors, (uint)(sectors - doneSectors), currentTrack.TrackSequence, SectorTagType.CdSectorSubchannel); doneSectors += sectors - doneSectors; } ctx.Update(sector); } } Assert.AreEqual(Tests[i].SubchannelMD5, ctx.End(), $"Subchannel hash: {testFile}"); } else { ctx = new Md5Context(); ulong doneSectors = 0; while (doneSectors < image.Info.Sectors) { byte[] sector; if (image.Info.Sectors - doneSectors >= SECTORS_TO_READ) { sector = image.ReadSectors(doneSectors, SECTORS_TO_READ); doneSectors += SECTORS_TO_READ; } else { sector = image.ReadSectors(doneSectors, (uint)(image.Info.Sectors - doneSectors)); doneSectors += image.Info.Sectors - doneSectors; } ctx.Update(sector); } Assert.AreEqual(Tests[i].MD5, ctx.End(), $"Hash: {testFile}"); } }); }); }