39 ReadOnlySpan<byte> data,
40 ReadOnlySpan<byte> key,
41 ReadOnlySpan<byte> initialCounter,
44 ValidateArguments(key, initialCounter, byteOffset);
45 byte[] output =
new byte[data.Length];
46 using var transform =
new NdsModcryptTransform(key, initialCounter, byteOffset);
47 transform.Transform(data, output);
99 ReadOnlyMemory<byte> key,
100 ReadOnlyMemory<byte> initialCounter,
102 CancellationToken cancellationToken =
default)
104 ArgumentNullException.ThrowIfNull(source);
105 ArgumentNullException.ThrowIfNull(destination);
108 throw new ArgumentException(
"The modcrypt source must be readable.", nameof(source));
111 if (!destination.CanWrite)
113 throw new ArgumentException(
"The modcrypt destination must be writable.", nameof(destination));
116 if (ReferenceEquals(source, destination))
118 throw new ArgumentException(
"Streaming modcrypt requires distinct source and destination streams.", nameof(destination));
123 throw new ArgumentOutOfRangeException(nameof(length),
"The transform length cannot be negative.");
126 ValidateArguments(key.Span, initialCounter.Span, byteOffset);
127 byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamBufferSize);
130 using var transform =
new NdsModcryptTransform(key.Span, initialCounter.Span, byteOffset);
131 long remaining = length;
132 while (remaining != 0)
134 cancellationToken.ThrowIfCancellationRequested();
135 int count = (int)Math.Min(remaining, StreamBufferSize);
136 await source.ReadExactlyAsync(buffer.AsMemory(0, count), cancellationToken).ConfigureAwait(
false);
137 transform.Transform(buffer.AsSpan(0, count), buffer.AsSpan(0, count));
138 await destination.WriteAsync(buffer.AsMemory(0, count), cancellationToken).ConfigureAwait(
false);
144 ArrayPool<byte>.Shared.Return(buffer, clearArray:
true);