예제 #1
0
        public static UInt64 ToUInt64Hash(this String in_String)
        {
            // Convert the String to an array for hashing
            Byte[] seedStringBytes = Encoding.Unicode.GetBytes(in_String);

            // Create a new config in order to hash to a 64-bit number
            FNVConfig c = new FNVConfig()
            {
                HashSizeInBits = 64,
                Prime          = new System.Numerics.BigInteger(1099511628211),
                Offset         = new System.Numerics.BigInteger(14695981039346656037),
            };

            // Compute the hash
            IHashValue hashValue = FNV1aFactory.Instance.Create(c).ComputeHash(seedStringBytes);

            if (true == BitConverter.IsLittleEndian)
            {
                return(BitConverter.ToUInt64(hashValue.Hash, 0));
            }
            else
            {
                return(BitConverter.ToUInt64(hashValue.Hash.Reverse().ToArray(), 0));
            }
        }
예제 #2
0
        public void FNVConfig_Defaults_HaventChanged()
        {
            var fnvConfig = new FNVConfig();


            Assert.Equal(0, fnvConfig.HashSizeInBits);
            Assert.Equal(BigInteger.Zero, fnvConfig.Prime);
            Assert.Equal(BigInteger.Zero, fnvConfig.Offset);
        }
예제 #3
0
        public void FNVConfig_GetPredefinedConfig_ExpectedValues_Work()
        {
            foreach (var expectedPredefinedConfig in _expectedPredefinedConfigs)
            {
                var fnvConfig = FNVConfig.GetPredefinedConfig(expectedPredefinedConfig.HashSizeInBits);

                Assert.Equal(expectedPredefinedConfig.HashSizeInBits, fnvConfig.HashSizeInBits);
                Assert.Equal(expectedPredefinedConfig.Prime, fnvConfig.Prime);
                Assert.Equal(expectedPredefinedConfig.Offset, fnvConfig.Offset);
            }
        }
예제 #4
0
        public void FNVConfig_GetPredefinedConfig_InvalidSizes_Throw()
        {
            var invalidHashSizes = new[] { 0, 1, 8, 16, 31, 33, 63, 65, 127, 255, 1023, 2048, 4096 };

            foreach (var invalidHashSize in invalidHashSizes)
            {
                Assert.Equal(
                    "hashSizeInBits",
                    Assert.Throws <ArgumentOutOfRangeException>(
                        () => FNVConfig.GetPredefinedConfig(invalidHashSize))
                    .ParamName);
            }
        }
        public void FNV1aFactory_Create_Config_Works()
        {
            var fnvConfig = FNVConfig.GetPredefinedConfig(32);

            var fnv1aFactory = FNV1aFactory.Instance;
            var fnv1a        = fnv1aFactory.Create(fnvConfig);

            Assert.NotNull(fnv1a);
            Assert.IsType <FNV1a_Implementation>(fnv1a);


            var resultingFNVConfig = fnv1a.Config;

            Assert.Equal(fnvConfig.HashSizeInBits, resultingFNVConfig.HashSizeInBits);
            Assert.Equal(fnvConfig.Prime, resultingFNVConfig.Prime);
            Assert.Equal(fnvConfig.Offset, resultingFNVConfig.Offset);
        }
예제 #6
0
        public void FNVConfig_Clone_Works()
        {
            var fnvConfig = new FNVConfig()
            {
                HashSizeInBits = 32,
                Prime          = new BigInteger(1337),
                Offset         = new BigInteger(7331)
            };

            var fnvConfigClone = fnvConfig.Clone();

            Assert.IsType <FNVConfig>(fnvConfigClone);

            Assert.Equal(fnvConfig.HashSizeInBits, fnvConfigClone.HashSizeInBits);
            Assert.Equal(fnvConfig.Prime, fnvConfigClone.Prime);
            Assert.Equal(fnvConfig.Offset, fnvConfigClone.Offset);
        }
        public void FNV1Factory_Create_Works()
        {
            var defaultFNVConfig = FNVConfig.GetPredefinedConfig(64);

            var fnv1Factory = FNV1Factory.Instance;
            var fnv1        = fnv1Factory.Create();

            Assert.NotNull(fnv1);
            Assert.IsType <FNV1_Implementation>(fnv1);


            var resultingFNVConfig = fnv1.Config;

            Assert.Equal(defaultFNVConfig.HashSizeInBits, resultingFNVConfig.HashSizeInBits);
            Assert.Equal(defaultFNVConfig.Prime, resultingFNVConfig.Prime);
            Assert.Equal(defaultFNVConfig.Offset, resultingFNVConfig.Offset);
        }
 protected override IFNV1 CreateHashFunction(int hashSize) =>
 new FNV1_Implementation(
     FNVConfig.GetPredefinedConfig(hashSize));