Skip to content

Commit 6b4823b

Browse files
committed
fix reading spans
1 parent 72418b6 commit 6b4823b

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/DataProtection/DataProtection/src/Cng/CbcAuthenticatedEncryptor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public byte[] Decrypt(ArraySegment<byte> ciphertext, ArraySegment<byte> addition
197197
try
198198
{
199199
Decrypt(ciphertext, additionalAuthenticatedData, ref pooledArrayBuffer);
200-
return pooledArrayBuffer.GetSpan(pooledArrayBuffer.WrittenCount).ToArray();
200+
return pooledArrayBuffer.WrittenSpan.ToArray();
201201
}
202202
finally
203203
{
@@ -395,8 +395,8 @@ public byte[] Encrypt(ArraySegment<byte> plaintext, ArraySegment<byte> additiona
395395
Encrypt(plaintext, additionalAuthenticatedData, ref pooledArrayBuffer);
396396
pooledArrayBuffer.Advance(postBufferSize);
397397

398-
var resultSpan = pooledArrayBuffer.GetSpan(pooledArrayBuffer.WrittenCount).ToArray();
399-
CryptoUtil.Assert(resultSpan.Length == outputSize, "bytesWritten == size");
398+
var resultSpan = pooledArrayBuffer.WrittenSpan.ToArray();
399+
CryptoUtil.Assert(resultSpan.Length == outputSize, "writtenSpan length should equal calculated outputSize");
400400

401401
return resultSpan;
402402
}

src/DataProtection/DataProtection/src/Cng/CngGcmAuthenticatedEncryptor.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public byte[] Decrypt(ArraySegment<byte> ciphertext, ArraySegment<byte> addition
163163
try
164164
{
165165
Decrypt(ciphertext, additionalAuthenticatedData, ref pooledArrayBuffer);
166-
return pooledArrayBuffer.GetSpan(pooledArrayBuffer.WrittenCount).ToArray();
166+
return pooledArrayBuffer.WrittenSpan.ToArray();
167167
}
168168
finally
169169
{
@@ -310,9 +310,13 @@ public byte[] Encrypt(ArraySegment<byte> plaintext, ArraySegment<byte> additiona
310310
var pooledArrayBuffer = new PooledArrayBufferWriter<byte>(outputSize);
311311
try
312312
{
313+
pooledArrayBuffer.Advance((int)preBufferSize);
313314
Encrypt(plaintext, additionalAuthenticatedData, ref pooledArrayBuffer);
314-
CryptoUtil.Assert(pooledArrayBuffer.WrittenCount == size, "bytesWritten == size");
315-
return pooledArrayBuffer.GetSpan(pooledArrayBuffer.WrittenCount).ToArray();
315+
pooledArrayBuffer.Advance((int)postBufferSize);
316+
317+
var resultSpan = pooledArrayBuffer.WrittenSpan.ToArray();
318+
CryptoUtil.Assert(resultSpan.Length == outputSize, "writtenSpan length should equal calculated outputSize");
319+
return resultSpan;
316320
}
317321
finally
318322
{

0 commit comments

Comments
 (0)