public void DeriveFrom() { byte[] foo = { 0x66, 0x6f, 0x6f }, bar = { 0x62, 0x61, 0x72 }; Assert.Equal( HashDigest <SHA1> .FromString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), HashDigest <SHA1> .DeriveFrom(foo) ); Assert.Equal( HashDigest <SHA1> .DeriveFrom(foo), HashDigest <SHA1> .DeriveFrom(ImmutableArray.Create(foo)) ); Assert.Equal( HashDigest <SHA1> .DeriveFrom(foo), HashDigest <SHA1> .DeriveFrom(foo.AsSpan()) ); Assert.Equal( HashDigest <SHA1> .FromString("62cdb7020ff920e5aa642c3d4066950dd1f01f4d"), HashDigest <SHA1> .DeriveFrom(bar) ); Assert.Equal( HashDigest <MD5> .FromString("acbd18db4cc2f85cedef654fccc4a4d8"), HashDigest <MD5> .DeriveFrom(foo) ); Assert.Equal( HashDigest <MD5> .FromString("37b51d194a7513e45b56f6524f2d51f2"), HashDigest <MD5> .DeriveFrom(bar) ); }
static MerkleTrie() { _codec = new Codec(); var bxNull = _codec.Encode(Null.Value); EmptyRootHash = HashDigest <SHA256> .DeriveFrom(bxNull); }
private static HashDigest <SHA256>?CalcualteTxHashes(IEnumerable <Transaction <T> > txs) { if (!txs.Any()) { return(null); } byte[][] serializedTxs = txs.Select(tx => tx.Serialize(true)).ToArray(); int txHashSourceLength = serializedTxs.Select(b => b.Length).Sum() + 2; var txHashSource = new byte[txHashSourceLength]; // Bencodex lists look like: l...e txHashSource[0] = 0x6c; txHashSource[txHashSourceLength - 1] = 0x65; int offset = 1; foreach (byte[] serializedTx in serializedTxs) { serializedTx.CopyTo(txHashSource, offset); offset += serializedTx.Length; } using SHA256 hashAlgo = SHA256.Create(); return(HashDigest <SHA256> .DeriveFrom(txHashSource)); }
public static HashDigest <SHA256>?ActionEvaluationsToHash( IEnumerable <ActionEvaluation> actionEvaluations) { ActionEvaluation actionEvaluation; var evaluations = actionEvaluations.ToList(); if (evaluations.Any()) { actionEvaluation = evaluations.Last(); } else { return((HashDigest <SHA256>?)null); } IImmutableSet <Address> updatedAddresses = actionEvaluation.OutputStates.UpdatedAddresses; var dict = Bencodex.Types.Dictionary.Empty; foreach (Address address in updatedAddresses) { dict.Add(address.ToHex(), actionEvaluation.OutputStates.GetState(address)); } return(HashDigest <SHA256> .DeriveFrom(new Codec().Encode(dict))); }
public void ToBencodex() { var buf = new byte[128]; var random = new Random(); random.NextBytes(buf); var hashDigest = HashDigest <SHA256> .DeriveFrom(buf); var valueNode = new HashNode(hashDigest); Assert.Equal((Binary)hashDigest.ToByteArray(), valueNode.ToBencodex()); }
public async Task ConstructDeterministic(int waitMilliseconds) { Address avatarAddress = new PrivateKey().ToAddress(); Address agentAddress = new PrivateKey().ToAddress(); AvatarState avatarStateA = GetNewAvatarState(avatarAddress, agentAddress); await Task.Delay(waitMilliseconds); AvatarState avatarStateB = GetNewAvatarState(avatarAddress, agentAddress); HashDigest <SHA256> Hash(AvatarState avatarState) => HashDigest <SHA256> .DeriveFrom(new Codec().Encode(avatarState.Serialize())); Assert.Equal(Hash(avatarStateA), Hash(avatarStateB)); }
public void DecodeValidFullNode() { var hashA = HashDigest <SHA256> .DeriveFrom(TestUtils.GetRandomBytes(128)); var hashB = HashDigest <SHA256> .DeriveFrom(TestUtils.GetRandomBytes(128)); var list = new List(new IValue[] { (Binary)hashA.ToByteArray(), Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, Null.Value, (Binary)hashB.ToByteArray(), }); Assert.Equal(17, list.Count); INode node = NodeDecoder.Decode(list); Assert.IsType <FullNode>(node); var fullNode = (FullNode)node; Assert.Equal(new HashNode(hashB), fullNode.Value); Assert.Equal(new HashNode(hashA), fullNode.Children[0]); for (int i = 1; i < 16; ++i) { Assert.Null(fullNode.Children[i]); } }
internal static HashDigest <SHA256> Hash(this INode node) { return(node is HashNode hashNode ? hashNode.HashDigest : HashDigest <SHA256> .DeriveFrom(node.Serialize())); }
public override void Set(IReadOnlyList <string> fields) { base.Set(fields); ItemId = HashDigest <SHA256> .DeriveFrom(Serialize().EncodeIntoChunks().SelectMany(b => b).ToArray()); }
public Row(Bencodex.Types.Dictionary serialized) : base(serialized) { ItemId = HashDigest <SHA256> .DeriveFrom(serialized.EncodeIntoChunks().SelectMany(b => b).ToArray()); }
public override void Set(IReadOnlyList <string> fields) { base.Set(fields); ItemId = HashDigest <SHA256> .DeriveFrom(_codec.Encode(Serialize())); }
public Row(Bencodex.Types.Dictionary serialized) : base(serialized) { ItemId = HashDigest <SHA256> .DeriveFrom(_codec.Encode(serialized)); }
internal static HashDigest <SHA256> Hash(this INode node) { return(node is HashNode hashNode ? hashNode.HashDigest : HashDigest <SHA256> .DeriveFrom(Codec.Encode(node.ToBencodex()))); }