Перейти к основному содержимому

AesStream

sealed class

Provides a stream that encrypts what is written to an underlying stream and decrypts what is read from it, once encryption is enabled.

Declaration

public sealed class AesStream : Stream
NamespaceMcProtoNet.Transport.Cryptography
AssemblyMcProtoNet.Transport.dll
Sourcesrc/McProtoNet.Transport/Cryptography/AesStream.cs#L17 ↗

Inheritance: ObjectMarshalByRefObjectAesStream

Remarks

Until AesStream.EnableEncryption or AesStream.EnableEncryption is called, every read and write passes through unchanged. Encryption can be enabled only once. The stream does not support seeking. Reads and writes each advance their own cipher, so one thread at a time may read and one thread at a time may write.

Constructors

SignatureDescription
AesStream(Stream baseStream, bool leaveOpen = false)Initializes a new instance of the AesStream class over the specified stream.

AesStream(Stream, bool)

public AesStream(Stream baseStream, bool leaveOpen = false)

Initializes a new instance of the AesStream class over the specified stream.

Parameters: baseStream (Stream), leaveOpen (Boolean)

Exceptions: ArgumentNullException - <code class="paramref">baseStream</code> is <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null"&gt;null&lt;/a>.

Properties

SignatureDescription
Stream BaseStreamGets the stream that this instance reads from and writes to.
bool CanReadWhen overridden in a derived class, gets a value indicating whether the current stream supports reading.
bool CanSeekGets a value indicating whether the stream supports seeking.
bool CanWriteWhen overridden in a derived class, gets a value indicating whether the current stream supports writing.
bool EncryptionEnabledGets a value indicating whether encryption is enabled on the stream.
long LengthGets the length of the stream.
long PositionGets or sets the position within the stream.

BaseStream

public Stream BaseStream { get; }

Gets the stream that this instance reads from and writes to.

Returns: Stream

Exceptions: ObjectDisposedException - The current instance has already been disposed.

CanRead

public override bool CanRead { get; }

When overridden in a derived class, gets a value indicating whether the current stream supports reading.

Returns: Boolean - <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool"&gt;true&lt;/a> if the stream supports reading; otherwise, <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool"&gt;false&lt;/a>.

CanSeek

public override bool CanSeek { get; }

Gets a value indicating whether the stream supports seeking.

Returns: Boolean - Always <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool"&gt;false&lt;/a>.

CanWrite

public override bool CanWrite { get; }

When overridden in a derived class, gets a value indicating whether the current stream supports writing.

Returns: Boolean - <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool"&gt;true&lt;/a> if the stream supports writing; otherwise, <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool"&gt;false&lt;/a>.

EncryptionEnabled

public bool EncryptionEnabled { get; }

Gets a value indicating whether encryption is enabled on the stream.

Returns: Boolean - <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool"&gt;true&lt;/a> if encryption has been enabled; otherwise, <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool"&gt;false&lt;/a>.

Length

public override long Length { get; }

Gets the length of the stream. This property is not supported.

Returns: Int64

Exceptions: NotSupportedException - In all cases.

Position

public override long Position { get; set; }

Gets or sets the position within the stream. This property is not supported.

Returns: Int64

Exceptions: NotSupportedException - In all cases.

Methods

SignatureDescription
void Dispose(bool disposing)Releases the unmanaged resources used by the AesStream and optionally releases the managed resources.
ValueTask DisposeAsync()Asynchronously releases all resources used by the current instance of the AesStream class.
void EnableEncryption(PacketCipher encryptor, PacketCipher decryptor)Enables encryption with the specified ciphers.
void EnableEncryption(ReadOnlySpan<byte> sharedSecret)Enables encryption with a pair of AES/CFB8 ciphers created from the specified shared secret.
void Flush()Flushes the underlying stream.
Task FlushAsync(CancellationToken cancellationToken)Asynchronously flushes the underlying stream.
int Read(byte[] buffer, int offset, int count)When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
int Read(Span<byte> buffer)Reads a sequence of bytes from the underlying stream and decrypts them in place.
Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken 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.
ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)Asynchronously reads a sequence of bytes from the underlying stream and decrypts them in place.
long Seek(long offset, SeekOrigin origin)Sets the position within the stream.
void SetLength(long value)Sets the length of the stream.
void Write(byte[] buffer, int offset, int count)When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
void Write(ReadOnlySpan<byte> buffer)Encrypts a sequence of bytes and writes them to the underlying stream.
Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)Asynchronously encrypts a sequence of bytes and writes them to the underlying stream.

Dispose(bool)

protected override void Dispose(bool disposing)

Releases the unmanaged resources used by the AesStream and optionally releases the managed resources.

Parameters: disposing (Boolean)

DisposeAsync()

public override ValueTask DisposeAsync()

Asynchronously releases all resources used by the current instance of the AesStream class.

Returns: ValueTask - A task that represents the asynchronous dispose operation.

EnableEncryption(PacketCipher, PacketCipher)

public void EnableEncryption(PacketCipher encryptor, PacketCipher decryptor)

Enables encryption with the specified ciphers.

Parameters: encryptor (PacketCipher), decryptor (PacketCipher)

Exceptions: ArgumentNullException - <code class="paramref">encryptor</code> is <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null"&gt;null&lt;/a>. -or- <code class="paramref">decryptor</code> is <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null"&gt;null&lt;/a>.; InvalidOperationException - Encryption is already enabled on this stream.; ObjectDisposedException - The current instance has already been disposed.

EnableEncryption(ReadOnlySpan<byte>)

public void EnableEncryption(ReadOnlySpan<byte> sharedSecret)

Enables encryption with a pair of AES/CFB8 ciphers created from the specified shared secret.

Parameters: sharedSecret (ReadOnlySpan<Byte>)

Exceptions: ArgumentException - <code class="paramref">sharedSecret</code> is not PacketCipher.SharedSecretLength bytes long.; InvalidOperationException - Encryption is already enabled on this stream.; ObjectDisposedException - The current instance has already been disposed.

Flush()

public override void Flush()

Flushes the underlying stream.

Exceptions: ObjectDisposedException - The current instance has already been disposed.

FlushAsync(CancellationToken)

public override Task FlushAsync(CancellationToken cancellationToken)

Asynchronously flushes the underlying stream.

Parameters: cancellationToken (CancellationToken)

Returns: Task - A task that represents the asynchronous flush operation.

Exceptions: ObjectDisposedException - The current instance has already been disposed.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.

Read(byte[], int, int)

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

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

Parameters: buffer (Byte[]), offset (Int32), count (Int32)

Returns: Int32 - 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 <code class="paramref">count</code> is 0 or the end of the stream has been reached.

Exceptions: ArgumentException - The sum of <code class="paramref">offset</code> and <code class="paramref">count</code> is larger than the buffer length.; ArgumentNullException - <code class="paramref">buffer</code> is <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null"&gt;null&lt;/a>.; ArgumentOutOfRangeException - <code class="paramref">offset</code> or <code class="paramref">count</code> is negative.; IOException - An I/O error occurs.; NotSupportedException - The stream does not support reading.; ObjectDisposedException - Methods were called after the stream was closed.

Read(Span<byte>)

public override int Read(Span<byte> buffer)

Reads a sequence of bytes from the underlying stream and decrypts them in place.

Parameters: buffer (Span<Byte>)

Returns: Int32 - The number of bytes read into <code class="paramref">buffer</code>, or 0 when the end of the stream is reached.

Exceptions: ObjectDisposedException - The current instance has already been disposed.

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

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken 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.

Parameters: buffer (Byte[]), offset (Int32), count (Int32), cancellationToken (CancellationToken)

Returns: Task<Int32> - A task that represents the asynchronous read operation. The value of the <code class="paramref">TResult</code> parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if <code class="paramref">count</code> is 0 or if the end of the stream has been reached.

Exceptions: ArgumentNullException - <code class="paramref">buffer</code> is <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null"&gt;null&lt;/a>.; ArgumentOutOfRangeException - <code class="paramref">offset</code> or <code class="paramref">count</code> is negative.; ArgumentException - The sum of <code class="paramref">offset</code> and <code class="paramref">count</code> is larger than the buffer length.; NotSupportedException - The stream does not support reading.; ObjectDisposedException - The stream has been disposed.; InvalidOperationException - The stream is currently in use by a previous read operation.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.

ReadAsync(Memory<byte>, CancellationToken)

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)

Asynchronously reads a sequence of bytes from the underlying stream and decrypts them in place.

Parameters: buffer (Memory<Byte>), cancellationToken (CancellationToken)

Returns: ValueTask<Int32> - A task that represents the asynchronous read operation. The result contains the number of bytes read, or 0 when the end of the stream is reached.

Exceptions: ObjectDisposedException - The current instance has already been disposed.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.

Seek(long, SeekOrigin)

public override long Seek(long offset, SeekOrigin origin)

Sets the position within the stream. This method is not supported.

Parameters: offset (Int64), origin (SeekOrigin)

Returns: Int64 - This method does not return a value.

Exceptions: NotSupportedException - In all cases.

SetLength(long)

public override void SetLength(long value)

Sets the length of the stream. This method is not supported.

Parameters: value (Int64)

Exceptions: NotSupportedException - In all cases.

Write(byte[], int, int)

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

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

Parameters: buffer (Byte[]), offset (Int32), count (Int32)

Exceptions: ArgumentException - The sum of <code class="paramref">offset</code> and <code class="paramref">count</code> is greater than the buffer length.; ArgumentNullException - <code class="paramref">buffer</code> is <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null"&gt;null&lt;/a>.; ArgumentOutOfRangeException - <code class="paramref">offset</code> or <code class="paramref">count</code> is negative.; IOException - An I/O error occurred, such as the specified file cannot be found.; NotSupportedException - The stream does not support writing.; ObjectDisposedException - Int32) was called after the stream was closed.

Write(ReadOnlySpan<byte>)

public override void Write(ReadOnlySpan<byte> buffer)

Encrypts a sequence of bytes and writes them to the underlying stream.

Parameters: buffer (ReadOnlySpan<Byte>)

Exceptions: ObjectDisposedException - The current instance has already been disposed.

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

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

Parameters: buffer (Byte[]), offset (Int32), count (Int32), cancellationToken (CancellationToken)

Returns: Task - A task that represents the asynchronous write operation.

Exceptions: ArgumentNullException - <code class="paramref">buffer</code> is <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null"&gt;null&lt;/a>.; ArgumentOutOfRangeException - <code class="paramref">offset</code> or <code class="paramref">count</code> is negative.; ArgumentException - The sum of <code class="paramref">offset</code> and <code class="paramref">count</code> is larger than the buffer length.; NotSupportedException - The stream does not support writing.; ObjectDisposedException - The stream has been disposed.; InvalidOperationException - The stream is currently in use by a previous write operation.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.

WriteAsync(ReadOnlyMemory<byte>, CancellationToken)

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)

Asynchronously encrypts a sequence of bytes and writes them to the underlying stream.

Parameters: buffer (ReadOnlyMemory<Byte>), cancellationToken (CancellationToken)

Returns: ValueTask - A task that represents the asynchronous write operation.

Exceptions: ObjectDisposedException - The current instance has already been disposed.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.