Ejemplo n.º 1
0
        public Block CreateBlock(string dataGraphIri, string rawRdf, RdfFormat rdfFormat)
        {
            _logger.LogDebug("Creating block with graphIri '{0}' and rdfFormat '{1}'...", dataGraphIri, rdfFormat.GetJenaName());

            try
            {
                HashSet <Triple> triples = GetTriplesFromSerializedModel(rawRdf, rdfFormat);

                long   newIndex    = GenerateNewIndex();
                string newBlockIri = GenerateNewBlockIri(newIndex);

                LastBlockInfo lastBlockInfo         = _repositoryManager.GetLastBlockInfo();
                string        previousBlock         = lastBlockInfo.BlockIri;
                string        previousHash          = lastBlockInfo.BlockHash;
                string        timestamp             = TimestampCreator.CreateTimestampString();
                string        dataHash              = _hashingService.CalculateHash(triples);
                string        stringToCalculateHash = (newIndex + previousBlock + previousHash + timestamp + dataGraphIri + dataHash).Trim();
                string        hash = _hashCalculator.CalculateHash(stringToCalculateHash).ToLower();

                BlockHeader blockHeader = new BlockHeader(
                    dataGraphIri,
                    dataHash,
                    hash,
                    newIndex.ToString(),
                    previousBlock,
                    previousHash,
                    timestamp);
                BlockContent blockContent = new BlockContent(dataGraphIri, triples);

                Block blockToStore = new Block(newBlockIri, blockHeader, blockContent);

                _repositoryManager.PersistBlock(blockToStore, true);

                // MAYBE: Maybe this should be obtained from Triple Store in order to avoid some kind of inconsistency.
                _lastIndex++;

                return(GetBlock(newIndex.ToString()));
            }
            catch (ReadingBlockException ex)
            {
                string msg = "Exception was thrown while getting information about the last block.";
                throw new CreatingBlockException(msg, ex);
            }
            catch (RdfSerializationException ex)
            {
                string msg = String.Format("Exception was thrown while deserializing RDF model from '{0}' format.", rdfFormat);
                throw new CreatingBlockException(msg, ex);
            }
            catch (CalculatingHashException ex)
            {
                throw new CreatingBlockException("Exception was thrown while calculating hash.", ex);
            }
        }
        public void TestCreateTimestampFromDigest()
        {
            byte[] digest = new System.Security.Cryptography.SHA1Cng().ComputeHash(Encoding.UTF8.GetBytes(dataToTimestamp));

            TimestampCreator creator   = new TimestampCreator();
            TimestampObject  timestamp = creator
                                         .SetTsaPrimaryUrl(tsaUrl2)
                                         .SetHashAlgorithm(HashAlgorithm.SHA1)
                                         .SetOutputFormat(OutputFormat.TSR)
                                         .SetMessageDigestForTimestamping(digest)
                                         .CreateTimestamp();
        }
        public void TestCreateWrongUrl()
        {
            byte[] data = Encoding.UTF8.GetBytes(dataToTimestamp);

            TimestampCreator creator   = new TimestampCreator();
            TimestampObject  timestamp = creator
                                         .SetTsaPrimaryUrl("http://wrong.url")
                                         .SetTsaSecondaryUrl("http://alsowrong.url")
                                         .SetHashAlgorithm(HashAlgorithm.SHA1)
                                         .SetOutputFormat(OutputFormat.TSR)
                                         .SetDataForTimestamping(data)
                                         .CreateTimestamp();
        }
        public void TestVerifyTimestampUsingConfiguration()
        {
            byte[] data = Encoding.UTF8.GetBytes(dataToTimestamp);

            TimestampCreator creator   = new TimestampCreator();
            TimestampObject  timestamp = creator
                                         .SetDataForTimestamping(data)
                                         .CreateTimestamp();

            Assert.AreEqual(HashAlgorithm.SHA1, timestamp.HashAlgorithm);

            TimestampVerifier verifier          = new TimestampVerifier();
            TimestampObject   verifiedTimestamp = verifier.SetTimestampedData(data)
                                                  .SetTimestamp(timestamp.Timestamp)
                                                  .Verify();
        }
        public void TestCreateTimestampSecondaryTsa()
        {
            byte[] data = Encoding.UTF8.GetBytes(dataToTimestamp);

            TimestampCreator creator   = new TimestampCreator();
            TimestampObject  timestamp = creator
                                         .SetTsaPrimaryUrl("http://www.google.com:81")
                                         .SetTsaSecondaryUrl(tsaUrl2)
                                         .SetTsaTimeout(1000 * 3)
                                         .SetHashAlgorithm(HashAlgorithm.SHA1)
                                         .SetOutputFormat(OutputFormat.TSR)
                                         .SetDataForTimestamping(data)
                                         .CreateTimestamp();

            Assert.AreEqual(timestamp.TsaIssuer, "C=PL,O=Unizeto Technologies S.A.,OU=Certum Certification Authority,CN=Certum Trusted Network CA");
        }
        public void TestCreateTimestampUsingConfigurationFile()
        {
            byte[] data = Encoding.UTF8.GetBytes(dataToTimestamp);
            Utils.LoadConfigurationFile(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\src\\configuration.txt");
            TimestampCreator creator   = new TimestampCreator();
            TimestampObject  timestamp = creator
                                         .SetDataForTimestamping(data)
                                         .CreateTimestamp();

            Assert.AreEqual(HashAlgorithm.SHA1, timestamp.HashAlgorithm);

            timestamp = creator
                        .SetHashAlgorithm(HashAlgorithm.SHA256)
                        .CreateTimestamp();

            Assert.AreEqual(HashAlgorithm.SHA256, timestamp.HashAlgorithm);
        }
        public void TestVerifyTimestampSimple()
        {
            byte[] data = Encoding.UTF8.GetBytes(dataToTimestamp);

            TimestampCreator creator   = new TimestampCreator();
            TimestampObject  timestamp = creator
                                         .SetTsaPrimaryUrl(tsaUrl2)
                                         .SetHashAlgorithm(HashAlgorithm.SHA1)
                                         .SetOutputFormat(OutputFormat.TSR)
                                         .SetDataForTimestamping(data)
                                         .CreateTimestamp();

            Assert.AreEqual(HashAlgorithm.SHA1, timestamp.HashAlgorithm);

            TimestampVerifier verifier          = new TimestampVerifier();
            TimestampObject   verifiedTimestamp = verifier.SetTimestampedData(data)
                                                  .SetHashAlgorithm(HashAlgorithm.SHA1)
                                                  .SetTimestamp(timestamp.Timestamp)
                                                  .Verify();
        }
        public void TestCreateTimestampAndCheckWarning()
        {
            byte[] data = Encoding.UTF8.GetBytes(dataToTimestamp);

            TimestampCreator creator   = new TimestampCreator();
            TimestampObject  timestamp = creator
                                         .SetTsaPrimaryUrl(tsaUrl2)
                                         .SetHashAlgorithm(HashAlgorithm.SHA1)
                                         .SetOutputFormat(OutputFormat.TSR)
                                         .SetDataForTimestamping(data)
                                         .SetMinimimCertificateValidityPeriod(180)
                                         .CreateTimestamp();

            Assert.IsNull(timestamp.Warning);

            timestamp = creator
                        .SetHashAlgorithm(HashAlgorithm.SHA256)
                        .SetMinimimCertificateValidityPeriod(4000)
                        .CreateTimestamp();

            Assert.IsNotNull(timestamp.Warning);
        }