3using System.Security.Authentication;
5using System.Threading.Tasks;
18 public const SslProtocols
SecureTls = (SslProtocols)((
int)SslProtocols.Tls12 | 12288 );
23 public const SslProtocols
TlsOnly = (SslProtocols)((
int)SslProtocols.Tls | (int)SslProtocols.Tls11 | (
int)SslProtocols.Tls12 | 12288 );
31 public static Task
CryptoTransform(ICryptoTransform Transform, Stream Source, Stream Destination)
43 public static async Task
CryptoTransform(ICryptoTransform Transform, Stream Source, Stream Destination,
int BufferSize)
46 throw new ArgumentException(
"Invalid buffer size.", nameof(BufferSize));
48 long l = Source.Length;
50 BufferSize = (int)Math.Min(l, BufferSize);
52 byte[] Input =
new byte[BufferSize];
53 byte[] Output =
new byte[BufferSize];
58 j = (int)Math.Min(BufferSize, l);
59 await Source.ReadAllAsync(Input, 0, j);
64 Output = Transform.TransformFinalBlock(Input, 0, j);
65 await Destination.WriteAsync(Output, 0, Output.Length);
69 j = Transform.TransformBlock(Input, 0, j, Output, 0);
70 await Destination.WriteAsync(Output, 0, j);
82 public static async Task<bool>
CopyAsync(Stream From, Stream To,
long DataLen)
86 int BufSize = (int)Math.Min(DataLen, 65536);
87 byte[] Buffer =
new byte[BufSize];
91 if (DataLen < BufSize)
92 BufSize = (int)DataLen;
94 if (await From.TryReadAllAsync(Buffer, 0, BufSize) != BufSize)
97 await To.WriteAsync(Buffer, 0, BufSize);
Helper methods for encrypting and decrypting streams of data.
static async Task CryptoTransform(ICryptoTransform Transform, Stream Source, Stream Destination, int BufferSize)
Transforms a stream of data.
const SslProtocols SecureTls
TLS 1.2 & 1.3
const SslProtocols TlsOnly
TLS 1.0, 1.1, 1.2 & 1.3
static async Task< bool > CopyAsync(Stream From, Stream To, long DataLen)
Copies DataLen number of bytes from From to To .
static Task CryptoTransform(ICryptoTransform Transform, Stream Source, Stream Destination)
Transforms a stream of data.