Esempio n. 1
0
        private static byte[] GenerateHash(DataForGeningHash dataForGeningHash)
        {
            using (SHA256 sha = new SHA256Managed())
                using (MemoryStream stream = new MemoryStream())
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        if (dataForGeningHash.Block.Nonce == 0)
                        {
                            // Is only used once to generate the hash.
                            writer.Write(dataForGeningHash.MinersPublicKey);
                            writer.Write(dataForGeningHash.Block.Data);
                            writer.Write(dataForGeningHash.Block.TimeStamp.ToBinary());
                            writer.Write(dataForGeningHash.Block.PreviousHash);
                        }

                        if (dataForGeningHash.FailedHash != null)
                        {
                            // By including the previous failedHash we force
                            // serial validation.
                            writer.Write(dataForGeningHash.FailedHash);
                        }

                        writer.Write(dataForGeningHash.Block.Nonce);
                        var streamArray = stream.ToArray();
                        return(sha.ComputeHash(streamArray));
                    }
        }
Esempio n. 2
0
        public static byte[] MineHash(IBlock block, byte[] minersPublicKey)
        {
            if (Difficulty == null)
            {
                throw new ArgumentNullException(nameof(Difficulty));
            }

            byte[] hash = new byte[0];
            int    d    = Difficulty.Length;

            DataForGeningHash dataForGeningHash = new DataForGeningHash();

            dataForGeningHash.Block           = block;
            dataForGeningHash.MinersPublicKey = minersPublicKey;

            while (!hash.Take(d).SequenceEqual(Difficulty) && block.Nonce <= int.MaxValue)
            {
                dataForGeningHash.Block.Nonce++;
                hash = GenerateHash(dataForGeningHash);
                dataForGeningHash.FailedHash = hash;
            }

            return(hash);
        }