Skip to content

Commit c582fa2

Browse files
vcsjonesbartonjs
andauthored
Avoid array -> span -> array in COSE ToBeSignedBuilder.
The ToBeSignedBuilder for hash and pure modes had an Append that accepts a ReadOnlySpan. For .NET Framework and .NET Standard, IncrementalHash and MemoryStream cannot operate on a ReadOnlySpan, so they were getting converted to an array again. There were a number of places where we already had an array though. This change avoids to array to span to array conversation by introducing a new AppendToBeSigned overload that accepts an array with an offset and length, which avoids a ToArray(). Co-authored-by: Jeremy Barton <[email protected]>
1 parent eec5790 commit c582fa2

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseMessage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ internal static void AppendToBeSigned(
439439
{
440440
while ((bytesRead = contentStream.Read(contentBuffer, 0, contentBuffer.Length)) > 0)
441441
{
442-
toBeSignedBuilder.AppendToBeSigned(contentBuffer.AsSpan(0, bytesRead));
442+
toBeSignedBuilder.AppendToBeSigned(contentBuffer, 0, bytesRead);
443443
}
444444
}
445445
finally
@@ -462,7 +462,7 @@ internal static async Task AppendToBeSignedAsync(
462462
int bytesWritten = CreateToBeSigned(buffer, context, bodyProtected.Span, signProtected.Span, associatedData.Span, ReadOnlySpan<byte>.Empty);
463463
bytesWritten -= 1; // Trim the empty bstr content, it is just a placeholder.
464464

465-
toBeSignedBuilder.AppendToBeSigned(buffer.AsSpan(0, bytesWritten));
465+
toBeSignedBuilder.AppendToBeSigned(buffer, 0, bytesWritten);
466466

467467
//content length
468468
CoseHelpers.WriteByteStringLength(toBeSignedBuilder, (ulong)(content.Length - content.Position));
@@ -476,7 +476,7 @@ internal static async Task AppendToBeSignedAsync(
476476
while ((bytesRead = await content.ReadAsync(contentBuffer, cancellationToken).ConfigureAwait(false)) > 0)
477477
#endif
478478
{
479-
toBeSignedBuilder.AppendToBeSigned(contentBuffer.AsSpan(0, bytesRead));
479+
toBeSignedBuilder.AppendToBeSigned(contentBuffer, 0, bytesRead);
480480
}
481481

482482
ArrayPool<byte>.Shared.Return(contentBuffer, clearArray: true);

src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/HashToBeSignedBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ internal override void AppendToBeSigned(ReadOnlySpan<byte> data)
2323
_incrementalHash.AppendData(data);
2424
}
2525

26+
internal override void AppendToBeSigned(byte[] data, int offset, int length)
27+
{
28+
_incrementalHash.AppendData(data, offset, length);
29+
}
30+
2631
internal override void WithDataAndResetAfterOperation(Span<byte> arg, ToBeSignedOperation operation)
2732
{
2833
#if NETSTANDARD2_0 || NETFRAMEWORK

src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/PureDataToBeSignedBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ internal override void AppendToBeSigned(ReadOnlySpan<byte> data)
2727
#endif
2828
}
2929

30+
internal override void AppendToBeSigned(byte[] data, int offset, int length)
31+
{
32+
_stream.Write(data, offset, length);
33+
}
34+
3035
internal override void WithDataAndResetAfterOperation(Span<byte> arg, ToBeSignedOperation operation)
3136
{
3237
#if NETSTANDARD2_0 || NETFRAMEWORK

src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/ToBeSignedBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void Dispose()
2525
}
2626

2727
internal abstract void AppendToBeSigned(ReadOnlySpan<byte> data);
28+
internal abstract void AppendToBeSigned(byte[] data, int offset, int length);
2829

2930
// arg is passthrough - we don't do anything with it but all usages need to pass in extra Span and it's not allowed to do through closure.
3031
internal abstract void WithDataAndResetAfterOperation(Span<byte> arg, ToBeSignedOperation operation);

0 commit comments

Comments
 (0)