private long Hash(K key)
        {
            var bytes = transform(key);
            var hash  = hasher.Hash(bytes);

            return(hash[0]);
        }
Beispiel #2
0
        public static int HashSolution(string salt, IHashStrategy hashStrategy)
        {
            var keys      = new List <string>();
            var hashCache = new Dictionary <int, string>();

            string GetHash(int index)
            {
                if (!hashCache.ContainsKey(index))
                {
                    hashCache[index] = hashStrategy.Hash(salt + index);
                }

                return(hashCache[index]);
            }

            for (int index = 0;; index++)
            {
                var hash = hashStrategy.Hash(salt + index);

                var c = FirstRepeatedCharacter(hash, 3);

                if (c.HasValue)
                {
                    for (int promoteIndex = index + 1; promoteIndex < index + 1000; promoteIndex++)
                    {
                        string promotionHash = GetHash(promoteIndex);

                        var fiveC = Enumerable.Repeat(c.Value, 5);
                        if (promotionHash.Contains(new string(fiveC.ToArray())))
                        {
                            keys.Add(hash);
                            if (keys.Count == 64)
                            {
                                return(index);
                            }
                            break;
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public float[,] PerformAvalancheTest(int numSamples)
        {
            var flipCounts = new int[NumInputBits, NumOutputBits];

            for (var runNumber = 0; runNumber < numSamples; runNumber++)
            {
                byte[] initial = new byte[_numInputBytes];
                _random.NextBytes(initial);

                T hash1 = _hashStrategy.Hash(initial);

                for (var i = 0; i < NumInputBits; i++)
                {
                    byte bitMaskForByteFlip = (byte)(0x1 << (i % 8));

                    initial[i / 8] ^= bitMaskForByteFlip;
                    T hash2 = _hashStrategy.Hash(initial);
                    initial[i / 8] ^= bitMaskForByteFlip;

                    for (var j = 0; j < NumOutputBits; j++)
                    {
                        if (_bitOperator.BitAt(hash1, j) != _bitOperator.BitAt(hash2, j))
                        {
                            flipCounts[i, j]++;
                        }
                    }
                }
            }

            var flipRanges = new float[NumInputBits, NumOutputBits];

            for (var i = 0; i < NumInputBits; i++)
            {
                for (var j = 0; j < NumOutputBits; j++)
                {
                    flipRanges[i, j] = (float)flipCounts[i, j] / numSamples;
                }
            }

            return(flipRanges);
        }