Пример #1
0
        public static string ComputeHash(int _time, string _PrevHash, string _dataS, string _Owner, string SHA256_Master) // Compute Hash for Block Data
        {
            string HashPrep = Convert.ToString(_time) + _PrevHash + Convert.ToString(_dataS.Length) + _dataS + Convert.ToString(_Owner.Length) + _Owner;

            SHA256 sha = SHA256.Create();

            sha.ComputeHash(Encoding.UTF8.GetBytes(HashPrep));

            string newHashVal = SharpChainUtils.HashByteArray(sha.Hash);

            sha = SHA256.Create();
            sha.ComputeHash(Encoding.UTF8.GetBytes(SHA256_Master + newHashVal));

            string generatedHash = SharpChainUtils.HashByteArray(sha.Hash);

            return(generatedHash);
        }
Пример #2
0
        private void WriteBlock(string filename, string data, string owner, string prevHash, int BlockIndex) // (Physical) Write Block to Blockchain
        {
            if (prevHash == null)
            {
                prevHash = "0000000000000000000000000000000000000000000000000000000000000000"; // Genesis Hash
            }

            FileStream fs = File.Open(filename, FileMode.Append);

            int    time     = (int)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
            string HashPrep = Convert.ToString(time) + prevHash + Convert.ToString(data.Length) + data + Convert.ToString(owner.Length) + owner;
            SHA256 sha      = SHA256.Create();

            sha.ComputeHash(Encoding.UTF8.GetBytes(HashPrep));

            string HashVal = SharpChainUtils.HashByteArray(sha.Hash);

            sha = SHA256.Create();
            sha.ComputeHash(Encoding.UTF8.GetBytes(SHA256_Master + HashVal));

            string MasterHashVal = SharpChainUtils.HashByteArray(sha.Hash);

            int bytesDataLength      = BitConverter.GetBytes(data.Length).Length;
            int bytesOwnerDataLength = BitConverter.GetBytes(owner.Length).Length;

            SharpChainBlock block = new SharpChainBlock(time, MasterHashVal, prevHash, data, fs.Name);

            SharpChainIndex.UpdateIndex(fs.Name, block, (int)fs.Length, bytesDataLength + data.Length + 139 + bytesOwnerDataLength + owner.Length);

            // Write to SharpChain
            fs.Write(BitConverter.GetBytes(this.Magic), 0, 3);                      // Byte: 0
            fs.Write(BitConverter.GetBytes(BlockIndex), 0, 1);                      // Byte: 0
            fs.Write(BitConverter.GetBytes('\x01'), 0, 1);                          // Byte: 4
            fs.Write(BitConverter.GetBytes(time), 0, 4);                            //Byte: 5
            fs.Write(BitConverter.GetBytes(data.Length), 0, bytesDataLength);       // Byte: 10
            fs.Write(Encoding.ASCII.GetBytes(data), 0, data.Length);                // Byte: 13 + data.Length
            fs.Write(Encoding.ASCII.GetBytes(prevHash), 0, 64);                     // Byte: 13 + data.Length + 64
            fs.Write(Encoding.ASCII.GetBytes(MasterHashVal), 0, 64);                // Byte: 13 + data.Length + 128
            fs.Write(BitConverter.GetBytes(owner.Length), 0, bytesOwnerDataLength); // Byte: 10
            fs.Write(Encoding.ASCII.GetBytes(owner), 0, owner.Length);              // Byte: 13 + data.Length
            fs.Write(BitConverter.GetBytes('\0'), 0, 1);                            // Byte: 13 + data.Length + 128 + 1 (END OF BLOCK!)

            fs.Flush();
            fs.Close();
        }