Example #1
0
        public static void Test_BlockchainWithDocumentsSignedAndGlobalSignature()
        {
            // Blockchain with the content having double signature and the block closure is guaranteed by digital signature

            System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
            var PublicKeyBase64 = Convert.ToBase64String(RSA.ExportCspBlob(false));

            Blockchain Blocks = new Blockchain(PublicKeyBase64, "Webmaster", "Phrases", Blockchain.BlockchainType.Binary, true);
            var        Test   = Blocks.Validate();

            byte[] Signature;
            bool   IsValid;

            Blockchain.Block Block1 = new Blockchain.Block(Blocks, "Hi my friends, I have a message for you");
            Signature = RSA.SignHash(Block1.HashBody(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            IsValid   = Block1.AddBodySignature(PublicKeyBase64, Signature, false); // Add signature to body
            Signature = RSA.SignHash(Block1.CalculateChecksumBytes(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            IsValid   = Block1.AddBlockSignature(Signature);                        // Close the block with the digital signature

            Blockchain.Block Block2 = new Blockchain.Block(Blocks, "This is a message number 2, signed");
            Signature = RSA.SignHash(Block2.HashBody(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            IsValid   = Block2.AddBodySignature(PublicKeyBase64, Signature, false); // Add signature to body
            Signature = RSA.SignHash(Block2.CalculateChecksumBytes(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            IsValid   = Block2.AddBlockSignature(Signature);                        // Close the block with the digital signature

            Blockchain.Block Block3 = new Blockchain.Block(Blocks, "In the last block I added the last message");
            Signature = RSA.SignHash(Block3.HashBody(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            IsValid   = Block3.AddBodySignature(PublicKeyBase64, Signature, false); // Add signature to body
            Signature = RSA.SignHash(Block3.CalculateChecksumBytes(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            IsValid   = Block3.AddBlockSignature(Signature);                        // Close the block with the digital signature

            int BlockError = Blocks.Validate();                                     // 0 = no error
            var LastBlock  = Blocks.GetLastBlock();
        }
Example #2
0
        public static void Test_BlockchainWithDocumentsSignedAndGlobalSignature()
        {
            // Blockchain with the content having double signature and the block closure is guaranteed by digital signature

            var rsa             = new System.Security.Cryptography.RSACryptoServiceProvider();
            var publicKeyBase64 = Convert.ToBase64String(rsa.ExportCspBlob(false));

            var  blocks = new Blockchain(new[] { publicKeyBase64 }, "Webmaster", "Phrases", Blockchain.BlockchainType.Binary, Blockchain.BlockSynchronization.AddInLocalAndSync, true);
            var  test   = blocks.Validate();
            bool isValid;

            var block1    = new Blockchain.Block(blocks, "Hi my friends, I have a message for you");
            var signature = rsa.SignHash(block1.HashBody(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));

            isValid   = block1.AddBodySignature(publicKeyBase64, signature, false); // Add signature to body
            signature = rsa.SignHash(block1.CalculateChecksumBytes(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            isValid   = block1.AddBlockSignature(signature);                        // Close the block with the digital signature

            var block2 = new Blockchain.Block(blocks, "This is a message number 2, signed");

            signature = rsa.SignHash(block2.HashBody(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            isValid   = block2.AddBodySignature(publicKeyBase64, signature, false); // Add signature to body
            signature = rsa.SignHash(block2.CalculateChecksumBytes(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            isValid   = block2.AddBlockSignature(signature);                        // Close the block with the digital signature

            var block3 = new Blockchain.Block(blocks, "In the last block I added the last message");

            signature = rsa.SignHash(block3.HashBody(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            isValid   = block3.AddBodySignature(publicKeyBase64, signature, false); // Add signature to body
            signature = rsa.SignHash(block3.CalculateChecksumBytes(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            isValid   = block3.AddBlockSignature(signature);                        // Close the block with the digital signature

            var blockError = blocks.Validate();                                     // 0 = no error
            var lastBlock  = blocks.GetLastBlock();
        }