예제 #1
0
        public async Task CompareNotSimilarTest()
        {
            var dataVeryChanged = "The worst docs are the Windows source. After all, this is not what the "
                                  + "system uses for docs when it decides what to do next! The "
                                  + "manuals paraphrase the source code, often having been written at "
                                  + "many times and by the same people than who wrote the code. "
                                  + "Think of them as nothing like guidelines. Always are more like wishes... "
                                  + "Therefore, it is all too common to ignore sources and find "
                                  + "options and weird behaviors that are not put in the docs. Sometimes "
                                  + "you find options not described in the docs that are incomplete "
                                  + "and weird by the source.";

            var tlsh = new TlshBuilder(BucketSize.Buckets256, ChecksumSize.Checksum3Bytes);

            await tlsh.LoadFromStringAsync(data);

            var hash1 = tlsh.GetHash(false);

            tlsh.Reset();

            await tlsh.LoadFromStringAsync(dataVeryChanged);

            var hash2 = tlsh.GetHash(false);

            var match = hash1.TotalDiff(hash2, true);

            Assert.IsTrue(match > 200);
        }
예제 #2
0
        public async Task CompareAccuracyTest()
        {
            var highAccuracyTlsh = new TlshBuilder(BucketSize.Buckets256, ChecksumSize.Checksum3Bytes);
            var lowAccuracyTlsh  = new TlshBuilder(BucketSize.Buckets128, ChecksumSize.Checksum1Byte);

            await highAccuracyTlsh.LoadFromStringAsync(data);

            var highAccuracyHash1 = highAccuracyTlsh.GetHash(false);
            await lowAccuracyTlsh.LoadFromStringAsync(data);

            var lowAccuracyHash1 = lowAccuracyTlsh.GetHash(false);

            highAccuracyTlsh.Reset();
            lowAccuracyTlsh.Reset();

            await highAccuracyTlsh.LoadFromStringAsync(dataSlightlyChanged);

            var highAccuracyHash2 = highAccuracyTlsh.GetHash(false);
            await lowAccuracyTlsh.LoadFromStringAsync(dataSlightlyChanged);

            var lowAccuracyHash2 = lowAccuracyTlsh.GetHash(false);

            var highAccuracyMatch = highAccuracyHash1.TotalDiff(highAccuracyHash2, true);
            var lowAccuracyMatch  = lowAccuracyHash1.TotalDiff(lowAccuracyHash2, true);

            Assert.IsTrue(highAccuracyMatch < 30);
            Assert.IsTrue(lowAccuracyMatch < 30);

            Assert.IsTrue(lowAccuracyMatch < highAccuracyMatch);
        }
예제 #3
0
        public async Task CompareHashTest()
        {
            var tlsh = new TlshBuilder(BucketSize.Buckets256, ChecksumSize.Checksum3Bytes);

            await tlsh.LoadFromStringAsync(data);

            var hash1 = tlsh.GetHash(false);

            tlsh.Reset();

            await tlsh.LoadFromStringAsync(dataSlightlyChanged);

            var hash2 = tlsh.GetHash(false);

            var match = hash1.TotalDiff(hash2, true);

            Assert.IsTrue(match < 30);
        }
예제 #4
0
        public async Task HashEncodingTest()
        {
            var tlsh = new TlshBuilder(BucketSize.Buckets128, ChecksumSize.Checksum1Byte);
            await tlsh.LoadFromStringAsync(data);

            var hash = tlsh.GetHash(false);

            Assert.AreEqual("6FF02BEF718027B0160B4391212923ED7F1A463D563B1549B86CF62973B197AD2731F8", hash.GetEncoded());
        }
예제 #5
0
        public void SyncHashEncodingTest()
        {
            var tlshBuilder = new TlshBuilder();

            tlshBuilder.LoadFromString(data);

            var tlshHash    = tlshBuilder.GetHash(false);
            var encodedHash = tlshHash.GetEncoded();

            Assert.AreEqual("6FF02BEF718027B0160B4391212923ED7F1A463D563B1549B86CF62973B197AD2731F8", encodedHash);
        }
예제 #6
0
        public void CalculateHash(byte[] data)
        {
            TlshBuilder hashBuilder = new TlshBuilder();

            hashBuilder.Update(data);
            hashTLSH = hashBuilder.IsValid(false) ? hashBuilder.GetHash(false) : null;

            using (MD5 md5Builder = MD5.Create())
            {
                hashMD5 = md5Builder.ComputeHash(data);
            }
        }
예제 #7
0
        public async Task ForceHashEncodingTest()
        {
            var shortData = "a quick brown fox jumps over the lazy dog and now this is long enough.";

            var tlsh = new TlshBuilder(BucketSize.Buckets128, ChecksumSize.Checksum1Byte);
            await tlsh.LoadFromStringAsync(shortData);

            var hash        = tlsh.GetHash(true);
            var encodedHash = hash.GetEncoded();

            Assert.IsTrue(true);
        }
예제 #8
0
        public async Task EncodeToStringAndBackTest()
        {
            var tlsh = new TlshBuilder(BucketSize.Buckets128, ChecksumSize.Checksum1Byte);
            await tlsh.LoadFromStringAsync(data);

            var hash    = tlsh.GetHash(false);
            var encoded = hash.GetEncoded();

            var hashFromEncoding = TlshHash.FromTlshStr(encoded);

            var match = hash.TotalDiff(hashFromEncoding, true);

            Assert.AreEqual(0, match);
        }
예제 #9
0
        public static FastBitmapHash CalculateImageHash(FastBitmapHSV bitmap, Rectangle box, Rectangle contextBox, FastPixelMatch colorMatch, int hashWidth, int hashHeight, bool bNormalizeColors = false)
        {
            byte[] hashImage = new byte[hashWidth * hashHeight];

            // scale to requested size
            float scaleX = (float)hashWidth / box.Width;
            float scaleY = (float)hashHeight / box.Height;
            float endY   = 0.0f;

            for (int hashY = 0; hashY < hashHeight; hashY++)
            {
                float startY = endY;
                endY = (hashY + 1) / scaleY;
                if (endY >= box.Height)
                {
                    endY = box.Height - 0.00001f;
                }
                float endX = 0.0f;

                for (int hashX = 0; hashX < hashWidth; hashX++)
                {
                    float startX = endX;
                    endX = (hashX + 1) / scaleX;
                    if (endX >= box.Width)
                    {
                        endX = box.Width - 0.00001f;
                    }
                    float sum = 0.0f;

                    int sumDivNum = 0;
                    for (int srcY = (int)startY; srcY <= (int)endY; srcY++)
                    {
                        float partY = 1.0f;
                        if (srcY == (int)startY)
                        {
                            partY -= startY - srcY;
                        }
                        if (srcY == (int)endY)
                        {
                            partY -= srcY + 1 - endY;
                        }

                        for (int srcX = (int)startX; srcX <= (int)endX; srcX++)
                        {
                            float partX = 1.0f;
                            if (srcX == (int)startX)
                            {
                                partX -= startX - srcX;
                            }
                            if (srcX == (int)endX)
                            {
                                partX -= srcX + 1 - endX;
                            }

                            FastPixelHSV valuePx      = bitmap.GetPixel(box.Left + srcX, box.Top + srcY);
                            float        srcValueNorm = colorMatch.IsMatching(valuePx) ? (bNormalizeColors ? 1.0f : (valuePx.Monochrome / 255.0f)) : 0.0f;
                            sum += srcValueNorm * partY * partX;
                            sumDivNum++;
                        }
                    }

                    float storeValueNorm = sum / sumDivNum;
                    hashImage[hashX + (hashY * hashWidth)] = (byte)Math.Min(15, 15 * storeValueNorm);
                }
            }

            TlshBuilder hashBuilder = new TlshBuilder();

            hashBuilder.Update(hashImage);
            TlshHash       complexHash    = hashBuilder.IsValid(false) ? hashBuilder.GetHash(false) : null;
            ScanLineHash   simpleHash     = ScanLineHash.CreateFromImage(hashImage, hashWidth, hashHeight);
            HashCollection hashCollection = new HashCollection(complexHash, simpleHash);

            return(new FastBitmapHash
            {
                Hash = hashCollection,
                Height = hashHeight,
                Width = hashWidth,
                Pixels = hashImage,
                SourceBounds = box,
                ContextBounds = contextBox,
                DrawPos = new Point(box.Right + 1, box.Top),
            });
        }