diff --git a/src/libraries/Common/src/System/Security/Cryptography/Asn1/DirectoryStringAsn.xml.cs b/src/libraries/Common/src/System/Security/Cryptography/Asn1/DirectoryStringAsn.xml.cs index 7310045bca4dea..2f420f8cc5c9a4 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Asn1/DirectoryStringAsn.xml.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Asn1/DirectoryStringAsn.xml.cs @@ -2,13 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable #pragma warning disable SA1028 // ignore whitespace warnings for generated code using System; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Cryptography.Asn1; -#nullable enable namespace System.Security.Cryptography.Asn1 { [StructLayout(LayoutKind.Sequential)] diff --git a/src/libraries/Common/src/System/Security/Cryptography/Asn1/GeneralNameAsn.xml.cs b/src/libraries/Common/src/System/Security/Cryptography/Asn1/GeneralNameAsn.xml.cs index dfd311632120fd..79316b126f8e9d 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Asn1/GeneralNameAsn.xml.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Asn1/GeneralNameAsn.xml.cs @@ -2,13 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable #pragma warning disable SA1028 // ignore whitespace warnings for generated code using System; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Cryptography.Asn1; -#nullable enable namespace System.Security.Cryptography.Asn1 { [StructLayout(LayoutKind.Sequential)] diff --git a/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/AsnFormatter.OSX.cs b/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/AsnFormatter.OSX.cs index 99440d90665051..2802eade16633a 100644 --- a/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/AsnFormatter.OSX.cs +++ b/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/AsnFormatter.OSX.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; using System.Security.Cryptography; using System.Security.Cryptography.Asn1; using System.Text; @@ -25,7 +24,7 @@ internal class AppleAsnFormatter : AsnFormatter { if (oid == null || string.IsNullOrEmpty(oid.Value)) { - return EncodeHexString(rawData, true); + return EncodeSpaceSeparatedHexString(rawData); } switch (oid.Value) diff --git a/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/AsnFormatter.cs b/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/AsnFormatter.cs index 2ac6572cbd0cbc..54f508e3df5c27 100644 --- a/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/AsnFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/AsnFormatter.cs @@ -3,7 +3,7 @@ // See the LICENSE file in the project root for more information. using System; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics; using System.Security.Cryptography; namespace Internal.Cryptography @@ -19,31 +19,19 @@ public string Format(Oid? oid, byte[] rawData, bool multiLine) protected abstract string? FormatNative(Oid? oid, byte[] rawData, bool multiLine); - protected static string EncodeHexString(byte[] sArray, bool spaceSeparated = false) + protected static string EncodeSpaceSeparatedHexString(byte[] sArray) { - return EncodeHexString(sArray, 0, (uint)sArray.Length, spaceSeparated); - } + Debug.Assert(sArray != null && sArray.Length != 0); - [return: NotNullIfNotNull("sArray")] - private static string? EncodeHexString(byte[]? sArray, uint start, uint end, bool spaceSeparated) - { - string? result = null; + int length = (sArray.Length * 3) - 1; // two chars per byte, plus 1 space between each - if (sArray != null) + return string.Create(length, sArray, (hexOrder, sArray) => { - uint len = (end - start) * 2; + int j = 0; - if (spaceSeparated) + for (int i = 0; i < sArray.Length; i++) { - // There will be n-1 spaces between n bytes. - len += (end - start - 1); - } - - char[] hexOrder = new char[len]; - - for (uint i = start, j = 0; i < end; i++) - { - if (spaceSeparated && i > start) + if (i != 0) { hexOrder[j++] = ' '; } @@ -53,10 +41,8 @@ protected static string EncodeHexString(byte[] sArray, bool spaceSeparated = fal hexOrder[j++] = HexConverter.ToCharUpper(digit); } - result = new string(hexOrder); - } - - return result; + Debug.Assert(j == hexOrder.Length); + }); } } } diff --git a/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OpenSslAsnFormatter.cs b/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OpenSslAsnFormatter.cs index 063d036aa86efc..c39484dd866b40 100644 --- a/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OpenSslAsnFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OpenSslAsnFormatter.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Security.Cryptography; using System.Text; @@ -16,7 +15,7 @@ internal sealed class OpenSslAsnFormatter : AsnFormatter { if (oid == null || string.IsNullOrEmpty(oid.Value)) { - return EncodeHexString(rawData, true); + return EncodeSpaceSeparatedHexString(rawData); } // The established behavior for this method is to return the native answer, if possible,