protected virtual void HandleException(IExecutionContext executionContext, Exception exception) { PutObjectRequest putObjectRequest = executionContext.get_RequestContext().get_OriginalRequest() as PutObjectRequest; if (putObjectRequest != null) { HashStream val = putObjectRequest.InputStream as HashStream; if (val != null) { putObjectRequest.InputStream = val.GetNonWrapperBaseStream(); } } UploadPartRequest uploadPartRequest = executionContext.get_RequestContext().get_OriginalRequest() as UploadPartRequest; if (uploadPartRequest != null) { HashStream val2 = uploadPartRequest.InputStream as HashStream; if (val2 != null) { uploadPartRequest.InputStream = val2.GetNonWrapperBaseStream(); } } if (executionContext.get_RequestContext().get_Request() != null) { AmazonS3Client.CleanupRequest(executionContext.get_RequestContext().get_Request()); } }
protected override void StartPrimarySignatureCore(SecurityToken token, SecurityKeyIdentifier keyIdentifier, MessagePartSpecification signatureParts, bool generateTargettableSignature) { string str3; XmlDictionaryString str4; SecurityAlgorithmSuite algorithmSuite = base.AlgorithmSuite; string defaultCanonicalizationAlgorithm = algorithmSuite.DefaultCanonicalizationAlgorithm; XmlDictionaryString defaultCanonicalizationAlgorithmDictionaryString = algorithmSuite.DefaultCanonicalizationAlgorithmDictionaryString; if (defaultCanonicalizationAlgorithm != "http://www.w3.org/2001/10/xml-exc-c14n#") { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(System.ServiceModel.SR.GetString("UnsupportedCanonicalizationAlgorithm", new object[] { algorithmSuite.DefaultCanonicalizationAlgorithm }))); } algorithmSuite.GetSignatureAlgorithmAndKey(token, out str3, out this.signatureKey, out str4); string defaultDigestAlgorithm = algorithmSuite.DefaultDigestAlgorithm; XmlDictionaryString defaultDigestAlgorithmDictionaryString = algorithmSuite.DefaultDigestAlgorithmDictionaryString; this.signedInfo = new PreDigestedSignedInfo(ServiceModelDictionaryManager.Instance, defaultCanonicalizationAlgorithm, defaultCanonicalizationAlgorithmDictionaryString, defaultDigestAlgorithm, defaultDigestAlgorithmDictionaryString, str3, str4); this.signedXml = new SignedXml(this.signedInfo, ServiceModelDictionaryManager.Instance, base.StandardsManager.SecurityTokenSerializer); if (keyIdentifier != null) { this.signedXml.Signature.KeyIdentifier = keyIdentifier; } if (generateTargettableSignature) { this.signedXml.Id = base.GenerateId(); } this.effectiveSignatureParts = signatureParts; this.hashStream = this.signedInfo.ResourcePool.TakeHashStream(defaultDigestAlgorithm); }
public StreamHasher() { var hasher = HashingMethods.GetHashAlgorithm(); m_Stream = new HashStream(hasher); Writer = new BinaryWriter(m_Stream); }
public async Task TestHashCalculationAsync() { FileInfo fileInfo = new FileInfo(GetType().Assembly.Location); byte[] hash; using (FileStream fileStream = fileInfo.OpenRead()) using (HashAlgorithm hashCalculator = HashAlgorithm.Create(HashAlgorithmName.Name)) { hash = hashCalculator.ComputeHash(fileStream); } HashStream hashStream; using (FileStream fileStream = fileInfo.OpenRead()) using (RandomReadNumberStream randomReadStream = new RandomReadNumberStream(fileStream)) using (hashStream = new HashStream(randomReadStream, CryptoStreamMode.Read, HashAlgorithmName)) { await hashStream.CopyToAsync(Stream.Null); } Assert.AreEqual(hash, hashStream.HashResult.HashBytes); using (FileStream fileStream = fileInfo.OpenRead()) using (RandomReadNumberStream randomReadStream = new RandomReadNumberStream(fileStream)) using (hashStream = new HashStream(Stream.Null, CryptoStreamMode.Write, HashAlgorithmName)) { await randomReadStream.CopyToAsync(hashStream); } Assert.AreEqual(hash, hashStream.HashResult.HashBytes); }
protected void SetupFlexibleChecksumStream(IWebResponseData responseData, Stream responseStream, long contentLength, IRequestContext requestContext) { var algorithm = ChecksumUtils.SelectChecksumForResponseValidation(requestContext?.OriginalRequest?.ChecksumResponseAlgorithms, responseData); if (algorithm == CoreChecksumAlgorithm.NONE) { return; } ChecksumAlgorithm = algorithm; ExpectedFlexibleChecksumResult = responseData.GetHeaderValue(ChecksumUtils.GetChecksumHeaderKey(algorithm)); var checksum = Convert.FromBase64String(ExpectedFlexibleChecksumResult); switch (algorithm) { case CoreChecksumAlgorithm.CRC32C: FlexibleChecksumStream = new HashStream <HashingWrapperCRC32C>(responseStream, checksum, contentLength); break; case CoreChecksumAlgorithm.CRC32: FlexibleChecksumStream = new HashStream <HashingWrapperCRC32>(responseStream, checksum, contentLength); break; case CoreChecksumAlgorithm.SHA256: FlexibleChecksumStream = new HashStream <HashingWrapperSHA256>(responseStream, checksum, contentLength); break; case CoreChecksumAlgorithm.SHA1: FlexibleChecksumStream = new HashStream <HashingWrapperSHA1>(responseStream, checksum, contentLength); break; default: throw new AmazonClientException($"Unsupported checksum algorithm {algorithm}"); } }
internal void WriteTo(XmlWriter writer, SamlSerializer samlSerializer, SecurityTokenSerializer keyInfoSerializer) { if (writer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer"); } if ((this.signingCredentials == null) && (this.signature == null)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.IdentityModel.SR.GetString("SamlAssertionMissingSigningCredentials"))); } XmlDictionaryWriter innerWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer); if (this.signingCredentials != null) { using (HashAlgorithm algorithm = CryptoHelper.CreateHashAlgorithm(this.signingCredentials.DigestAlgorithm)) { this.hashStream = new HashStream(algorithm); this.keyInfoSerializer = keyInfoSerializer; this.dictionaryManager = samlSerializer.DictionaryManager; SamlDelegatingWriter writer3 = new SamlDelegatingWriter(innerWriter, this.hashStream, this, samlSerializer.DictionaryManager.ParentDictionary); this.WriteXml(writer3, samlSerializer, keyInfoSerializer); return; } } this.tokenStream.SetElementExclusion(null, null); this.tokenStream.WriteTo(innerWriter, samlSerializer.DictionaryManager); }
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); }
/// <summary> /// Catch exceptions and clean up any open streams. /// </summary> /// <param name="executionContext"></param> /// <param name="exception"></param> protected virtual void HandleException(IExecutionContext executionContext, Exception exception) { var putObjectRequest = executionContext.RequestContext.OriginalRequest as PutObjectRequest; if (putObjectRequest != null) { // If InputStream was a HashStream, compare calculated hash to returned etag HashStream hashStream = putObjectRequest.InputStream as HashStream; if (hashStream != null) { // Set InputStream to its original value putObjectRequest.InputStream = hashStream.GetNonWrapperBaseStream(); } } var uploadPartRequest = executionContext.RequestContext.OriginalRequest as UploadPartRequest; if (uploadPartRequest != null) { // If InputStream was a HashStream, compare calculated hash to returned etag HashStream hashStream = uploadPartRequest.InputStream as HashStream; if (hashStream != null) { // Set InputStream to its original value uploadPartRequest.InputStream = hashStream.GetNonWrapperBaseStream(); } } AmazonS3Client.CleanupRequest(executionContext.RequestContext.OriginalRequest); }
} // func ReadStreamData private static (string hashName, byte[] hashValue) CopyData(bool shouldDeflate, Stream srcStream, byte[] buf, int readed, Stream dstStream) { // pack destination stream var dst = shouldDeflate ? new GZipStream(dstStream, CompressionMode.Compress, true) : dstStream; using (var dstHashStream = new HashStream(dst, HashStreamDirection.Write, false, SHA256.Create())) { try { // copy stream into file dstHashStream.Write(buf, 0, readed); srcStream.CopyTo(dst); } finally { dstHashStream.Flush(); dstHashStream.Dispose(); } dstHashStream.Close(); return("SHA2_256", dstHashStream.HashSum); } } // func CopyData
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> /// 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 static void ComputeString(HashAlgorithm hashAlgorithm, IUVStream<ArraySegment<byte>> stream, Action<string> callback) { var hs = new HashStream(hashAlgorithm, stream); hs.Complete += () => { callback(hs.HashString); hs.Dispose(); }; }
public static void Compute(HashAlgorithm hashAlgorithm, IUVStream stream, Action<byte[]> callback) { var hs = new HashStream(hashAlgorithm, stream); hs.Complete += () => { callback(hs.Hash); hs.Dispose(); }; }
public void TestHashStreamDisposed() { using (HashStream hs = new HashStream(new SHA256Managed())) { hs.Close(); hs.Close(); //<- fails, already closed and/or disposed } }
private static BitcoinStream CreateHashWriter(HashVersion version) { HashStream hs = new HashStream(); BitcoinStream stream = new BitcoinStream(hs, true); stream.Type = SerializationType.Hash; stream.TransactionOptions = version == HashVersion.Original ? TransactionOptions.None : TransactionOptions.Witness; return stream; }
public static void Compute(HashAlgorithm hashAlgorithm, IUVStream <ArraySegment <byte> > stream, Action <byte[]> callback) { var hs = new HashStream(hashAlgorithm, stream); hs.Complete += () => { callback(hs.Hash); hs.Dispose(); }; }
public static void ComputeString(HashAlgorithm hashAlgorithm, IUVStream stream, Action <string> callback) { var hs = new HashStream(hashAlgorithm, stream); hs.Complete += () => { callback(hs.HashString); hs.Dispose(); }; }
/// <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 static void Compute(Loop loop, string file) { var @in = new UVFileStream(loop); @in.Open(file, UVFileAccess.Read, (ex) => { HashStream.ComputeString(SHA1Managed.Create(), @in, (str) => { Console.WriteLine("{0} {1}", str, file); }); @in.Resume(); }); }
protected override ISignatureValueSecurityElement CompletePrimarySignatureCore(SendSecurityHeaderElement[] signatureConfirmations, SecurityToken[] signedEndorsingTokens, SecurityToken[] signedTokens, SendSecurityHeaderElement[] basicTokens) { ISignatureValueSecurityElement signedXml; if (this.signedXml == null) { return(null); } SecurityTimestamp timestamp = base.Timestamp; if (timestamp != null) { if (timestamp.Id == null) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("TimestampToSignHasNoId"))); } HashStream stream = this.TakeHashStream(); base.StandardsManager.WSUtilitySpecificationVersion.WriteTimestampCanonicalForm(stream, timestamp, this.signedInfo.ResourcePool.TakeEncodingBuffer()); this.signedInfo.AddReference(timestamp.Id, stream.FlushHashAndGetValue()); } if ((base.ShouldSignToHeader && (this.signatureKey is AsymmetricSecurityKey)) && (base.Version.Addressing != AddressingVersion.None)) { if (this.toHeaderHash == null) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("TransportSecurityRequireToHeader"))); } this.signedInfo.AddReference(this.toHeaderId, this.toHeaderHash); } this.AddSignatureReference(signatureConfirmations); if (base.RequireMessageProtection) { this.AddSignatureReference(signedEndorsingTokens); this.AddSignatureReference(signedTokens); this.AddSignatureReference(basicTokens); } if (this.signedInfo.ReferenceCount == 0) { throw TraceUtility.ThrowHelperError(new MessageSecurityException(System.ServiceModel.SR.GetString("NoPartsOfMessageMatchedPartsToSign")), base.Message); } try { this.signedXml.ComputeSignature(this.signatureKey); signedXml = this.signedXml; } finally { this.hashStream = null; this.signedInfo = null; this.signedXml = null; this.signatureKey = null; this.effectiveSignatureParts = null; } return(signedXml); }
protected static BitcoinStream CreateHashWriter(HashVersion version) { var hs = new HashStream(); var stream = new BitcoinStream(hs, true) { Type = SerializationType.Hash, TransactionOptions = version == HashVersion.Original ? TransactionOptions.None : TransactionOptions.Witness }; return(stream); }
private async Task <HashResult> GetExampleHashResult() { FileInfo fileInfo = new FileInfo(GetType().Assembly.Location); HashStream hashStream; using (FileStream fileStream = fileInfo.OpenRead()) using (hashStream = new HashStream(HashAlgorithmName.SHA256)) { await fileStream.CopyToAsync(hashStream); } return(hashStream.HashResult); }
private HashStream TakeHashStream() { HashStream hashStream = null; if (this.hashStream == null) { this.hashStream = hashStream = new HashStream(System.ServiceModel.Security.CryptoHelper.CreateHashAlgorithm(base.AlgorithmSuite.DefaultDigestAlgorithm)); return(hashStream); } hashStream = this.hashStream; hashStream.Reset(); return(hashStream); }
public override int Read(byte[] buffer, int offset, int count) { HashStream.Position = CurrentSector * DigestSize; HashStream.Read(_hashBuffer, 0, DigestSize); int bytesRead = base.Read(buffer, 0, count); // If a hash is zero the data for the entire block is zero if (Type == IntegrityStreamType.Save && _hashBuffer.IsEmpty()) { Array.Clear(buffer, 0, SectorSize); return(bytesRead); } if (bytesRead < SectorSize) { // Pad out unused portion of block Array.Clear(buffer, bytesRead, SectorSize - bytesRead); } if (!EnableIntegrityChecks) { return(bytesRead); } _hash.Initialize(); if (Type == IntegrityStreamType.Save) { _hash.TransformBlock(Salt, 0, Salt.Length, null, 0); } _hash.TransformBlock(buffer, 0, SectorSize, null, 0); _hash.TransformFinalBlock(buffer, 0, 0); byte[] hash = _hash.Hash; if (Type == IntegrityStreamType.Save) { // This bit is set on all save hashes hash[0x1F] |= 0x80; } if (!Util.ArraysEqual(_hashBuffer, hash)) { throw new InvalidDataException("Hash error!"); } return(bytesRead); }
internal static void PrepareForRetry(IRequestContext requestContext) { if (requestContext.Request.ContentStream != null && requestContext.Request.OriginalStreamPosition >= 0) { Stream stream = requestContext.Request.ContentStream; HashStream hashStream = stream as HashStream; if (hashStream != null) { hashStream.Reset(); stream = hashStream.GetSeekableBaseStream(); } stream.Position = requestContext.Request.OriginalStreamPosition; } }
public void TestHashStreamWrite() { Random r = new Random(); byte[][] test = new byte[][] { new byte[300], new byte[1], new byte[500], new byte[11], new byte[1], new byte[1000], }; using (HashStream hs = new HashStream(new SHA256Managed())) using (MemoryStream ms = new MemoryStream()) using (HashStream hsWrap = new HashStream(new SHA256Managed(), ms)) { Assert.IsTrue(hs.CanWrite); long len = 0; foreach (byte[] bytes in test) { len += bytes.Length; r.NextBytes(bytes); hsWrap.Write(bytes, 0, bytes.Length); hs.Write(bytes, 0, bytes.Length); } for (int i = 0; i < 5; i++) { len += 1; byte val = (byte)r.Next(255); hsWrap.WriteByte(val); hs.WriteByte(val); } Assert.AreEqual(len, ms.Position); Hash expect = Hash.SHA256(ms.ToArray()); Hash actual = hs.Close(); Assert.AreEqual(expect, actual); Assert.AreEqual(expect.ToArray(), actual.ToArray()); Assert.AreEqual(expect.ToString(), actual.ToString()); //wrapped test actual = hsWrap.FinalizeHash(); Assert.AreEqual(expect, actual); Assert.AreEqual(expect.ToArray(), actual.ToArray()); Assert.AreEqual(expect.ToString(), actual.ToString()); } }
private void AddSignatureReference(SecurityToken token) { if (token.Id == null) { throw TraceUtility.ThrowHelperError(new MessageSecurityException(System.ServiceModel.SR.GetString("ElementToSignMustHaveId")), base.Message); } HashStream stream = this.TakeHashStream(); XmlDictionaryWriter writer = this.TakeUtf8Writer(); writer.StartCanonicalization(stream, false, null); base.StandardsManager.SecurityTokenSerializer.WriteToken(writer, token); writer.EndCanonicalization(); this.signedInfo.AddReference(token.Id, stream.FlushHashAndGetValue()); }
private HashStream TakeHashStream() { HashStream hashStream = null; if (_hashStream == null) { _hashStream = hashStream = new HashStream(CryptoHelper.CreateHashAlgorithm(AlgorithmSuite.DefaultDigestAlgorithm)); } else { hashStream = _hashStream; ; hashStream.Reset(); } return(hashStream); }
/// <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 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()); }
public async Task TestNoStreamConstructor() { FileInfo fileInfo = new FileInfo(GetType().Assembly.Location); HashStream hashStream1, hashStream2; using (FileStream fileStream = fileInfo.OpenRead()) using (hashStream1 = new HashStream(fileStream, CryptoStreamMode.Read, HashAlgorithmName)) using (hashStream2 = new HashStream(HashAlgorithmName)) { await hashStream1.CopyToAsync(Stream.Null); fileStream.Seek(0, SeekOrigin.Begin); await fileStream.CopyToAsync(hashStream2); } Assert.AreEqual(hashStream1.HashResult, hashStream2.HashResult); }