Basic BlockChain and verification

This commit is contained in:
2019-05-23 00:19:23 +02:00
commit 3352ce5273
9 changed files with 244 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
/.vs/*
/BasicBlockChain/bin/*
/BasicBlockChain/obj/*
/packages/

25
BasicBlockChain.sln Normal file
View File

@@ -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

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

View File

@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{34581A96-29BE-4AB6-9298-BC1AD3E78369}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>BasicBlockChain</RootNamespace>
<AssemblyName>BasicBlockChain</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="VAR.Json, Version=1.0.6252.27492, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\VAR.Json.1.0.6252.27492\lib\net461\VAR.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Block.cs" />
<Compile Include="BlockChain.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

35
BasicBlockChain/Block.cs Normal file
View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace BasicBlockChain
{
public class BlockChain
{
public List<Block> Chain { get; } = new List<Block>();
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;
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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")]

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="VAR.Json" version="1.0.6252.27492" targetFramework="net472" />
</packages>