public static string GetChecksum(this TypeDefinition type, TextualRepresentationOptions options) { uint lastHash = 0; StringBuilder checksumBuilder = new StringBuilder(2 * ChecksumThreshold); Action <string> appendAction = (s) => { checksumBuilder.Append(s); if (checksumBuilder.Length > ChecksumThreshold) { byte[] bytes = Encoding.ASCII.GetBytes(checksumBuilder.ToString()); lastHash = Crc32CAlgorithm.Append(lastHash, bytes); checksumBuilder.Clear(); } }; type.GetTextualRepresentation(appendAction, options); // clear if something left in StringBuilder if (checksumBuilder.Length > 0) { byte[] bytes = Encoding.ASCII.GetBytes(checksumBuilder.ToString()); lastHash = Crc32CAlgorithm.Append(lastHash, bytes); checksumBuilder.Clear(); } return(lastHash.ToString()); }
public void MethodConsistency(string text, int offset, int tail, int split) { var checksums = new List <uint>(); var array = Encoding.ASCII.GetBytes(text); var padded = Enumerable.Repeat <byte>(17, offset).Concat(array).Concat(Enumerable.Repeat <byte>(93, tail)).ToArray(); var half1 = array.Take(split).ToArray(); var half2 = array.Skip(split).ToArray(); checksums.Add(Crc32CAlgorithm.Compute(array)); checksums.Add(Crc32CAlgorithm.Compute(padded, offset, array.Length)); checksums.Add(Crc32CAlgorithm.Append(0, array)); checksums.Add(Crc32CAlgorithm.Append(0, padded, offset, array.Length)); checksums.Add(Crc32CAlgorithm.Append(Crc32CAlgorithm.Append(0, half1), half2)); checksums.Add(Crc32CAlgorithm.Append(Crc32CAlgorithm.Append(0, padded, offset, split), padded, offset + split, array.Length - split)); using (var hash = new Crc32CAlgorithm()) checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(array), 0)); using (var hash = new Crc32CAlgorithm()) checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(padded, offset, array.Length), 0)); using (var stream = new MemoryStream(array)) using (var hash = new Crc32CAlgorithm()) checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(stream), 0)); if (text.Length == 0) { Assert.AreEqual(0, checksums[0]); } foreach (var checksum in checksums) { Assert.AreEqual(checksums[0], checksum); } }
public static uint GetCrc32FromFile(string fileName) { try { using (FileStream file = new FileStream(fileName, FileMode.Open)) { const int NumBytes = 10000; var bytes = new byte[NumBytes]; var numRead = file.Read(bytes, 0, NumBytes); if (numRead == 0) { return(0); } var crc = 0u; while (numRead > 0) { Crc32CAlgorithm.Append(crc, bytes, 0, numRead); numRead = file.Read(bytes, 0, NumBytes); } return(crc); } } catch (Exception ex) when(ex is UnauthorizedAccessException || ex is IOException) { return(0); } }
/// <summary> /// Write a single record. /// </summary> /// <param name="stream">Stream containing record in binary form</param> public void Write(Stream stream) { ulong length = (ulong)stream.Length; uint crcLength = Crc32CAlgorithm.Compute(BitConverter.GetBytes(length)); uint maskLength = MaskCrc32(crcLength); _fileStream.Write(BitConverter.GetBytes(length), 0, 8); // uint64 length _fileStream.Write(BitConverter.GetBytes(maskLength), 0, 4); // uint32 masked_crc32_of_length stream.Seek(0, SeekOrigin.Begin); // Read from head byte[] buffer = new byte[BufferSize]; int count = 0; int readSize = stream.Read(buffer, 0, buffer.Length); uint crcData = Crc32CAlgorithm.Compute(buffer, 0, readSize); for (bool firstRun = true; readSize > 0; count += readSize, readSize = stream.Read(buffer, 0, buffer.Length), firstRun = false) { if (!firstRun) { crcData = Crc32CAlgorithm.Append(crcData, buffer, 0, readSize); } _fileStream.Write(buffer, 0, readSize); // byte data[length] } if (count != (int)length) { throw new Exception("Stream length does not equal to read length."); } uint maskCrcData = MaskCrc32(crcData); _fileStream.Write(BitConverter.GetBytes(maskCrcData), 0, 4); // uint32 masked_crc32_of_data }
public void ResultConsistency(string text, int offset) { var bytes = Encoding.ASCII.GetBytes(text); var crc1 = Crc32CAlgorithm.Append(123456789, bytes, offset, bytes.Length - offset); CoreSwitchHelper.SetCoreAlgType("Crc32C.SafeProxy"); var crc2 = Crc32CAlgorithm.Append(123456789, bytes, offset, bytes.Length - offset); Assert.That(crc2, Is.EqualTo(crc1)); }
public void ResultConsistencyLong() { var bytes = new byte[30000]; new Random().NextBytes(bytes); var crc1 = Crc32CAlgorithm.Append(123456789, bytes, 0, bytes.Length); CoreSwitchHelper.SetCoreAlgType("Crc32C.SafeProxy"); var crc2 = Crc32CAlgorithm.Append(123456789, bytes, 0, bytes.Length); Assert.That(crc2, Is.EqualTo(crc1)); }
public static uint Append(BytesView bv, uint initial) { uint crc = initial; while (bv != null) { if (bv.len > 0) { crc = Crc32CAlgorithm.Append(crc, bv.bytes, bv.offset, bv.len); } bv = bv.nextNode; } return(crc); }
public void Exceptions() { Assert.Throws <ArgumentNullException>(() => Crc32CAlgorithm.Compute(null)); Assert.Throws <ArgumentNullException>(() => Crc32CAlgorithm.Compute(null, 0, 0)); Assert.Throws <ArgumentNullException>(() => Crc32CAlgorithm.Append(0, null)); Assert.Throws <ArgumentNullException>(() => Crc32CAlgorithm.Append(0, null, 0, 0)); var buffer = new byte[10]; Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Compute(buffer, -1, 5)); Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Compute(buffer, 0, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Compute(buffer, 5, 6)); Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Append(0, buffer, -1, 5)); Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Append(0, buffer, 0, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Append(0, buffer, 5, 6)); }
internal bool VerifyChecksum() { uint crc = 0; using (Stream source = GetStream()) { byte[] buffer = new byte[PPeXCore.Settings.BufferSize]; int length = 0; while ((length = source.Read(buffer, 0, (int)PPeXCore.Settings.BufferSize)) > 0) { crc = Crc32CAlgorithm.Append(crc, buffer, 0, length); } } return(crc == Crc); }
private void CalculateCrc32(PhotoModel item) { var crc = Crc32CAlgorithm.Compute(Encoding.Default.GetBytes(item.LocalPath.ToLowerInvariant())); var buffer = new byte[8096]; using (var stream = File.OpenRead(item.LocalPath)) { while (true) { var read = stream.Read(buffer, 0, 8096); crc = Crc32CAlgorithm.Append(crc, buffer, 0, read); if (read < 8096) { item.Crc32 = crc.ToString(); return; } } } }
public uint Process() { if (Complete) { throw new InvalidOperationException($"{typeof(T)} has already been mapped."); } uint crc32C = 0; stopwatch.Start(); foreach (T result in Map()) { crc32C = Crc32CAlgorithm.Append(crc32C, Encoding.UTF8.GetBytes(JsonSerializer.Serialize(result))); results.Add(result); } stopwatch.Stop(); Complete = true; return(crc32C); }
public void Throughput() { var data = new byte[65536]; var random = new Random(); random.NextBytes(data); long total = 0; var stopwatch = new Stopwatch(); uint crc = 0; stopwatch.Start(); while (stopwatch.Elapsed < TimeSpan.FromSeconds(3)) { var length = random.Next(data.Length + 1); var offset = random.Next(data.Length - length); crc = Crc32CAlgorithm.Append(crc, data, offset, length); total += length; } stopwatch.Stop(); Console.WriteLine("Throughput: {0:0.0} GB/s", total / stopwatch.Elapsed.TotalSeconds / 1024 / 1024 / 1024); }
public static uint Append(byte[] bytes, uint initial) { return(Crc32CAlgorithm.Append(initial, bytes)); }