public void SingleBlockXxh32UsingSpanMatchesTheirs(string text)
        {
            var input    = Encoding.UTF8.GetBytes(text);
            var expected = Theirs32(input);
            var actual   = XXH32.DigestOf(input.AsSpan());

            Assert.Equal(expected, actual);
        }
示例#2
0
        private void ReadFrame()
        {
            FlushPeek();

            var magic = TryPeek32();

            if (magic != 0x184D2204)
            {
                throw MagicNumberExpected();
            }

            FlushPeek();

            var FLG_BD = Peek16();

            var FLG = FLG_BD & 0xFF;
            var BD  = (FLG_BD >> 8) & 0xFF;

            var version = (FLG >> 6) & 0x11;

            if (version != 1)
            {
                throw UnknownFrameVersion(version);
            }

            var blockChaining   = ((FLG >> 5) & 0x01) == 0;
            var blockChecksum   = ((FLG >> 4) & 0x01) != 0;
            var hasContentSize  = ((FLG >> 3) & 0x01) != 0;
            var contentChecksum = ((FLG >> 2) & 0x01) != 0;
            var hasDictionary   = (FLG & 0x01) != 0;
            var blockSizeCode   = (BD >> 4) & 0x07;

            var contentLength = hasContentSize ? (long?)Peek64() : null;
            var dictionaryId  = hasDictionary ? (uint?)Peek32() : null;

            var actualHC   = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);
            var expectedHC = Peek8();

            if (actualHC != expectedHC)
            {
                throw InvalidHeaderChecksum();
            }

            var blockSize = MaxBlockSize(blockSizeCode);

            if (hasDictionary)
            {
                throw NotImplemented(
                          "Predefined dictionaries feature is not implemented");               // Write32(dictionaryId);
            }
            // ReSharper disable once ExpressionIsAlwaysNull
            _frameInfo = new LZ4Descriptor(
                contentLength, contentChecksum, blockChaining, blockChecksum, dictionaryId,
                blockSize);
            _decoder = _decoderFactory(_frameInfo);
            _buffer  = new byte[blockSize];
        }
        public void HashAlgorithmWrapperReturnsSameResults(int seed, int length)
        {
            var bytes = new byte[length];

            new Random(seed).NextBytes(bytes);

            var expected = XXH32.DigestOf(bytes, 0, bytes.Length);
            var actual   = new XXH32().AsHashAlgorithm().ComputeHash(bytes);

            Assert.Equal(expected, BitConverter.ToUInt32(actual, 0));
        }
示例#4
0
        public static UInt32 xxHash(this String value)
        {
#if DEBUG
            var hash = XXH32.DigestOf(Encoding.UTF8.GetBytes(value));
            if (!_cache.ContainsKey(hash))
            {
                _cache[hash] = value;
            }
#endif
            return(XXH32.DigestOf(Encoding.UTF8.GetBytes(value)));
        }
示例#5
0
        static string HashPath(string path)
        {
            string file = Path.GetFileName(path);
            string dir  = Path.GetDirectoryName(path);

            file = $"{XXH32.DigestOf(Encoding.UTF8.GetBytes(file)):x8}";
            if (string.IsNullOrEmpty(dir))
            {
                return(file);
            }
            return(Path.Combine(HashPath(dir), file));
        }
示例#6
0
        private void ReadFrame()
        {
            Read0();

            var magic = TryRead32();

            if (magic != 0x184D2204)
            {
                throw MagicNumberExpected();
            }

            Read0();

            var FLG_BD = Read16();

            var FLG = FLG_BD & 0xFF;
            var BD  = (FLG_BD >> 8) & 0xFF;

            var version = (FLG >> 6) & 0x11;

            if (version != 1)
            {
                throw UnknownFrameVersion(version);
            }

            var blockChaining   = ((FLG >> 5) & 0x01) == 0;
            var blockChecksum   = ((FLG >> 4) & 0x01) != 0;
            var hasContentSize  = ((FLG >> 3) & 0x01) != 0;
            var contentChecksum = ((FLG >> 2) & 0x01) != 0;

            var blockSizeCode = (BD >> 4) & 0x07;

            var contentLength = hasContentSize ? (long?)Read64() : null;

            var actualHC   = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);
            var expectedHC = Read8();

            if (actualHC != expectedHC)
            {
                throw InvalidHeaderChecksum();
            }

            var blockSize = MaxBlockSize(blockSizeCode);

            _frameInfo = new LZ4FrameInfo(contentLength, contentChecksum, blockChaining, blockChecksum, blockSize);
            _decoder   = _decoderFactory(_frameInfo);
            _buffer    = new byte[blockSize];
        }
        public void EveryCallToDigestReturnsSameHash(int seed, int length)
        {
            var random = new Random(seed);
            var bytes  = new byte[length];

            random.NextBytes(bytes);

            var expected = XXH32.DigestOf(bytes, 0, bytes.Length);

            var transform = new XXH32();

            transform.Update(bytes, 0, length);

            for (var i = 0; i < 100; i++)
            {
                Assert.Equal(expected, transform.Digest());
            }
        }
        private void WriteFrame()
        {
            Stash32(0x184D2204);
            FlushStash();

            const int versionCode     = 0x01;
            var       blockChaining   = _descriptor.Chaining;
            var       blockChecksum   = _descriptor.BlockChecksum;
            var       contentChecksum = _descriptor.ContentChecksum;
            var       hasContentSize  = _descriptor.ContentLength.HasValue;
            var       hasDictionary   = _descriptor.Dictionary.HasValue;

            var FLG =
                (versionCode << 6) |
                ((blockChaining ? 0 : 1) << 5) |
                ((blockChecksum ? 1 : 0) << 4) |
                ((hasContentSize ? 1 : 0) << 3) |
                ((contentChecksum ? 1 : 0) << 2) |
                (hasDictionary ? 1 : 0);

            var blockSize = _descriptor.BlockSize;

            var BD = MaxBlockSizeCode(blockSize) << 4;

            Stash16((ushort)((FLG & 0xFF) | (BD & 0xFF) << 8));

            if (hasContentSize)
            {
                throw NotImplemented(
                          "ContentSize feature is not implemented");               // Write64(contentSize);
            }
            if (hasDictionary)
            {
                throw NotImplemented(
                          "Predefined dictionaries feature is not implemented");               // Write32(dictionaryId);
            }
            var HC = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);

            Stash8(HC);
            FlushStash();

            _encoder = CreateEncoder();
            _buffer  = new byte[LZ4Codec.MaximumOutputSize(blockSize)];
        }
        public unsafe void EmptyHash()
        {
            var input = Array.Empty <byte>();

            var expected = Theirs32(input);

            var actual1 = XXH32.DigestOf(input, 0, input.Length);

            Assert.Equal(expected, actual1);

            fixed(byte *inputP = input)
            {
                var actual2 = XXH32.DigestOf(inputP, 0);

                Assert.Equal(expected, actual2);
            }

            var actual3 = XXH32.EmptyHash;

            Assert.Equal(expected, actual3);
        }
        public void RestartableHashReturnsSameResultsAsSingleBlock(int seed, int length, int chunk)
        {
            var random = new Random(seed);
            var bytes  = new byte[length];

            random.NextBytes(bytes);

            var expected = XXH32.DigestOf(bytes, 0, bytes.Length);

            var transform = new XXH32();
            var index     = 0;

            while (index < length)
            {
                var l = Math.Min(chunk, length - index);
                transform.Update(bytes, index, l);
                index += l;
            }

            var actual = transform.Digest();

            Assert.Equal(expected, actual);
        }
示例#11
0
 private protected uint DigestOfStash(int offset = 0) =>
 XXH32.DigestOf(Stash.AsSpan(offset));
示例#12
0
 public uint HashParameters()
 {
     return(XXH32.DigestOf(Encoding.UTF8.GetBytes(ResamplerName + " " + SourceFile + " " + GetResamplerExeArgs())));
 }
示例#13
0
 string HashHex(string s)
 {
     return($"{XXH32.DigestOf(Encoding.UTF8.GetBytes(s)):x8}");
 }