コード例 #1
0
ファイル: Address.cs プロジェクト: kr1992/PhantasmaChain
        public static Address FromHash(byte[] bytes)
        {
            var hash = CryptoExtensions.SHA256(bytes);

            hash[0] = SystemOpcode;
            return(new Address(hash));
        }
コード例 #2
0
        private static MerkleTreeNode Build(MerkleTreeNode[] leaves)
        {
            Throw.If(leaves.Length == 0, "leaves cant be empty list");

            if (leaves.Length == 1)
            {
                return(leaves[0]);
            }

            MerkleTreeNode[] parents = new MerkleTreeNode[(leaves.Length + 1) / 2];
            for (int i = 0; i < parents.Length; i++)
            {
                parents[i]           = new MerkleTreeNode();
                parents[i].LeftChild = leaves[i * 2];
                leaves[i * 2].Parent = parents[i];

                if (i * 2 + 1 == leaves.Length)
                {
                    parents[i].RightChild = parents[i].LeftChild;
                }
                else
                {
                    parents[i].RightChild    = leaves[i * 2 + 1];
                    leaves[i * 2 + 1].Parent = parents[i];
                }

                var temp = ByteArrayUtils.ConcatBytes(parents[i].LeftChild.Hash.ToByteArray(), parents[i].RightChild.Hash.ToByteArray());
                parents[i].Hash = new Hash(CryptoExtensions.Hash256(temp));
            }

            return(Build(parents));
        }
コード例 #3
0
        public static Address FromHash(byte[] input)
        {
            var hash  = CryptoExtensions.SHA256(input);
            var bytes = ByteArrayUtils.ConcatBytes(new byte[] { (byte)AddressKind.System, 0 }, hash);

            return(new Address(bytes));
        }
コード例 #4
0
        public bool VerifyContent(byte[] content, uint offset)
        {
            Throw.If(offset >= MaxDepthLeafCount, "Offset does not correspond to maximum depth leaf");

            var hash = new Hash(CryptoExtensions.Sha256(content, 0, (uint)content.Length));

            return(hash == _tree[offset]);
        }
コード例 #5
0
ファイル: Hash.cs プロジェクト: phamduchieuit/PhantasmaChain
        public static Hash FromBytes(byte[] input)
        {
            if (input.Length > Length)
            {
                input = CryptoExtensions.SHA256(input);
            }

            var bytes = new byte[Length];

            Array.Copy(input, bytes, input.Length);
            return(new Hash(bytes));
        }
コード例 #6
0
ファイル: Hash.cs プロジェクト: JustinR1/PhantasmaChain
        public static Hash FromBytes(byte[] input)
        {
            if (input.Length != Length) // NOTE this is actually problematic, better to separate into 2 methods
            {
                input = CryptoExtensions.SHA256(input);
            }

            var bytes = new byte[Length];

            Array.Copy(input, bytes, input.Length);
            return(new Hash(bytes));
        }
コード例 #7
0
        // chunkSize must be a power of 2
        public MerkleTree(byte[] content)
        {
            //Throw.If(content == null || content.Length < ChunkSize, "invalid content");
            Throw.If(content == null, "invalid content");

            var chunkCount = (uint)(content.Length / ChunkSize);

            if (chunkCount * ChunkSize < content.Length)
            {
                chunkCount++;
            }

            MaxDepthLeafCount = NextPowerOf2(chunkCount);

            //int maxLevel = 1;
            var  temp      = MaxDepthLeafCount;
            uint nodeCount = 0;

            while (temp > 0)
            {
                nodeCount += temp;
                temp      /= 2;
                //maxLevel++;
            }

            _tree = new Hash[nodeCount];

            //hash the maximum depth leaves of the tree
            for (int i = 0; i < MaxDepthLeafCount; i++)
            {
                Hash hash;

                var ofs = (uint)(i * ChunkSize);
                if (ofs < content.Length)
                {
                    var length = ChunkSize;
                    if (ofs + length > content.Length)
                    {
                        length = (uint)(content.Length - ofs);
                    }
                    hash = new Hash(CryptoExtensions.Sha256(content, ofs, length));
                }
                else
                {
                    hash = Hash.Null;
                }

                _tree[i] = hash;
            }

            //and how combine the leaf hashes in the branches
            uint prevOffset = 0;
            uint prevRows   = MaxDepthLeafCount;

            while (true)
            {
                uint rows = prevRows / 2;
                if (rows <= 0)
                {
                    break;
                }

                uint offset = prevOffset + prevRows;

                for (uint i = 0; i < rows; i++)
                {
                    uint childIndex = prevOffset + (i * 2);
                    var  left       = _tree[childIndex];
                    var  right      = _tree[childIndex + 1];
                    _tree[offset + i] = Hash.MerkleCombine(left, right);
                }

                prevOffset = offset;
                prevRows   = rows;
            }
        }
コード例 #8
0
        public static Hash CalculateBlockHash(byte[] content)
        {
            var hash = new Hash(CryptoExtensions.Sha256(content, 0, (uint)content.Length));

            return(hash);
        }
コード例 #9
0
ファイル: Hash.cs プロジェクト: JustinR1/PhantasmaChain
        public static Hash FromString(string str)
        {
            var bytes = CryptoExtensions.Sha256(str);

            return(new Hash(bytes));
        }