public void hashing_stream_disposal() { var under = Substitute.For <Stream>(); var subject = new HashingReadStream(under, System.Security.Cryptography.MD5.Create()); subject.Dispose(); under.Received().Close(); }
public byte[] CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest, string hashAlgorithmName) { var hash = HashAlgorithm.Create(hashAlgorithmName); using (var getTx = Request(loadRequest)) { var hashStream = new HashingReadStream(getTx.RawBodyStream, hash); var storeRq = storeRequest.Data(hashStream, getTx.BodyReader.ExpectedLength).Build(); Request(storeRq).Dispose(); return(hashStream.GetHashValue()); } }
/// <summary> /// Request data from one resource and provide to another, calculating a /// hash of the cross-loaded data. This is done in a memory-efficient manner. /// If either source or destination return a non-success result (including redirects) /// an exception will be thrown /// </summary> /// <param name="loadRequest">Request that will provide body data (should be a GET or POST)</param> /// <param name="storeRequest">Request that will accept body data (should be a PUT or POST)</param> /// <param name="hashAlgorithmName">Name of hash algorithm to use (should be a name supported by <see cref="System.Security.Cryptography.HashAlgorithm"/>)</param> /// <exception cref="ShiftIt.Http.HttpTransferException">Response to the request was not a succesful HTTP status.</exception> /// <exception cref="System.Net.Sockets.SocketException">Low level transport exception occured.</exception> /// <exception cref="ShiftIt.Http.TimeoutException">A timeout occured during transfer.</exception> public byte[] CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest, string hashAlgorithmName) { var hash = HashAlgorithm.Create(hashAlgorithmName); using (var getTx = RequestOrThrow(loadRequest)) { var hashStream = new HashingReadStream(getTx.RawBodyStream, hash); var storeRq = storeRequest.Build(hashStream, getTx.BodyReader.ExpectedLength); using (RequestOrThrow(storeRq)) // dispose of the response stream { } return(hashStream.GetHashValue()); } }
/// <summary> /// Request data from one resource and provide to another, calculating a /// hash of the cross-loaded data. This is done in a memory-efficient manner. /// If either source or destination return a non-success result (including redirects) /// an exception will be thrown /// </summary> /// <param name="loadRequest">Request that will provide body data (should be a GET or POST)</param> /// <param name="storeRequest">Request that will accept body data (should be a PUT or POST)</param> /// <param name="hashAlgorithmName">Name of hash algorithm to use (should be a name supported by <see cref="System.Security.Cryptography.HashAlgorithm"/>)</param> /// <exception cref="ShiftIt.Http.HttpTransferException">Response to the request was not a succesful HTTP status.</exception> /// <exception cref="System.Net.Sockets.SocketException">Low level transport exception occured.</exception> /// <exception cref="ShiftIt.Http.TimeoutException">A timeout occured during transfer.</exception> public byte[] CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest, string hashAlgorithmName) { if (loadRequest == null) { throw new ArgumentNullException(nameof(loadRequest)); } if (storeRequest == null) { throw new ArgumentNullException(nameof(storeRequest)); } var hash = HashAlgorithm.Create(hashAlgorithmName ?? "SHA256"); using (var getTx = RequestOrThrow(loadRequest)) { var hashStream = new HashingReadStream(getTx.RawBodyStream, hash); var storeRq = storeRequest.Build(hashStream, getTx.BodyReader.ExpectedLength); using (RequestOrThrow(storeRq)) // dispose of the response stream { } return(hashStream.GetHashValue() ?? throw new Exception("System hash function returned a null result")); } }