| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Buffers.Binary; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Security.Cryptography; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | |
| | | 12 | | namespace System.IO.Compression |
| | | 13 | | { |
| | | 14 | | internal sealed class ZipCryptoStream : Stream |
| | | 15 | | { |
| | | 16 | | private const int EncryptionBufferSize = 4096; |
| | | 17 | | |
| | | 18 | | private readonly bool _encrypting; |
| | | 19 | | private readonly Stream _base; |
| | | 20 | | private readonly bool _leaveOpen; |
| | | 21 | | private bool _headerWritten; |
| | | 22 | | private bool _disposed; |
| | | 23 | | private readonly ushort _verifierLow2Bytes; // (DOS time low word when streaming) |
| | | 24 | | private readonly uint? _crc32ForHeader; // (CRC-based header when not streaming) |
| | | 25 | | |
| | | 26 | | private uint _key0; |
| | | 27 | | private uint _key1; |
| | | 28 | | private uint _key2; |
| | 0 | 29 | | private static readonly uint[] s_crc2Table = CreateCrc32Table(); |
| | | 30 | | |
| | | 31 | | // Reusable work buffer for write operations, lazily allocated on first write |
| | | 32 | | private byte[]? _writeWorkBuffer; |
| | | 33 | | |
| | | 34 | | private static uint[] CreateCrc32Table() |
| | 0 | 35 | | { |
| | 0 | 36 | | var table = new uint[256]; |
| | 0 | 37 | | for (uint i = 0; i < 256; i++) |
| | 0 | 38 | | { |
| | 0 | 39 | | uint c = i; |
| | 0 | 40 | | for (int j = 0; j < 8; j++) |
| | 0 | 41 | | c = (c & 1) != 0 ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); |
| | 0 | 42 | | table[i] = c; |
| | 0 | 43 | | } |
| | 0 | 44 | | return table; |
| | 0 | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | private static uint Crc32Update(uint crc, byte b) => s_crc2Table[(crc ^ b) & 0xFF] ^ (crc >> 8); |
| | | 48 | | |
| | | 49 | | // Private decryption constructor - use Create/CreateAsync factory methods instead. |
| | | 50 | | // Keys must already be validated before calling this constructor. |
| | 0 | 51 | | private ZipCryptoStream(Stream baseStream, uint key0, uint key1, uint key2, bool leaveOpen = false) |
| | 0 | 52 | | { |
| | 0 | 53 | | _base = baseStream; |
| | 0 | 54 | | _key0 = key0; |
| | 0 | 55 | | _key1 = key1; |
| | 0 | 56 | | _key2 = key2; |
| | 0 | 57 | | _encrypting = false; |
| | 0 | 58 | | _leaveOpen = leaveOpen; |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Creates a ZipCryptoStream for decryption. Reads and validates the 12-byte header synchronously. |
| | | 63 | | /// </summary> |
| | | 64 | | internal static ZipCryptoStream Create(Stream baseStream, ZipCryptoKeys keys, byte expectedCheckByte, bool encry |
| | 0 | 65 | | { |
| | 0 | 66 | | ArgumentNullException.ThrowIfNull(baseStream); |
| | 0 | 67 | | Debug.Assert(!encrypting, "Use the overload with passwordVerifierLow2Bytes for encryption."); |
| | | 68 | | |
| | 0 | 69 | | (uint key0, uint key1, uint key2) = ReadAndValidateHeaderCore(isAsync: false, baseStream, keys, expectedChec |
| | 0 | 70 | | return new ZipCryptoStream(baseStream, key0, key1, key2, leaveOpen); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Creates a ZipCryptoStream for decryption. Reads and validates the 12-byte header asynchronously. |
| | | 75 | | /// </summary> |
| | | 76 | | internal static async Task<ZipCryptoStream> CreateAsync(Stream baseStream, ZipCryptoKeys keys, byte expectedChec |
| | 0 | 77 | | { |
| | 0 | 78 | | ArgumentNullException.ThrowIfNull(baseStream); |
| | 0 | 79 | | Debug.Assert(!encrypting, "Use the overload with passwordVerifierLow2Bytes for encryption."); |
| | | 80 | | |
| | 0 | 81 | | (uint key0, uint key1, uint key2) = await ReadAndValidateHeaderCore(isAsync: true, baseStream, keys, expecte |
| | 0 | 82 | | return new ZipCryptoStream(baseStream, key0, key1, key2, leaveOpen); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Creates a ZipCryptoStream for encryption. Only synchronous creation is needed since no I/O is performed here |
| | | 87 | | /// </summary> |
| | | 88 | | internal static ZipCryptoStream Create(Stream baseStream, |
| | | 89 | | ZipCryptoKeys keys, |
| | | 90 | | ushort passwordVerifierLow2Bytes, |
| | | 91 | | bool encrypting, |
| | | 92 | | uint? crc32 = null, |
| | | 93 | | bool leaveOpen = false) |
| | 0 | 94 | | { |
| | 0 | 95 | | ArgumentNullException.ThrowIfNull(baseStream); |
| | 0 | 96 | | Debug.Assert(encrypting, "Use the overload with expectedCheckByte for decryption."); |
| | | 97 | | |
| | 0 | 98 | | return new ZipCryptoStream(baseStream, keys, passwordVerifierLow2Bytes, crc32, leaveOpen); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | // Encryption constructor |
| | 0 | 102 | | private ZipCryptoStream(Stream baseStream, |
| | 0 | 103 | | ZipCryptoKeys keys, |
| | 0 | 104 | | ushort passwordVerifierLow2Bytes, |
| | 0 | 105 | | uint? crc32, |
| | 0 | 106 | | bool leaveOpen) |
| | 0 | 107 | | { |
| | 0 | 108 | | _base = baseStream; |
| | 0 | 109 | | _encrypting = true; |
| | 0 | 110 | | _leaveOpen = leaveOpen; |
| | 0 | 111 | | _verifierLow2Bytes = passwordVerifierLow2Bytes; |
| | 0 | 112 | | _crc32ForHeader = crc32; |
| | 0 | 113 | | _key0 = keys.Key0; |
| | 0 | 114 | | _key1 = keys.Key1; |
| | 0 | 115 | | _key2 = keys.Key2; |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | // Creates the persisted key material from a password. |
| | | 119 | | // Returns a struct of 3 integers to keep the key off the heap. |
| | | 120 | | internal static ZipCryptoKeys CreateKey(ReadOnlySpan<char> password) |
| | 0 | 121 | | { |
| | | 122 | | // Initialize keys with standard ZipCrypto initial values |
| | 0 | 123 | | uint key0 = 305419896; |
| | 0 | 124 | | uint key1 = 591751049; |
| | 0 | 125 | | uint key2 = 878082192; |
| | | 126 | | |
| | | 127 | | // Feed the password's UTF-8 bytes into the key schedule. UTF-8 (rather than ASCII) |
| | | 128 | | // preserves non-ASCII passwords; interop with tools that assume a different code page |
| | | 129 | | // is documented as a caveat. |
| | 0 | 130 | | byte[] passwordBytes = ArrayPool<byte>.Shared.Rent(Encoding.UTF8.GetMaxByteCount(password.Length)); |
| | | 131 | | try |
| | 0 | 132 | | { |
| | 0 | 133 | | int byteCount = Encoding.UTF8.GetBytes(password, passwordBytes); |
| | 0 | 134 | | for (int i = 0; i < byteCount; i++) |
| | 0 | 135 | | { |
| | 0 | 136 | | UpdateKeys(ref key0, ref key1, ref key2, passwordBytes[i]); |
| | 0 | 137 | | } |
| | 0 | 138 | | } |
| | | 139 | | finally |
| | 0 | 140 | | { |
| | 0 | 141 | | CryptographicOperations.ZeroMemory(passwordBytes); |
| | 0 | 142 | | ArrayPool<byte>.Shared.Return(passwordBytes); |
| | 0 | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | return new ZipCryptoKeys(key0, key1, key2); |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | private void CalculateHeader(Span<byte> header) |
| | 0 | 149 | | { |
| | 0 | 150 | | Debug.Assert(header.Length == 12); |
| | | 151 | | |
| | | 152 | | // bytes 0..9 random |
| | 0 | 153 | | RandomNumberGenerator.Fill(header.Slice(0, 10)); |
| | | 154 | | |
| | | 155 | | // bytes 10..11 verifier |
| | 0 | 156 | | if (_crc32ForHeader.HasValue) |
| | 0 | 157 | | { |
| | 0 | 158 | | uint crc = _crc32ForHeader.Value; |
| | 0 | 159 | | BinaryPrimitives.WriteUInt16LittleEndian(header.Slice(10), (ushort)(crc >> 16)); |
| | 0 | 160 | | } |
| | | 161 | | else |
| | 0 | 162 | | { |
| | 0 | 163 | | BinaryPrimitives.WriteUInt16LittleEndian(header.Slice(10), _verifierLow2Bytes); |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | // encrypt in place |
| | 0 | 167 | | for (int i = 0; i < header.Length; i++) |
| | 0 | 168 | | { |
| | 0 | 169 | | byte p = header[i]; |
| | 0 | 170 | | byte ks = DecryptByte(_key2); |
| | 0 | 171 | | header[i] = (byte)(p ^ ks); |
| | | 172 | | |
| | | 173 | | // keys updated with PLAINTEXT per ZIP spec |
| | 0 | 174 | | UpdateKeys(ref _key0, ref _key1, ref _key2, p); |
| | 0 | 175 | | } |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | private unsafe void EnsureHeader() |
| | 0 | 179 | | { |
| | 0 | 180 | | if (!_encrypting || _headerWritten) |
| | 0 | 181 | | { |
| | 0 | 182 | | return; |
| | | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | Span<byte> header = stackalloc byte[12]; |
| | 0 | 186 | | CalculateHeader(header); |
| | 0 | 187 | | _base.Write(header); |
| | 0 | 188 | | _headerWritten = true; |
| | 0 | 189 | | } |
| | | 190 | | |
| | | 191 | | private async ValueTask EnsureHeaderAsync(CancellationToken cancellationToken) |
| | 0 | 192 | | { |
| | 0 | 193 | | if (!_encrypting || _headerWritten) |
| | 0 | 194 | | { |
| | 0 | 195 | | return; |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | byte[] header = new byte[12]; |
| | 0 | 199 | | CalculateHeader(header); |
| | 0 | 200 | | await _base.WriteAsync(header, cancellationToken).ConfigureAwait(false); |
| | 0 | 201 | | _headerWritten = true; |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | private static async Task<(uint key0, uint key1, uint key2)> ReadAndValidateHeaderCore(bool isAsync, Stream base |
| | 0 | 205 | | { |
| | | 206 | | // Initialize keys from input |
| | 0 | 207 | | uint key0 = keys.Key0; |
| | 0 | 208 | | uint key1 = keys.Key1; |
| | 0 | 209 | | uint key2 = keys.Key2; |
| | | 210 | | |
| | 0 | 211 | | byte[] hdr = new byte[12]; |
| | | 212 | | |
| | | 213 | | try |
| | 0 | 214 | | { |
| | 0 | 215 | | if (isAsync) |
| | 0 | 216 | | { |
| | 0 | 217 | | await baseStream.ReadExactlyAsync(hdr, cancellationToken).ConfigureAwait(false); |
| | 0 | 218 | | } |
| | | 219 | | else |
| | 0 | 220 | | { |
| | 0 | 221 | | baseStream.ReadExactly(hdr); |
| | 0 | 222 | | } |
| | 0 | 223 | | } |
| | 0 | 224 | | catch (EndOfStreamException) |
| | 0 | 225 | | { |
| | 0 | 226 | | throw new InvalidDataException(SR.TruncatedZipCryptoHeader); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | // Decrypt header and update keys |
| | 0 | 230 | | for (int i = 0; i < hdr.Length; i++) |
| | 0 | 231 | | { |
| | 0 | 232 | | byte m = DecryptByte(key2); |
| | 0 | 233 | | byte plain = (byte)(hdr[i] ^ m); |
| | 0 | 234 | | UpdateKeys(ref key0, ref key1, ref key2, plain); |
| | 0 | 235 | | hdr[i] = plain; |
| | 0 | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | if (hdr[11] != expectedCheckByte) |
| | 0 | 239 | | { |
| | 0 | 240 | | throw new InvalidDataException(SR.InvalidPassword); |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | return (key0, key1, key2); |
| | 0 | 244 | | } |
| | | 245 | | |
| | | 246 | | private static void UpdateKeys(ref uint key0, ref uint key1, ref uint key2, byte b) |
| | 0 | 247 | | { |
| | 0 | 248 | | key0 = Crc32Update(key0, b); |
| | 0 | 249 | | key1 += (key0 & 0xFF); |
| | 0 | 250 | | key1 = key1 * 134775813 + 1; |
| | 0 | 251 | | key2 = Crc32Update(key2, (byte)(key1 >> 24)); |
| | 0 | 252 | | } |
| | | 253 | | |
| | | 254 | | private static byte DecryptByte(uint key2) |
| | 0 | 255 | | { |
| | 0 | 256 | | uint temp = key2 | 2; |
| | 0 | 257 | | return (byte)((temp * (temp ^ 1)) >> 8); |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | private byte DecryptAndUpdateKeys(byte ciph) |
| | 0 | 261 | | { |
| | 0 | 262 | | byte m = DecryptByte(_key2); |
| | 0 | 263 | | byte plain = (byte)(ciph ^ m); |
| | 0 | 264 | | UpdateKeys(ref _key0, ref _key1, ref _key2, plain); |
| | 0 | 265 | | return plain; |
| | 0 | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | public override bool CanRead => !_disposed && !_encrypting; |
| | 0 | 269 | | public override bool CanSeek => false; |
| | 0 | 270 | | public override bool CanWrite => !_disposed && _encrypting; |
| | 0 | 271 | | public override long Length => throw new NotSupportedException(); |
| | | 272 | | public override long Position |
| | | 273 | | { |
| | 0 | 274 | | get => throw new NotSupportedException(); |
| | 0 | 275 | | set => throw new NotSupportedException(); |
| | | 276 | | } |
| | | 277 | | public override void Flush() |
| | 0 | 278 | | { |
| | 0 | 279 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 280 | | _base.Flush(); |
| | 0 | 281 | | } |
| | | 282 | | |
| | | 283 | | public override int Read(byte[] buffer, int offset, int count) |
| | 0 | 284 | | { |
| | 0 | 285 | | ValidateBufferArguments(buffer, offset, count); |
| | 0 | 286 | | return Read(buffer.AsSpan(offset, count)); |
| | 0 | 287 | | } |
| | | 288 | | |
| | | 289 | | public override int Read(Span<byte> destination) |
| | 0 | 290 | | { |
| | 0 | 291 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 292 | | if (_encrypting) |
| | 0 | 293 | | { |
| | 0 | 294 | | throw new NotSupportedException(SR.ReadingNotSupported); |
| | | 295 | | } |
| | 0 | 296 | | int n = _base.Read(destination); |
| | 0 | 297 | | for (int i = 0; i < n; i++) |
| | 0 | 298 | | destination[i] = DecryptAndUpdateKeys(destination[i]); |
| | 0 | 299 | | return n; |
| | | 300 | | |
| | 0 | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| | 0 | 304 | | public override void SetLength(long value) => throw new NotSupportedException(); |
| | | 305 | | |
| | | 306 | | public override void Write(byte[] buffer, int offset, int count) |
| | 0 | 307 | | { |
| | 0 | 308 | | ValidateBufferArguments(buffer, offset, count); |
| | 0 | 309 | | Write(buffer.AsSpan(offset, count)); |
| | 0 | 310 | | } |
| | | 311 | | |
| | | 312 | | public override void Write(ReadOnlySpan<byte> buffer) |
| | 0 | 313 | | { |
| | 0 | 314 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 315 | | if (!_encrypting) |
| | 0 | 316 | | { |
| | 0 | 317 | | throw new NotSupportedException(SR.WritingNotSupported); |
| | | 318 | | } |
| | | 319 | | |
| | 0 | 320 | | EnsureHeader(); |
| | | 321 | | |
| | 0 | 322 | | byte[] workBuffer = GetWriteWorkBuffer(); |
| | | 323 | | |
| | 0 | 324 | | while (!buffer.IsEmpty) |
| | 0 | 325 | | { |
| | 0 | 326 | | int chunkSize = Math.Min(buffer.Length, workBuffer.Length); |
| | | 327 | | |
| | 0 | 328 | | for (int i = 0; i < chunkSize; i++) |
| | 0 | 329 | | { |
| | 0 | 330 | | byte ks = DecryptByte(_key2); |
| | 0 | 331 | | byte p = buffer[i]; |
| | 0 | 332 | | workBuffer[i] = (byte)(p ^ ks); |
| | 0 | 333 | | UpdateKeys(ref _key0, ref _key1, ref _key2, p); |
| | 0 | 334 | | } |
| | | 335 | | |
| | 0 | 336 | | _base.Write(workBuffer, 0, chunkSize); |
| | 0 | 337 | | buffer = buffer[chunkSize..]; |
| | 0 | 338 | | } |
| | 0 | 339 | | } |
| | | 340 | | |
| | | 341 | | protected override void Dispose(bool disposing) |
| | 0 | 342 | | { |
| | 0 | 343 | | if (_disposed) |
| | 0 | 344 | | { |
| | 0 | 345 | | return; |
| | | 346 | | } |
| | 0 | 347 | | _disposed = true; |
| | | 348 | | |
| | 0 | 349 | | if (disposing) |
| | 0 | 350 | | { |
| | | 351 | | // If encrypted empty entry (no payload written), still must emit 12-byte header: |
| | 0 | 352 | | if (_encrypting) |
| | 0 | 353 | | { |
| | 0 | 354 | | EnsureHeader(); |
| | 0 | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | if (!_leaveOpen) |
| | 0 | 358 | | { |
| | 0 | 359 | | _base.Dispose(); |
| | 0 | 360 | | } |
| | 0 | 361 | | } |
| | 0 | 362 | | base.Dispose(disposing); |
| | 0 | 363 | | } |
| | | 364 | | |
| | | 365 | | public override async ValueTask DisposeAsync() |
| | 0 | 366 | | { |
| | 0 | 367 | | if (_disposed) |
| | 0 | 368 | | { |
| | 0 | 369 | | return; |
| | | 370 | | } |
| | 0 | 371 | | _disposed = true; |
| | | 372 | | |
| | | 373 | | // If encrypted empty entry (no payload written), still must emit 12-byte header: |
| | 0 | 374 | | if (_encrypting) |
| | 0 | 375 | | { |
| | 0 | 376 | | await EnsureHeaderAsync(CancellationToken.None).ConfigureAwait(false); |
| | 0 | 377 | | } |
| | 0 | 378 | | if (!_leaveOpen) |
| | 0 | 379 | | { |
| | 0 | 380 | | await _base.DisposeAsync().ConfigureAwait(false); |
| | 0 | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | GC.SuppressFinalize(this); |
| | | 384 | | |
| | | 385 | | // Don't call base.DisposeAsync() as it would call Dispose() synchronously, |
| | | 386 | | // which could fail on async-only streams. We've already handled all cleanup. |
| | 0 | 387 | | } |
| | | 388 | | |
| | | 389 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 390 | | { |
| | 0 | 391 | | ValidateBufferArguments(buffer, offset, count); |
| | 0 | 392 | | return ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); |
| | 0 | 393 | | } |
| | | 394 | | |
| | | 395 | | public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = defaul |
| | 0 | 396 | | { |
| | 0 | 397 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 398 | | if (_encrypting) |
| | 0 | 399 | | { |
| | 0 | 400 | | throw new NotSupportedException(SR.ReadingNotSupported); |
| | | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 404 | | int n = await _base.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); |
| | 0 | 405 | | Span<byte> span = buffer.Span; |
| | | 406 | | |
| | 0 | 407 | | for (int i = 0; i < n; i++) |
| | 0 | 408 | | { |
| | 0 | 409 | | span[i] = DecryptAndUpdateKeys(span[i]); |
| | 0 | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | return n; |
| | 0 | 413 | | } |
| | | 414 | | |
| | | 415 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 416 | | { |
| | 0 | 417 | | ValidateBufferArguments(buffer, offset, count); |
| | 0 | 418 | | return WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); |
| | 0 | 419 | | } |
| | | 420 | | |
| | | 421 | | public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = de |
| | 0 | 422 | | { |
| | 0 | 423 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 424 | | if (!_encrypting) |
| | 0 | 425 | | { |
| | 0 | 426 | | throw new NotSupportedException(SR.WritingNotSupported); |
| | | 427 | | } |
| | | 428 | | |
| | 0 | 429 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 430 | | |
| | 0 | 431 | | await EnsureHeaderAsync(cancellationToken).ConfigureAwait(false); |
| | | 432 | | |
| | 0 | 433 | | byte[] workBuffer = GetWriteWorkBuffer(); |
| | | 434 | | |
| | 0 | 435 | | while (!buffer.IsEmpty) |
| | 0 | 436 | | { |
| | 0 | 437 | | int chunkSize = Math.Min(buffer.Length, workBuffer.Length); |
| | 0 | 438 | | ReadOnlySpan<byte> chunk = buffer.Span[..chunkSize]; |
| | | 439 | | |
| | 0 | 440 | | for (int i = 0; i < chunkSize; i++) |
| | 0 | 441 | | { |
| | 0 | 442 | | byte ks = DecryptByte(_key2); |
| | 0 | 443 | | byte p = chunk[i]; |
| | 0 | 444 | | workBuffer[i] = (byte)(p ^ ks); |
| | 0 | 445 | | UpdateKeys(ref _key0, ref _key1, ref _key2, p); |
| | 0 | 446 | | } |
| | | 447 | | |
| | 0 | 448 | | await _base.WriteAsync(workBuffer.AsMemory(0, chunkSize), cancellationToken).ConfigureAwait(false); |
| | 0 | 449 | | buffer = buffer[chunkSize..]; |
| | 0 | 450 | | } |
| | 0 | 451 | | } |
| | | 452 | | |
| | | 453 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | 0 | 454 | | { |
| | 0 | 455 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 456 | | return _base.FlushAsync(cancellationToken); |
| | 0 | 457 | | } |
| | | 458 | | |
| | 0 | 459 | | private byte[] GetWriteWorkBuffer() => _writeWorkBuffer ??= new byte[EncryptionBufferSize]; |
| | | 460 | | } |
| | | 461 | | } |