public static Address FromHash(byte[] bytes) { var hash = CryptoExtensions.SHA256(bytes); hash[0] = SystemOpcode; return(new Address(hash)); }
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)); }
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)); }
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]); }
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)); }
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)); }
// 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; } }
public static Hash CalculateBlockHash(byte[] content) { var hash = new Hash(CryptoExtensions.Sha256(content, 0, (uint)content.Length)); return(hash); }
public static Hash FromString(string str) { var bytes = CryptoExtensions.Sha256(str); return(new Hash(bytes)); }