Table of Contents

Class HashStream

Namespace
Amazon.Runtime.Internal.Util
Assembly
AWSSDK.Core.dll

A wrapper stream that calculates a hash of the base stream as it is being read. The calculated hash is only available after the stream is closed or CalculateHash is called. After calling CalculateHash, any further reads on the streams will not change the CalculatedHash. If an ExpectedHash is specified and is not equal to the calculated hash, Close or CalculateHash methods will throw an AmazonClientException. If CalculatedHash is calculated for only the portion of the stream that is read.

public abstract class HashStream : WrapperStream, IDisposable, IAsyncDisposable
Inheritance
HashStream
Implements
Derived
Inherited Members

Constructors

HashStream(Stream, byte[], long)

Initializes an HashStream with a hash algorithm and a base stream.

protected HashStream(Stream baseStream, byte[] expectedHash, long expectedLength)

Parameters

baseStream Stream

Stream to calculate hash for.

expectedHash byte[]

Expected hash. Will be compared against calculated hash on stream close. Pass in null to disable check.

expectedLength long

Expected length of the stream. If the reading stops before reaching this position, CalculatedHash will be set to empty array.

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

Properties

Algorithm

Algorithm to use to calculate hash.

protected IHashingWrapper Algorithm { get; set; }

Property Value

IHashingWrapper

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

CalculatedHash

Calculated hash for the stream. This value is set only after the stream is closed.

public byte[] CalculatedHash { get; protected set; }

Property Value

byte[]

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

CanSeek

Gets a value indicating whether the current stream supports seeking. HashStream does not support seeking, this will always be false.

public override bool CanSeek { get; }

Property Value

bool

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

CurrentPosition

Current position in the stream.

protected long CurrentPosition { get; }

Property Value

long

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

ExpectedHash

Expected hash value. Compared against CalculatedHash upon Close(). If the hashes are different, an AmazonClientException is thrown.

public byte[] ExpectedHash { get; }

Property Value

byte[]

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

ExpectedLength

Expected length of stream.

public long ExpectedLength { get; protected set; }

Property Value

long

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

FinishedHashing

True if hashing is finished and no more hashing should be done; otherwise false.

protected bool FinishedHashing { get; }

Property Value

bool

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

Length

Gets the overridden length used to construct the HashStream

public override long Length { get; }

Property Value

long

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

Position

Gets or sets the position within the current stream. HashStream does not support seeking, attempting to set Position will throw NotSupportedException.

public override long Position { get; set; }

Property Value

long

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

Methods

CalculateHash()

Calculates the hash for the stream so far and disables any further hashing.

public virtual void CalculateHash()

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

CompareHashes(byte[], byte[])

Compares two hashes (arrays of bytes).

protected static bool CompareHashes(byte[] expected, byte[] actual)

Parameters

expected byte[]

Expected hash.

actual byte[]

Actual hash.

Returns

bool

True if the hashes are identical; otherwise false.

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

Dispose(bool)

Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream.

protected override void Dispose(bool disposing)

Parameters

disposing bool

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

Read(byte[], int, int)

Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count int

The maximum number of bytes to be read from the current stream.

Returns

int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

ReadAsync(byte[], int, int, CancellationToken)

Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Parameters

buffer byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count int

The maximum number of bytes to be read from the current stream.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default value is System.Threading.CancellationToken.None.

Returns

Task<int>

A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

Reset()

Resets the hash stream to starting state. Use this if the underlying stream has been modified and needs to be rehashed without reconstructing the hierarchy.

public void Reset()

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.

Seek(long, SeekOrigin)

Sets the position within the current stream. HashStream does not support seeking, attempting to call Seek will throw NotSupportedException.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

A byte offset relative to the origin parameter.

origin SeekOrigin

A value of type System.IO.SeekOrigin indicating the reference point used to obtain the new position.

Returns

long

The new position within the current stream.

Exceptions

AmazonClientException

Exception thrown during Close() or CalculateHash(), if ExpectedHash is set and is different from CalculateHash that the stream calculates, provided that CalculatedHash is not a zero-length byte array.