Beispiel #1
0
        public void ShouldDifferOnBitSwitch()
        {
            var adler32 = new Adler32();

            var hash1 = adler32.ComputeHash(Encoding.Default.GetBytes("00001"));

            var hash2 = adler32.ComputeHash(Encoding.Default.GetBytes("00010"));

            CollectionAssert.AreNotEqual(hash1, hash2);
        }
Beispiel #2
0
        private static bool VerifyEntry(ArcEntry entry, IProgress?progress)
        {
            var buffer = ArrayPool <byte> .Shared.Rent(BufferSize);

            try
            {
                using var inputStream = entry.Open();
                var hash = new Adler32();
                while (true)
                {
                    var bytesInBuffer = inputStream.Read(buffer);
                    if (bytesInBuffer == 0)
                    {
                        break;
                    }
                    hash.ComputeHash(buffer, 0, bytesInBuffer);

                    progress?.AddValue(bytesInBuffer);
                }
                return(entry.Hash == hash.Hash);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
Beispiel #3
0
    public static void Compress(Stream originalStream, Stream compressedStream)
    {
        uint checksum = 0;

        using (var checksumStream = new MemoryStream())
        {
            var position = originalStream.Position;
            originalStream.CopyTo(checksumStream);
            originalStream.Seek(position, SeekOrigin.Begin);
            var buffer = checksumStream.ToArray();
            checksum = Adler32.ComputeHash(buffer);
        }

        compressedStream.Write(new byte[] { 0x78, 0x9C }, 0, 2);
        using (var compressionStream = new DeflateStream(compressedStream, CompressionLevel.Optimal, true))
        {
            originalStream.Seek(0, SeekOrigin.Begin);
            originalStream.CopyTo(compressionStream);
        }

        compressedStream.Write(new byte[]
        {
            (byte)(checksum >> 24),
            (byte)(checksum >> 16),
            (byte)(checksum >> 8),
            (byte)checksum
        }, 0, 4);
    }
Beispiel #4
0
        public void ShouldReturnSameHashForMultipleCalls()
        {
            var adler32 = new Adler32();

            byte[] previous = null;

            for (int i = 0; i < 100; i++)
            {
                var actual = adler32.ComputeHash(
                    Encoding.Default.GetBytes("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris porta ex quis volutpat hendrerit. Vivamus consequat venenatis velit sit amet varius."));

                if (previous != null)
                {
                    CollectionAssert.AreEqual(previous, actual);
                }

                previous = actual;
            }
        }