/// <summary> /// Generates the hash of a <see cref="BlockHeader"/>. /// </summary> /// <returns>A hash.</returns> public virtual uint256 GetHash() { uint256 hash = null; uint256[] hashes = this.hashes; if (hashes != null) { hash = hashes[0]; } if (hash != null) { return(hash); } using (var hs = new HashStream()) { this.ReadWrite(new BitcoinStream(hs, true)); hash = hs.GetHash(); } hashes = this.hashes; if (hashes != null) { hashes[0] = hash; } return(hash); }
public override uint256 GetHash() { uint256 hash = null; uint256[] innerHashes = this.hashes; if (innerHashes != null) { hash = innerHashes[0]; } if (hash != null) { return(hash); } using (var hs = new HashStream()) { this.ReadWriteHashingStream(new BitcoinStream(hs, true)); hash = hs.GetHash(); } innerHashes = this.hashes; if (innerHashes != null) { innerHashes[0] = hash; } return(hash); }
public uint256 GetHash() { uint256 h = null; var hashes = _Hashes; if (hashes != null) { h = hashes[0]; } if (h != null) { return(h); } using (HashStream hs = new HashStream()) { this.ReadWrite(new BitcoinStream(hs, true)); h = hs.GetHash(); } hashes = _Hashes; if (hashes != null) { hashes[0] = h; } return(h); }
/// <summary> /// This method should be removed once PR https://github.com/MetacoSA/NBitcoin/pull/1005 /// will be merged to master and new package will be released. /// Original code stores the calculated hash and returns it once it has been calculated so it's more optimal /// </summary> public static uint256 GetHash(this Transaction tx, int maxArraySize) { // try to use original implementation because it's more efficient try { return(tx.GetHash()); } catch (ArgumentOutOfRangeException) { // catch ArgumentOutOfRangeException in case Tx is bigger than 1MB // and parse it again by increasing the maxArraySize } using var hs = new HashStream(); var stream = new BitcoinStream(hs, true) { TransactionOptions = TransactionOptions.None, ConsensusFactory = tx.GetConsensusFactory(), MaxArraySize = maxArraySize }; stream.SerializationTypeScope(SerializationType.Hash); tx.ReadWrite(stream); return(hs.GetHash()); }
/// <summary>Calculates the hash of a <see cref="BlockHeader"/>.</summary> protected virtual uint256 CalculateHash() { using (var hs = new HashStream()) { this.ReadWrite(new BitcoinStream(hs, true)); uint256 hash = hs.GetHash(); return(hash); } }
public void InitiateTaggedWorks() { #if HAS_SPAN var sha = new NBitcoin.Secp256k1.SHA256(); sha.InitializeTagged("lol"); sha.Write(new byte[32]); byte[] buff = new byte[32]; sha.GetHash(buff); Assert.Equal("9185788706ad8d475d2410ce07554aeff7a212418159a8fa8ef2b3cb4a883b62", new uint256(buff).ToString()); #endif HashStream stream = new HashStream(); stream.SingleSHA256 = true; stream.InitializeTagged("lol"); stream.Write(new byte[32], 0, 32); var actual = stream.GetHash(); Assert.Equal("9185788706ad8d475d2410ce07554aeff7a212418159a8fa8ef2b3cb4a883b62", actual.ToString()); }
/// <summary> /// Generates the hash of a <see cref="BlockHeader"/>. /// </summary> /// <returns>A hash.</returns> public virtual uint256 GetHash() { uint256 hash = null; uint256[] hashes = this.hashes; if (hashes != null) { hash = hashes[0]; } if (hash != null) { return(hash); } if (true) { if (this.version > 6 && false) { hash = Hashes.Hash256(this.ToBytes()); } else { hash = this.GetPoWHash(); } } else { using (HashStream hs = new HashStream()) { this.ReadWrite(new BitcoinStream(hs, true)); hash = hs.GetHash(); } } hashes = this.hashes; if (hashes != null) { hashes[0] = hash; } return(hash); }
public uint256 GetHash(NetworkOptions options) { uint256 hash = null; uint256[] hashes = this.hashes; if (hashes != null) { hash = hashes[0]; } if (hash != null) { return(hash); } if (options.IsProofOfStake) { if (this.version > 6) { hash = Hashes.Hash256(this.ToBytes()); } else { hash = this.GetPoWHash(); } } else { using (HashStream hs = new HashStream()) { this.ReadWrite(new BitcoinStream(hs, true)); hash = hs.GetHash(); } } hashes = this.hashes; if (hashes != null) { hashes[0] = hash; } return(hash); }
public uint256 GetHash() { uint256 h = null; var hashes = _Hashes; if (hashes != null) { h = hashes[0]; } if (h != null) { return(h); } if (Block.BlockSignature) { if (this.nVersion > 6) { h = Hashes.Hash256(this.ToBytes()); } else { h = this.GetPoWHash(); } } else { using (HashStream hs = new HashStream()) { this.ReadWrite(new BitcoinStream(hs, true)); h = hs.GetHash(); } } hashes = _Hashes; if (hashes != null) { hashes[0] = h; } return(h); }
public Hash128 GetHash() { return(m_Stream.GetHash().ToHash128()); }