From 3352ce5273fe2ad056e6764e9e778b4c3b568e07 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Thu, 23 May 2019 00:19:23 +0200 Subject: [PATCH] Basic BlockChain and verification --- .gitignore | 4 ++ BasicBlockChain.sln | 25 +++++++++ BasicBlockChain/App.config | 6 +++ BasicBlockChain/BasicBlockChain.csproj | 59 ++++++++++++++++++++++ BasicBlockChain/Block.cs | 35 +++++++++++++ BasicBlockChain/BlockChain.cs | 40 +++++++++++++++ BasicBlockChain/Program.cs | 35 +++++++++++++ BasicBlockChain/Properties/AssemblyInfo.cs | 36 +++++++++++++ BasicBlockChain/packages.config | 4 ++ 9 files changed, 244 insertions(+) create mode 100644 .gitignore create mode 100644 BasicBlockChain.sln create mode 100644 BasicBlockChain/App.config create mode 100644 BasicBlockChain/BasicBlockChain.csproj create mode 100644 BasicBlockChain/Block.cs create mode 100644 BasicBlockChain/BlockChain.cs create mode 100644 BasicBlockChain/Program.cs create mode 100644 BasicBlockChain/Properties/AssemblyInfo.cs create mode 100644 BasicBlockChain/packages.config diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..10c0290 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.vs/* +/BasicBlockChain/bin/* +/BasicBlockChain/obj/* +/packages/ diff --git a/BasicBlockChain.sln b/BasicBlockChain.sln new file mode 100644 index 0000000..19b8c80 --- /dev/null +++ b/BasicBlockChain.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.28803.452 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicBlockChain", "BasicBlockChain\BasicBlockChain.csproj", "{34581A96-29BE-4AB6-9298-BC1AD3E78369}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {34581A96-29BE-4AB6-9298-BC1AD3E78369}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34581A96-29BE-4AB6-9298-BC1AD3E78369}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34581A96-29BE-4AB6-9298-BC1AD3E78369}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34581A96-29BE-4AB6-9298-BC1AD3E78369}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {48CECB6D-7E06-474E-9AB9-3EC17F135997} + EndGlobalSection +EndGlobal diff --git a/BasicBlockChain/App.config b/BasicBlockChain/App.config new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ b/BasicBlockChain/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BasicBlockChain/BasicBlockChain.csproj b/BasicBlockChain/BasicBlockChain.csproj new file mode 100644 index 0000000..425c00d --- /dev/null +++ b/BasicBlockChain/BasicBlockChain.csproj @@ -0,0 +1,59 @@ + + + + + Debug + AnyCPU + {34581A96-29BE-4AB6-9298-BC1AD3E78369} + Exe + BasicBlockChain + BasicBlockChain + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + ..\packages\VAR.Json.1.0.6252.27492\lib\net461\VAR.Json.dll + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BasicBlockChain/Block.cs b/BasicBlockChain/Block.cs new file mode 100644 index 0000000..27c186b --- /dev/null +++ b/BasicBlockChain/Block.cs @@ -0,0 +1,35 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace BasicBlockChain +{ + public class Block + { + public int Index { get; set; } + public DateTime Date { get; set; } + public string PreviousHash { get; set; } + public string Data { get; set; } + public string Hash { get; set; } + + public Block(DateTime date, Block previousBlock, string data) + { + Index = (previousBlock?.Index ?? -1) + 1; + Date = date; + PreviousHash = previousBlock?.Hash; + Data = data; + Hash = CalculateHash(); + } + + public string CalculateHash() + { + SHA256 sha256 = SHA256.Create(); + + byte[] dataBytes = Encoding.UTF8.GetBytes(string.Format("{0}-{1}-{2}", Date, PreviousHash, Data)); + byte[] hashBytes = sha256.ComputeHash(dataBytes); + + string hash = Convert.ToBase64String(hashBytes); + return hash; + } + } +} diff --git a/BasicBlockChain/BlockChain.cs b/BasicBlockChain/BlockChain.cs new file mode 100644 index 0000000..6b472b5 --- /dev/null +++ b/BasicBlockChain/BlockChain.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace BasicBlockChain +{ + public class BlockChain + { + public List Chain { get; } = new List(); + + public BlockChain(DateTime? genesisDate = null) + { + Block genesisBlock = new Block(genesisDate ?? DateTime.UtcNow, null, "{}"); + Chain.Add(genesisBlock); + } + + public void AddData(DateTime date, string data) + { + Block lastBlock = Chain.Last(); + Block newBlock = new Block(date, lastBlock, data); + Chain.Add(newBlock); + } + + public bool Verify() + { + for (int i = 1; i < Chain.Count; i++) + { + Block currentBlock = Chain[i]; + Block previousBlock = Chain[i - 1]; + + string currentBlockHashRecalculated = currentBlock.CalculateHash(); + if (currentBlock.Hash != currentBlockHashRecalculated || currentBlock.PreviousHash != previousBlock.Hash) + { + return false; + } + } + return true; + } + } +} diff --git a/BasicBlockChain/Program.cs b/BasicBlockChain/Program.cs new file mode 100644 index 0000000..a895bc9 --- /dev/null +++ b/BasicBlockChain/Program.cs @@ -0,0 +1,35 @@ +using System; + +namespace BasicBlockChain +{ + internal class Program + { + private static void Main(string[] args) + { + VAR.Json.JsonWriter jsonWriter = new VAR.Json.JsonWriter(1); + + // Example BlockChain with some example data + Console.WriteLine(); + Console.WriteLine("#### BlockChain with sample data"); + BlockChain nullCoin = new BlockChain(new DateTime(2000, 1, 1)); + nullCoin.AddData(new DateTime(2000, 1, 2), "{ sender: VAR, receiver: NAM, amount: 10.00}"); + nullCoin.AddData(new DateTime(2000, 1, 3), "{ sender: NAM, receiver: VAR, amount: 5.00}"); + nullCoin.AddData(new DateTime(2000, 1, 4), "{ sender: NAM, receiver: VAR, amount: 5.00}"); + Console.WriteLine(jsonWriter.Write(nullCoin)); + + // Verify + Console.WriteLine("BlockChain is Valid? {0}", nullCoin.Verify() ? "True" : "False"); + + // Tamper with the data + Console.WriteLine(); + Console.WriteLine("#### Tampering with the data"); + nullCoin.Chain[1].Data = "{ sender: VAR, receiver: NAM, amount: 1000.00}"; + Console.WriteLine(jsonWriter.Write(nullCoin)); + + // Verify + Console.WriteLine("BlockChain is Valid? {0}", nullCoin.Verify() ? "True" : "False"); + + Console.Read(); + } + } +} diff --git a/BasicBlockChain/Properties/AssemblyInfo.cs b/BasicBlockChain/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..03b87f6 --- /dev/null +++ b/BasicBlockChain/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BasicBlockChain")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BasicBlockChain")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("34581a96-29be-4ab6-9298-bc1ad3e78369")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/BasicBlockChain/packages.config b/BasicBlockChain/packages.config new file mode 100644 index 0000000..ca3a1df --- /dev/null +++ b/BasicBlockChain/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file