Exemplo n.º 1
0
        public void ConstructorsEquivalent()
        {
            ulong      ui1       = 0xdeadcafe;
            ulong      ui2       = 0xbaceba11;
            SpookyHash fromU     = new SpookyHash(ui1, ui2);
            SpookyHash fromZeroU = new SpookyHash();

            fromZeroU.Init(ui1, ui2);
            long       l1        = unchecked ((long)ui1);
            long       l2        = unchecked ((long)ui2);
            SpookyHash fromL     = new SpookyHash(l1, l2);
            SpookyHash fromZeroL = new SpookyHash();

            fromZeroL.Init(l1, l2);
            fromU.Update(MediumLengthString);
            fromZeroU.Update(MediumLengthString);
            fromL.Update(MediumLengthString);
            fromZeroL.Update(MediumLengthString);
            HashCode128 hash = fromU.Final();

            Assert.Equal(hash, fromZeroU.Final());
            Assert.Equal(hash, fromL.Final());
            Assert.Equal(hash, fromZeroL.Final());
            Assert.Equal(hash.ToString(), fromZeroL.Final().ToString());
        }
Exemplo n.º 2
0
        public unsafe void TestPieces()
        {
            byte[] bufArr = new byte[BufferSize];
            for (int i = 0; i < BufferSize; ++i)
            {
                bufArr[i] = unchecked ((byte)i);
            }
            for (int i = 0; i < BufferSize; ++i)
            {
                ulong      a, b, c, d, seed1 = 1, seed2 = 2;
                SpookyHash state = new SpookyHash();

                // all as one call
                a = seed1;
                b = seed2;
                fixed(byte *buf = bufArr)
                {
                    SpookyHash.Hash128(buf, i, ref a, ref b);
                }

                // all as one piece
                c = 0xdeadbeefdeadbeef;
                d = 0xbaceba11baceba11;
                state.Init(seed1, seed2);
                fixed(byte *buf = bufArr)
                {
                    state.Update(buf, i);
                }

                state.Final(out c, out d);

                Assert.Equal(a, c);
                Assert.Equal(b, d);

                for (int j = 0; j < i; ++j)
                {
                    c = seed1;
                    d = seed2;
                    state.Init(c, d);
                    fixed(byte *buf = bufArr)
                    {
                        state.Update(buf, j);
                        state.Update(buf + j, i - j);
                    }

                    state.Final(out c, out d);
                    Assert.Equal(a, c);
                    Assert.Equal(b, d);
                }
            }
        }
Exemplo n.º 3
0
        public static StorageMetadata CreateStorageFile(Stream dataStream, uint archiveNumber, ulong?profileKey)
        {
            StorageMetadata metadata = new StorageMetadata();

            metadata.ArchiveNumber = archiveNumber + 2;

            byte[] data = null;
            using (BinaryReader reader = new BinaryReader(dataStream))
            {
                data = reader.ReadBytes((int)reader.BaseStream.Length);
            }

            Array.Resize(ref data, data.Length + 1); // 1 byte null padding

            // Generate Spooky hash of data for later
            SpookyHash sh = new SpookyHash(0x155af93ac304200, 0x8ac7230489e7ffff);

            sh.Update(new SHA256Managed().ComputeHash(data));
            sh.Update(data);
            sh.Final(out metadata.Key[0], out metadata.Key[1]);

            var compressedData = Compress(data);
            var key            = GenerateKey(metadata, profileKey, true);

            metadata.StorageRaw       = XXTEA.Encrypt(compressedData, key);
            metadata.CompressedSize   = (uint)compressedData.Length;
            metadata.DecompressedSize = (uint)data.Length;

            return(metadata);
        }
Exemplo n.º 4
0
        private static void ParseStorageFile(StorageMetadata metadata, Stream storageStream, ulong?profileKey = null)
        {
            byte[] data = null;
            using (BinaryReader reader = new BinaryReader(storageStream))
            {
                data = reader.ReadBytes((int)reader.BaseStream.Length);
            }

            var rawSha256 = new SHA256Managed().ComputeHash(data);
            var sha256    = BitConverter.ToString(rawSha256).Replace("-", "");

            if (sha256 != metadata.SHA256)
            {
                throw new InvalidDataException("Invalid storage file. Corrupt or wrong file?");
            }

            if (metadata.StorageVersion == STORAGE_VERSION_V1)
            {
                var key           = GenerateKey(metadata, profileKey);
                var decryptedData = XXTEA.Decrypt(data, key);
                var output        = Decompress(metadata, decryptedData);

                // Verify data
                SpookyHash sh = new SpookyHash(0x155af93ac304200, 0x8ac7230489e7ffff);
                sh.Update(new SHA256Managed().ComputeHash(output));
                sh.Update(output);

                ulong hash1, hash2;
                sh.Final(out hash1, out hash2);

                ulong orig_hash1 = metadata.Key[0];
                ulong orig_hash2 = metadata.Key[1];

                if (orig_hash1 != hash1 || orig_hash2 != hash2)
                {
                    throw new InvalidDataException("Invalid decrypted data. Wrong key?");
                }

                metadata.StorageRaw = output;
            }
            else
            {
                SpookyHash sh = new SpookyHash(0x155af93ac304200, 0x8ac7230489e7ffff);
                sh.Update(rawSha256);
                sh.Update(data);

                ulong hash1, hash2;
                sh.Final(out hash1, out hash2);

                ulong orig_hash1 = metadata.Key[0];
                ulong orig_hash2 = metadata.Key[1];

                if (orig_hash1 != hash1 || orig_hash2 != hash2)
                {
                    throw new InvalidDataException("Invalid decrypted data. Wrong key?");
                }

                metadata.StorageRaw = data;
            }
        }
Exemplo n.º 5
0
        public void SequenceOfStrings()
        {
            SpookyHash sh = new SpookyHash();

            sh.Update(MediumLengthSequence());
            Assert.Equal(MediumLengthString.SpookyHash128(), sh.Final());
        }
Exemplo n.º 6
0
        public void StringExtension()
        {
            string testString;

            using (Stream stm = GetStream())
                using (StreamReader tr = new StreamReader(stm))
                {
                    testString = tr.ReadToEnd();
                }
            SpookyHash sh = new SpookyHash();

            sh.Update(testString);
            HashCode128 h   = sh.Final();
            int         len = testString.Length;

            Assert.Equal(h, testString.SpookyHash128());
            Assert.Equal(h, testString.SpookyHash128(0, len, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF));
            Assert.Equal(
                h,
                unchecked (testString.SpookyHash128(0, len, (long)0xDEADBEEFDEADBEEF, (long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(h, testString.SpookyHash128(0, len));
            HashCode128 hashSlice = testString.SpookyHash128(50, 100, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF);

            Assert.NotEqual(h, hashSlice);
            Assert.Equal(
                hashSlice,
                unchecked (testString.SpookyHash128(50, 100, (long)0xDEADBEEFDEADBEEF, (long)0xDEADBEEFDEADBEEF)));
            long longHash = testString.SpookyHash64(0, len, unchecked ((long)0xDEADBEEFDEADBEEF));

            Assert.Equal(longHash, testString.SpookyHash64(unchecked ((long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(longHash, testString.SpookyHash64(0, len));
            Assert.Equal(longHash, testString.SpookyHash64());
            int hash = testString.SpookyHash32(0, len, unchecked ((int)0xDEADBEEF));

            Assert.Equal(hash, testString.SpookyHash32(unchecked ((int)0xDEADBEEF)));
            Assert.Equal(hash, testString.SpookyHash32(0, len));
            Assert.Equal(hash, testString.SpookyHash32());
            Assert.Equal(HashCode128.Zero, default(string).SpookyHash128());
            Assert.Equal(
                HashCode128.Zero,
                default(string).SpookyHash128(0, 200, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF));
            Assert.Equal(
                HashCode128.Zero,
                unchecked (default(string).SpookyHash128(0, 200, (long)0xDEADBEEFDEADBEEF, (long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(HashCode128.Zero, default(string).SpookyHash128(0, 200));
            Assert.Equal(
                HashCode128.Zero,
                default(string).SpookyHash128(50, 100, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF));
            Assert.Equal(0, default(string).SpookyHash64(0, 200, unchecked ((long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(0, default(string).SpookyHash64(unchecked ((long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(0, default(string).SpookyHash64(0, 200));
            Assert.Equal(0, default(string).SpookyHash64());
            Assert.Equal(0, default(string).SpookyHash32(0, 200, unchecked ((int)0xDEADBEEF)));
            Assert.Equal(0, default(string).SpookyHash32(unchecked ((int)0xDEADBEEF)));
            Assert.Equal(0, default(string).SpookyHash32(0, 200));
            Assert.Equal(0, default(string).SpookyHash32());
        }
Exemplo n.º 7
0
        public void Serialize()
        {
            SpookyHash sh = new SpookyHash();

            sh.Update(MediumLengthString);
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, sh);
                ms.Seek(0, SeekOrigin.Begin);
                SpookyHash copy = (SpookyHash)bf.Deserialize(ms);
                Assert.Equal(sh.Final(), copy.Final());
            }
        }
Exemplo n.º 8
0
        public void FinalsEquivalent()
        {
            SpookyHash sh = new SpookyHash();

            sh.Update("abcdefg");
            HashCode128 h128 = sh.Final();

            sh.Final(out ulong u1, out ulong u2);
            Assert.Equal(h128.UHash1, u1);
            Assert.Equal(h128.UHash2, u2);
            sh.Final(out long l1, out long l2);
            Assert.Equal(h128.Hash1, l1);
            Assert.Equal(h128.Hash2, l2);
        }
Exemplo n.º 9
0
        public int GetHashCode(IList <T> list)
        {
            var hasher = new SpookyHash();//use methods with seeds if you need to prevent HashDos

            foreach (var item in list)
            {
                hasher.Update(item.GetHashCode());//or relevant feeds of item, etc.
            }
            return(hasher.Final().GetHashCode());

            /*
             * int hash = list.Any()?0:9;
             * unchecked
             * {
             *  foreach (var o in list)
             *  {
             *  int code = o.ToInt32(CultureInfo.InvariantCulture);
             *      hash *= 251; // multiply by a prime number
             *      hash += code; // add next hash code
             *  }
             * }
             * return hash;
             */
        }
Exemplo n.º 10
0
 public static uint SpookyHash(this string str)
 {
     SpookilySharp.SpookyHash hash = new SpookyHash();
     hash.Update(str);
     return((uint)hash.Final().UHash2);
 }