public void TestDataHasherWithAlgorithmNotImplemented()
        {
            HashingException ex = Assert.Throws <HashingException>(delegate
            {
                CryptoTestFactory.CreateDataHasher(HashAlgorithm.Sha3256);
            });

            Assert.That(ex.Message, Does.StartWith("Hash algorithm(SHA3-256) is not supported"));
        }
        public void TestDataHasherWithAlgorithmNull()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate
            {
                CryptoTestFactory.CreateDataHasher(null);
            });

            Assert.AreEqual("algorithm", ex.ParamName);
        }
        public void TestDataHasherWithRipemd160Algorithm()
        {
            IDataHasher hasher = CryptoTestFactory.CreateDataHasher(HashAlgorithm.Ripemd160);

            Assert.AreEqual(HashAlgorithm.Ripemd160.Length, hasher.GetHash().Value.Length, "Hash length should be correct");
            byte[] bytes = new byte[hasher.GetHash().Value.Length];
            hasher.GetHash().Value.CopyTo(bytes, 0);
            Assert.AreEqual("9C-11-85-A5-C5-E9-FC-54-61-28-08-97-7E-E8-F5-48-B2-25-8D-31", BitConverter.ToString(bytes), "Hash value should be calculated correctly");
        }
        public void TestDataHasherWithDefaultAlgorithmAndEmptyData()
        {
            IDataHasher hasher = CryptoTestFactory.CreateDataHasher();

            Assert.AreEqual(HashAlgorithm.Sha2256.Length, hasher.GetHash().Value.Length, "Hash length should be correct");
            byte[] bytes = new byte[hasher.GetHash().Value.Length];
            hasher.GetHash().Value.CopyTo(bytes, 0);
            Assert.AreEqual("E3-B0-C4-42-98-FC-1C-14-9A-FB-F4-C8-99-6F-B9-24-27-AE-41-E4-64-9B-93-4C-A4-95-99-1B-78-52-B8-55", BitConverter.ToString(bytes),
                            "Hash value should be calculated correctly");
        }
        public void TestDataHasherWithNullStream()
        {
            IDataHasher           hasher = CryptoTestFactory.CreateDataHasher();
            ArgumentNullException ex     = Assert.Throws <ArgumentNullException>(delegate
            {
                hasher.AddData((Stream)null);
            });

            Assert.AreEqual("inStream", ex.ParamName);
        }
        public void TestDataHasherWithNullBytesAndNoLength()
        {
            IDataHasher           hasher = CryptoTestFactory.CreateDataHasher();
            ArgumentNullException ex     = Assert.Throws <ArgumentNullException>(delegate
            {
                hasher.AddData(null, 0, 0);
            });

            Assert.AreEqual("data", ex.ParamName);
        }
        public void TestDataHasherWithNullICollection()
        {
            IDataHasher           hasher = CryptoTestFactory.CreateDataHasher();
            ArgumentNullException ex     = Assert.Throws <ArgumentNullException>(delegate
            {
                hasher.AddData((byte[])null);
            });

            Assert.AreEqual("data", ex.ParamName);
        }
        public void TestDataHasherWithDefaultAlgorithmAndByteLength()
        {
            IDataHasher hasher = CryptoTestFactory.CreateDataHasher();

            byte[] data = System.Text.Encoding.UTF8.GetBytes(Resources.DataHasher_TestString);
            hasher.AddData(data, 0, data.Length);
            Assert.AreEqual(HashAlgorithm.Sha2256.Length, hasher.GetHash().Value.Length, "Hash length should be correct");
            byte[] bytes = new byte[hasher.GetHash().Value.Length];
            hasher.GetHash().Value.CopyTo(bytes, 0);
            Assert.AreEqual("CF-00-FC-3A-72-A2-F7-1C-7D-E2-B7-18-C0-A4-DF-F3-8D-83-C0-E1-95-7E-C2-19-C3-B2-66-F8-CC-38-B9-EA", BitConverter.ToString(bytes),
                            "Hash value should be calculated correctly");
        }
        public void TestDataHasherWithDefaultAlgorithmAndFileStreamLimitedBuffer()
        {
            IDataHasher hasher = CryptoTestFactory.CreateDataHasher();
            FileStream  stream = new FileStream(Path.Combine(TestSetup.LocalPath, Resources.DataHasher_TestFile), FileMode.Open);

            hasher.AddData(stream, 1);
            Assert.AreEqual(HashAlgorithm.Sha2256.Length, hasher.GetHash().Value.Length, "Hash length should be correct");
            byte[] bytes = new byte[hasher.GetHash().Value.Length];
            hasher.GetHash().Value.CopyTo(bytes, 0);
            Assert.AreEqual("54-66-E3-CB-A1-4A-84-3A-5E-93-B7-8E-3D-6A-B8-D3-49-1E-DC-AC-7E-06-43-1C-E1-A7-F4-98-28-C3-40-C3", BitConverter.ToString(bytes),
                            "Hash value should be calculated correctly");
            stream.Close();
        }
        public void TestDataHasherWithAddingDataAfterHashHasBeenCalculated()
        {
            IDataHasher hasher = CryptoTestFactory.CreateDataHasher();

            byte[] data = System.Text.Encoding.UTF8.GetBytes(Resources.DataHasher_TestString);
            hasher.AddData(data);
            hasher.GetHash();
            HashingException ex = Assert.Throws <HashingException>(delegate
            {
                hasher.AddData(data);
            });

            Assert.That(ex.Message, Does.StartWith("Output hash has already been calculated"));
        }
        public void CreateDataHasherTest()
        {
            string typeName = CryptoTestFactory.CreateDataHasher().GetType().FullName;

            switch (CryptoTestFactory.ProviderType)
            {
            case CryptoProviderType.Microsoft:
                Assert.AreEqual("Guardtime.KSI.Crypto.Microsoft.Hashing.DataHasher", typeName, "Invalid data hasher type name.");
                break;

            case CryptoProviderType.BouncyCastle:
                Assert.AreEqual("Guardtime.KSI.Crypto.BouncyCastle.Hashing.DataHasher", typeName, "Invalid data hasher type name.");
                break;
            }
        }
        public void VerifySignedHashWithInvalidHashTest(Ksi ksi)
        {
            VerificationResult verificationResult;

            using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("test")))
            {
                IDataHasher dataHasher = CryptoTestFactory.CreateDataHasher(HashAlgorithm.Sha2256);
                dataHasher.AddData(memoryStream);
                IKsiSignature signature = ksi.Sign(dataHasher.GetHash());

                VerificationContext verificationContext = new VerificationContext(signature)
                {
                    DocumentHash = new DataHash(HashAlgorithm.Sha2256,
                                                Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")),
                    PublicationsFile = ksi.GetPublicationsFile()
                };
                KeyBasedVerificationPolicy policy = new KeyBasedVerificationPolicy();

                verificationResult = policy.Verify(verificationContext);
            }

            Assert.AreEqual(VerificationResultCode.Fail, verificationResult.ResultCode, "Invalid hash should not verify with key based policy");
            Assert.AreEqual(VerificationError.Gen01, verificationResult.VerificationError);
        }