Giter Site home page Giter Site logo

blockchain's Introduction

a simple blockchain sample in C# for .Net

A simple blockchain example in c# using SHA512 for hashing.

This lib can:

  • Generate a block with data.
  • Add that block to a chain (in this case a List)
  • Create a hash for that block
  • Create a hash based non a Nonce, a proof of work, or: mining a block.
  • Validate that hash and the hash of a previous block
  • Validate the whole chain

Enjoy

usage, as like in a Console application:

  using nl.hyperdata.blockchain;
  using System;
  using System.Linq;

  namespace BlockChainSample
  {
        class Program
        {
            private static Random random = new Random(DateTime.Now.Millisecond);
            private static readonly IBlock genesis = new Block(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });

            /** proof of work (https://en.wikipedia.org/wiki/Proof-of-work_system)  is set here: a hash needs 2 trailing zero bytes, increase the number of bytes to reduce the number of valid  hashes, and increse the proof of work time **/
            private static readonly byte[] difficulty = new byte[] { 0x00, 0x00 };

            static void Main(string[] args)
            {

                /** Initiate the chain, set a difficulty for genarating the hash (proof of work) 
                and define the genesis data (not really important) **/
                BlockChain chain = new BlockChain(difficulty, genesis);

                // ** start mining 20 blocks in a loop **/
                for (int i = 0; i < 20; i++)
                {
                    /** randomly generated dummy data **/
                    var data = Enumerable.Range(0, 256).Select(x => (byte)random.Next());

                    /** add the fresh, unhashed block with the data to the chain **/
                    chain.Add(new Block(data.ToArray()));

                    /**-> blockchain magic happens here <-**/

                    /** output the generated block details **/
                    Console.WriteLine(chain.LastOrDefault().ToString());

                    /** validate the chain (ie: is each block's hash valid and is the prevoius block has valid)
                    Console.WriteLine(String.Format("Chain is valid {0}:", chain.IsValid()));
                }

             }
        }
  }

blockchain's People

Contributors

casparkleijne avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.