Esempio n. 1
0
        public void TestGetHashFromPaths()
        {
            string path1 = Path.Combine(Environment.CurrentDirectory, "test1.txt");

            File.WriteAllText(path1, dataToTimestamp);
            string path2 = Path.Combine(Environment.CurrentDirectory, "test2.txt");

            File.WriteAllText(path2, dataToTimestamp2);

            string[] paths = { path1, path2 };

            TimestampData timestampData = new TimestampData(paths);

            byte[] result = timestampData.GetHashedData(HashAlgorithm.SHA1);

            byte[][] datas =
            {
                Encoding.UTF8.GetBytes(dataToTimestamp),
                Encoding.UTF8.GetBytes(dataToTimestamp2)
            };

            System.Security.Cryptography.HashAlgorithm algorithm = new System.Security.Cryptography.SHA1Cng();
            for (int i = 0; i < datas.Length - 1; i++)
            {
                algorithm.TransformBlock(datas[i], 0, datas[i].Length, datas[i], 0);
            }
            algorithm.TransformFinalBlock(datas[datas.Length - 1], 0, datas[datas.Length - 1].Length);
            byte[] expectedResult = algorithm.Hash;

            Assert.IsTrue(result.SequenceEqual(expectedResult));
        }
Esempio n. 2
0
        public void TestGetHashFromStreams()
        {
            Stream[] streams =
            {
                new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp)),
                new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp2))
            };
            TimestampData timestampData = new TimestampData(streams);

            byte[] result = timestampData.GetHashedData(HashAlgorithm.SHA1);

            Stream[] streams2 =
            {
                new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp)),
                new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp2))
            };
            System.Security.Cryptography.HashAlgorithm algorithm = new System.Security.Cryptography.SHA1Cng();
            using (MemoryStream finalStream = new MemoryStream())
            {
                foreach (Stream stream in streams2)
                {
                    stream.CopyTo(finalStream);
                }
                byte[] expectedResult = algorithm.ComputeHash(finalStream.ToArray());

                Assert.IsTrue(result.SequenceEqual(expectedResult));
            }
        }
Esempio n. 3
0
        public void TestGetHashFromWrongPath()
        {
            string        path          = "C:\\InvalidPath";
            TimestampData timestampData = new TimestampData(path);

            byte[] result = timestampData.GetHashedData(HashAlgorithm.SHA1);
        }
Esempio n. 4
0
        public void TestGetHashFromDigestWrongAlgorithm()
        {
            byte[]        digest        = new System.Security.Cryptography.SHA1Cng().ComputeHash(Encoding.UTF8.GetBytes(dataToTimestamp));
            TimestampData timestampData = new TimestampData(digest, true);

            byte[] result = timestampData.GetHashedData(HashAlgorithm.MD5);
        }
Esempio n. 5
0
        public override DateTime GetDateTime(int i)
        {
            this.CheckNull(i);
            ISessionInterface sessionProxy = this._command.Connection.InnerConnection.SessionProxy;

            switch (this._rResult.MetaData.ColumnTypes[i].TypeCode)
            {
            case 0x5b:
            {
                TimestampData columnInType = (TimestampData)this.GetColumnInType(i, SqlType.SqlDate);
                if (columnInType == null)
                {
                    throw new InvalidCastException();
                }
                return((DateTime)SqlType.SqlDate.ConvertSQLToCSharp(sessionProxy, columnInType));
            }

            case 0x5c:
            {
                TimeData columnInType = (TimeData)this.GetColumnInType(i, SqlType.SqlTime);
                if (columnInType == null)
                {
                    throw new InvalidCastException();
                }
                return((DateTime)SqlType.SqlTime.ConvertSQLToCSharp(sessionProxy, columnInType));
            }

            case 0x5d:
            {
                TimestampData columnInType = (TimestampData)this.GetColumnInType(i, SqlType.SqlTimestamp);
                if (columnInType == null)
                {
                    throw new InvalidCastException();
                }
                return((DateTime)SqlType.SqlTimestamp.ConvertSQLToCSharp(sessionProxy, columnInType));
            }

            case 0x5e:
            {
                TimeData columnInType = (TimeData)this.GetColumnInType(i, SqlType.SqlTimeWithTimeZone);
                if (columnInType == null)
                {
                    throw new InvalidCastException();
                }
                return((DateTime)SqlType.SqlTimeWithTimeZone.ConvertSQLToCSharp(sessionProxy, columnInType));
            }

            case 0x5f:
            {
                TimestampData columnInType = (TimestampData)this.GetColumnInType(i, SqlType.SqlTimestampWithTimeZone);
                if (columnInType == null)
                {
                    throw new InvalidCastException();
                }
                return((DateTime)SqlType.SqlTimestampWithTimeZone.ConvertSQLToCSharp(sessionProxy, columnInType));
            }
            }
            throw new InvalidCastException();
        }
Esempio n. 6
0
 protected override void WriteTimestamp(TimestampData o, SqlType type)
 {
     this.WriteLong(o.GetSeconds());
     this.WriteInt(o.GetNanos());
     if (type.TypeCode == 0x5f)
     {
         this.WriteInt(o.GetZone());
     }
 }
Esempio n. 7
0
        public void TestGetHashFromDigest()
        {
            byte[]        digest        = new System.Security.Cryptography.SHA1Cng().ComputeHash(Encoding.UTF8.GetBytes(dataToTimestamp));
            TimestampData timestampData = new TimestampData(digest, true);

            byte[] result = timestampData.GetHashedData(HashAlgorithm.SHA1);

            Assert.IsTrue(result.SequenceEqual(digest));
        }
Esempio n. 8
0
        public void TestGetHashFromByteArray()
        {
            byte[]        data          = Encoding.UTF8.GetBytes(dataToTimestamp);
            TimestampData timestampData = new TimestampData(data);

            byte[] result         = timestampData.GetHashedData(HashAlgorithm.SHA1);
            byte[] expectedResult = new System.Security.Cryptography.SHA1Cng().ComputeHash(data);

            Assert.IsTrue(result.SequenceEqual(expectedResult));
        }
Esempio n. 9
0
        public void TestGetHashFromStream()
        {
            Stream        stream        = new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp));
            TimestampData timestampData = new TimestampData(stream);

            byte[] result = timestampData.GetHashedData(HashAlgorithm.SHA1);

            stream = new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp));
            byte[] expectedResult = new System.Security.Cryptography.SHA1Cng().ComputeHash(stream);

            Assert.IsTrue(result.SequenceEqual(expectedResult));
        }
Esempio n. 10
0
        public void TestGetHashFromStreamRepeatedly()
        {
            Stream        stream        = new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp));
            TimestampData timestampData = new TimestampData(stream);

            byte[] result1 = timestampData.GetHashedData(HashAlgorithm.SHA1);

            stream        = new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp));
            timestampData = new TimestampData(stream);
            byte[] result2 = timestampData.GetHashedData(HashAlgorithm.SHA1);

            Assert.IsTrue(result1.SequenceEqual(result2));
        }
Esempio n. 11
0
        public void TestGetHashFromPath()
        {
            string path = Path.Combine(Environment.CurrentDirectory, "test.txt");

            File.WriteAllText(path, dataToTimestamp);

            TimestampData timestampData = new TimestampData(path);

            byte[] result         = timestampData.GetHashedData(HashAlgorithm.SHA1);
            byte[] data           = Encoding.UTF8.GetBytes(dataToTimestamp);
            byte[] expectedResult = new System.Security.Cryptography.SHA1Cng().ComputeHash(data);

            Assert.IsTrue(result.SequenceEqual(expectedResult));
        }
Esempio n. 12
0
        public void TestGetHashFromWrongPaths()
        {
            string path1 = Path.Combine(Environment.CurrentDirectory, "test1.txt");

            File.WriteAllText(path1, dataToTimestamp);
            string path2 = Path.Combine(Environment.CurrentDirectory, "test2.txt");

            File.WriteAllText(path2, dataToTimestamp2);

            string[] paths = { path1, path2, "InvalidPath" };

            TimestampData timestampData = new TimestampData(paths);

            byte[] result = timestampData.GetHashedData(HashAlgorithm.SHA1);
        }
Esempio n. 13
0
        public void TestGetHashFromByteArraysRepeatableResults()
        {
            byte[][] datas =
            {
                Encoding.UTF8.GetBytes(dataToTimestamp),
                Encoding.UTF8.GetBytes(dataToTimestamp2)
            };
            TimestampData timestampData = new TimestampData(datas);

            byte[] result1 = timestampData.GetHashedData(HashAlgorithm.SHA1);
            byte[] result2 = timestampData.GetHashedData(HashAlgorithm.SHA1);
            byte[] result3 = timestampData.GetHashedData(HashAlgorithm.SHA1);

            Assert.IsTrue(result1.SequenceEqual(result2));
            Assert.IsTrue(result1.SequenceEqual(result3));
            Assert.IsTrue(result2.SequenceEqual(result3));
        }
Esempio n. 14
0
        public void TestGetHashFromByteArrayVsArrays()
        {
            byte[]        data           = Encoding.UTF8.GetBytes(dataToTimestamp);
            TimestampData timestampData1 = new TimestampData(data);

            byte[] result1 = timestampData1.GetHashedData(HashAlgorithm.SHA1);

            byte[][] datas =
            {
                Encoding.UTF8.GetBytes(dataToTimestamp),
                Encoding.UTF8.GetBytes(dataToTimestamp2)
            };
            TimestampData timestampData2 = new TimestampData(datas);

            byte[] result2 = timestampData2.GetHashedData(HashAlgorithm.SHA1);

            Assert.IsFalse(result1.SequenceEqual(result2));
        }
Esempio n. 15
0
        public void TestGetHashFromByteArraysWrongOrder()
        {
            byte[][] datas =
            {
                Encoding.UTF8.GetBytes(dataToTimestamp),
                Encoding.UTF8.GetBytes(dataToTimestamp2)
            };
            TimestampData timestampData = new TimestampData(datas);

            byte[] result = timestampData.GetHashedData(HashAlgorithm.SHA1);

            System.Security.Cryptography.HashAlgorithm algorithm = new System.Security.Cryptography.SHA1Cng();
            for (int i = datas.Length - 1; i >= 1; i--)
            {
                algorithm.TransformBlock(datas[i], 0, datas[i].Length, datas[i], 0);
            }
            algorithm.TransformFinalBlock(datas[0], 0, datas[0].Length);
            byte[] expectedResult = algorithm.Hash;;

            Assert.IsFalse(result.SequenceEqual(expectedResult));
        }
Esempio n. 16
0
        public void TestGetHashFromStreamsWrongOrder()
        {
            Stream[] streams1 =
            {
                new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp)),
                new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp2))
            };
            TimestampData timestampData1 = new TimestampData(streams1);

            byte[] result1 = timestampData1.GetHashedData(HashAlgorithm.SHA1);

            Stream[] streams2 =
            {
                new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp2)),
                new MemoryStream(Encoding.UTF8.GetBytes(dataToTimestamp))
            };
            TimestampData timestampData2 = new TimestampData(streams2);

            byte[] result2 = timestampData2.GetHashedData(HashAlgorithm.SHA1);

            Assert.IsFalse(result1.SequenceEqual(result2));
        }
Esempio n. 17
0
 protected abstract void WriteTimestamp(TimestampData o, SqlType type);
Esempio n. 18
0
 protected override void WriteDate(TimestampData o, SqlType type)
 {
     this.WriteLong(o.GetSeconds());
 }
Esempio n. 19
0
 public static void Seed(TimestampQueryContext context)
 {
     context.Entities.AddRange(TimestampData.CreateEntities());
     context.SaveChanges();
 }
Esempio n. 20
0
 protected override void WriteTimestamp(TimestampData o, SqlType type)
 {
     base.Write(0x27);
     base.WriteBytes(type.ConvertToString(o));
     base.Write(0x27);
 }