Beispiel #1
0
        public void TestNullStreamThrowException()
        {
            Assert.ThrowsException <ArgumentNullHashLibException>(() => hash.ComputeStream(null));

            //
            hash.Initialize();

            Assert.ThrowsException <ArgumentNullHashLibException>(() => hash.TransformStream(null));

            hash.TransformFinal();
        }
Beispiel #2
0
        } // end function

        public static void TestEmptyStream(string expected, IHash hash)
        {
            Stream stream;
            string ActualString;

            stream = new MemoryStream();

            ActualString = hash.ComputeStream(stream).ToString();

            Assert.AreEqual(expected, ActualString,
                            String.Format("Expected {0} but got {1}.",
                                          expected, ActualString));

            stream.Close(); // close stream
        } // end function
Beispiel #3
0
        protected void TestHashStream(IHash a_hash, int a_block_size)
        {
            int old = Hash.BUFFER_SIZE;

            Hash.BUFFER_SIZE = a_block_size;

            try
            {
                for (int i = 1; i < 10; i++)
                {
                    byte[] data = new byte[13 * i];
                    new Random().NextBytes(data);

                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        HashResult h1 = a_hash.ComputeBytes(data);
                        HashResult h2 = a_hash.ComputeStream(ms);

                        Assert.AreEqual(h1, h2);

                        h1 = a_hash.ComputeBytes(HashLib.ArrayExtensions.SubArray(data, i, i * 7));
                        ms.Seek(i, SeekOrigin.Begin);
                        h2 = a_hash.ComputeStream(ms, i * 7);

                        Assert.AreEqual(h1, h2);

                        h1 = a_hash.ComputeBytes(data);
                        a_hash.Initialize();
                        a_hash.TransformBytes(data, 0, i * 3);
                        ms.Seek(i * 3, SeekOrigin.Begin);
                        a_hash.TransformStream(ms, i * 2);
                        a_hash.TransformBytes(data, i * 5);
                        h2 = a_hash.TransformFinal();

                        Assert.AreEqual(h1, h2);
                    }
                }

                int[] sizes = { 1,      11,   63,   64,    65,   127,    128, 129, 255, 256, 257, 511, 512, 513,
                                1023, 1024, 1025, 4011, 64000, 66000, 250000, Hash.BUFFER_SIZE * 20 + 511 };

                foreach (int size in sizes)
                {
                    byte[] data = new byte[size];
                    new Random().NextBytes(data);

                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        HashResult h1 = a_hash.ComputeBytes(data);
                        HashResult h2 = a_hash.ComputeStream(ms);

                        Assert.AreEqual(h1, h2);
                    }
                }

                {
                    byte[] data = new byte[1011];
                    new Random().NextBytes(data);

                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        bool ex = false;

                        try
                        {
                            ms.Position = 1011;
                            a_hash.ComputeStream(ms, -1);
                        }
                        catch
                        {
                            ex = true;
                        }

                        Assert.IsFalse(ex);

                        ex = false;

                        try
                        {
                            ms.Position = 1010;
                            a_hash.ComputeStream(ms, 2);
                        }
                        catch
                        {
                            ex = true;
                        }

                        Assert.IsTrue(ex);

                        try
                        {
                            ms.Position = 0;
                            a_hash.ComputeStream(ms, 1012);
                        }
                        catch
                        {
                            ex = true;
                        }

                        Assert.IsTrue(ex);
                    }
                }
            }
            finally
            {
                Hash.BUFFER_SIZE = old;
            }
        }
Beispiel #4
0
        public static void Main(string[] args)
        {
            // Prepare temp file.
            string file_name = Path.GetTempFileName();

            using (var fs = new FileStream(file_name, FileMode.Open))
            {
                byte[] v = new byte[256];
                new Random().NextBytes(v);
                fs.Write(v, 0, v.Length);
            }

            // Prepare stream.
            MemoryStream ms = new MemoryStream(new byte[] { 2, 3, 4, 5, 6, 7 });

            // Choose algorithm. Explore HashFactory for more algorithms.
            IHash hash = HashFactory.Crypto.CreateSHA256();

            // Hash data immediate.
            HashResult r = hash.ComputeString("test", Encoding.ASCII);

            // Hash data.
            hash.Initialize(); // Not mandatory after Compute and TransformFinal
            hash.TransformULong(6);
            hash.TransformString("test");
            r = hash.TransformFinal();

            // Calculate 32-bits hash.
            hash = HashFactory.Checksum.CreateCRC32_IEEE();
            uint crc32 = hash.ComputeString("test", Encoding.ASCII).GetUInt();

            // For CRCs you may specify parameters.
            hash = HashFactory.Checksum.CreateCRC32(
                HashLib.Checksum.CRC32Polynomials.IEEE_802_3, uint.MaxValue, uint.MaxValue);
            hash = HashFactory.Checksum.CreateCRC32(
                0xF0F0F0F0, uint.MaxValue, uint.MaxValue);

            // Most hashes can be created in two ways.
            hash = HashFactory.Crypto.CreateHaval(HashRounds.Rounds3, HashSize.HashSize256);
            hash = HashFactory.Crypto.CreateHaval_3_256();

            // Calculate 64-bits hash.
            hash = HashFactory.Hash64.CreateMurmur2();
            ulong crc64 = hash.ComputeString("test", Encoding.ASCII).GetULong();

            // Calculate hash with key.
            var hash_with_key = HashFactory.Hash128.CreateMurmur3_128();

            hash_with_key.Key = new MersenneTwister().NextBytes(hash_with_key.KeyLength.Value);
            r = hash_with_key.ComputeString("test", Encoding.ASCII);
            // This will restore default key.
            hash_with_key.Key = null;

            // Get some information about algorithm. BlockSize has only informative meaning.
            System.Console.WriteLine("{0}, {1}, {2}", hash.BlockSize, hash.HashSize, hash.Name);

            // Here you can find algorithms grouped by its properties.
            foreach (var h in Hashes.CryptoAll)
            {
                System.Console.WriteLine(((IHash)Activator.CreateInstance(h)).Name);
            }
            foreach (var h in Hashes.CryptoNotBuildIn)
            {
                System.Console.WriteLine(((IHash)Activator.CreateInstance(h)).Name);
            }
            foreach (var h in Hashes.CryptoBuildIn)
            {
                System.Console.WriteLine(((IHash)Activator.CreateInstance(h)).Name);
            }
            foreach (var h in Hashes.Checksums)
            {
                System.Console.WriteLine(((IHash)Activator.CreateInstance(h)).Name);
            }
            // ... And more

            // Hash stream.
            r           = hash.ComputeStream(ms);
            ms.Position = 2;
            r           = hash.ComputeStream(ms);    // Compute all bytes starting from 2
            ms.Position = 3;
            r           = hash.ComputeStream(ms, 2); // Compute 2 bytes starting from 3

            hash.TransformInt(111);
            ms.Position = 0;
            hash.TransformStream(ms);
            r = hash.TransformFinal();

            // Hash file
            r = hash.ComputeFile(file_name);
            r = hash.ComputeFile(file_name, 10);     // Compute all bytes starting from 10
            r = hash.ComputeFile(file_name, 12, 10); // Compute 10 bytes starting from 12.

            hash.TransformInt(111);
            hash.TransformFile(file_name);
            r = hash.TransformFinal();

            // Calculate HMAC.
            IHMAC hmac = HashFactory.HMAC.CreateHMAC(HashFactory.Crypto.CreateSHA256());

            hmac.Key = Converters.ConvertStringToBytes("secret", Encoding.ASCII);
            r        = hmac.ComputeString("test", Encoding.ASCII);
            Debug.Assert(hmac.KeyLength == null, "NULL means key can be any length");

            // Get System.Security.Cryptography.HashAlgorithm wrapper for algorithms from this library.
            System.Security.Cryptography.HashAlgorithm hash2 = HashFactory.Wrappers.HashToHashAlgorithm(hash);

            // And back.
            hash = HashFactory.Wrappers.HashAlgorithmToHash(hash2);

            // Some algorithms have fast specialized methods for calculating hashes for all data types.
            // They are designed for calculating good-behaving hash codes for hash-tables.
            hash = HashFactory.Hash32.CreateMurmur2();
            Debug.Assert(hash is IFastHash32);

            // Some algorithms can calculated hashes only when they had all needed data,
            // they accumulated data to the very end.
            hash = HashFactory.Hash32.CreateMurmur2();
            Debug.Assert(hash is INonBlockHash);

            // Use build-in cryptography hash algorithms.
            hash = HashFactory.Crypto.BuildIn.CreateSHA256Cng();

            // Delete temp file.
            new FileInfo(file_name).Delete();
        }
Beispiel #5
0
        protected void TestHashStreamValueType(IHash a_hash_stream, IHash a_hash)
        {
            string file_name = Path.GetTempFileName();
            int ratio = (TEST_BLOCK_SIZE * 3 / 2) / 9;

            try
            {
                for(int i = 0; i < 10; i++)
                {
                    var data = new byte[ratio * i];
                    m_random.NextBytes(data);

                    using(var stream = new FileStream(file_name, FileMode.Truncate))
                    {
                        new BinaryWriter(stream).Write(data);

                        stream.Seek(0, SeekOrigin.Begin);

                        HashResult h1 = a_hash_stream.ComputeStream(stream);
                        HashResult h2 = a_hash.ComputeBytes(data);

                        Assert.AreEqual(h2, h1, String.Format("{0}, {1}", a_hash.Name, data.Length));
                    }
                }
            }
            finally
            {
                new FileInfo(file_name).Delete();
            }
        }
Beispiel #6
0
        protected void TestHashStream(IHash a_hash, int a_block_size)
        {
            int old = Hash.BUFFER_SIZE;
            Hash.BUFFER_SIZE = a_block_size;

            try
            {
                for (int i = 1; i < 10; i++)
                {
                    byte[] data = new byte[13 * i];
                    new Random().NextBytes(data);

                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        HashResult h1 = a_hash.ComputeBytes(data);
                        HashResult h2 = a_hash.ComputeStream(ms);

                        Assert.AreEqual(h1, h2);

                        h1 = a_hash.ComputeBytes(HashLib.ArrayExtensions.SubArray(data, i, i * 7));
                        ms.Seek(i, SeekOrigin.Begin);
                        h2 = a_hash.ComputeStream(ms, i * 7);

                        Assert.AreEqual(h1, h2);

                        h1 = a_hash.ComputeBytes(data);
                        a_hash.Initialize();
                        a_hash.TransformBytes(data, 0, i * 3);
                        ms.Seek(i * 3, SeekOrigin.Begin);
                        a_hash.TransformStream(ms, i * 2);
                        a_hash.TransformBytes(data, i * 5);
                        h2 = a_hash.TransformFinal();

                        Assert.AreEqual(h1, h2);
                    }
                }

                int[] sizes = { 1, 11, 63, 64, 65, 127, 128, 129, 255, 256, 257, 511, 512, 513,
                              1023, 1024, 1025, 4011, 64000, 66000, 250000, Hash.BUFFER_SIZE * 20 + 511};

                foreach (int size in sizes)
                {
                    byte[] data = new byte[size];
                    new Random().NextBytes(data);

                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        HashResult h1 = a_hash.ComputeBytes(data);
                        HashResult h2 = a_hash.ComputeStream(ms);

                        Assert.AreEqual(h1, h2);
                    }
                }

                {
                    byte[] data = new byte[1011];
                    new Random().NextBytes(data);

                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        bool ex = false;

                        try
                        {
                            ms.Position = 1011;
                            a_hash.ComputeStream(ms, -1);
                        }
                        catch
                        {
                            ex = true;
                        }

                        Assert.IsFalse(ex);

                        ex = false;

                        try
                        {
                            ms.Position = 1010;
                            a_hash.ComputeStream(ms, 2);
                        }
                        catch
                        {
                            ex = true;
                        }

                        Assert.IsTrue(ex);

                        try
                        {
                            ms.Position = 0;
                            a_hash.ComputeStream(ms, 1012);
                        }
                        catch
                        {
                            ex = true;
                        }

                        Assert.IsTrue(ex);
                    }

                }
            }
            finally
            {
                Hash.BUFFER_SIZE = old;
            }
        }