Repository: veler/DevToys
File: src/dev/impl/DevToys/Helpers/CertificateHelper.cs (C#)

Code before:
                decoded = certificate.ToString();
            }
            catch (CryptographicException wce)
            {
                // If this is a valid certificate, but an incorrect/missing password, we should still return true
                if (wce.HResult == ERROR_INVALID_PASSWORD_HRESULT)
                {
                    decoded = LanguageManager.Instance.CertificateEncoderDecoder.InvalidPasswordError;
                    return true;
                }

                return false;
            }
            catch (Exception)
            {
                return false;
            }

            // Try to decode the X.509 extensions. Put this is a separate try/catch because it's not critical.
            try
            {
                decoded += Environment.NewLine;

                foreach (X509Extension x509Extension in certificate.Extensions)
                {
                    StringBuilder extensionData = new StringBuilder();
                    AsnEncodedData asnEncodedData = new AsnEncodedData(x509Extension.Oid, x509Extension.RawData);

                    // Add the name in brackets to match the previous output from X509Certificate.ToString()
                    extensionData.Append($"[{x509Extension.Oid.FriendlyName}]");
                    extensionData.Append(Environment.NewLine);

                    // Add each line of the data, indented by two spaces to match the output from X509Certificate.ToString()
                    foreach (string dataLine in asnEncodedData.Format(multiLine: true).Split(Environment.NewLine))
                    {
                        extensionData.Append($"  {dataLine}");
                        extensionData.Append(Environment.NewLine);
                    }

                    // Add it to the output
                    decoded += extensionData.ToString();
                }

                decoded = decoded.Trim();
            }
            catch (Exception ex)
            {
                Logger.LogFault("Failed to parse X.509 extensions from certificate.", ex);
            }

            return true;

Review comment (suggestion):
I believe this `StringBuilder` could be declared outside of the `foreach` loop and it would be more optimal since right now you are allocating a new string on each iteration by concatenating to `decoded` ([line 95](https://github.com/veler/DevToys/pull/831/files#diff-b0f9ffa47ec01cd1ab78444903b160769b3c781f18cd59f380ea3b7b8331f7edR95)).

Code after:
                decoded = certificate.ToString();
            }
            catch (CryptographicException wce)
            {
                // If this is a valid certificate, but an incorrect/missing password, we should still return true
                if (wce.HResult == ERROR_INVALID_PASSWORD_HRESULT)
                {
                    decoded = LanguageManager.Instance.CertificateEncoderDecoder.InvalidPasswordError;
                    return true;
                }

                return false;
            }
            catch (Exception)
            {
                return false;
            }

            if (!string.IsNullOrEmpty(decoded) && certificate.Extensions.OfType<X509Extension>().Any())
            {
                decoded = string.Join(Environment.NewLine, decoded, DecodeExtensions(certificate));
            }

            return true;
        }

        private static string DecodeExtensions(X509Certificate2 certificate)
        {
            // Try to decode the X.509 extensions.
            try
            {
                StringBuilder extensionData = new();
                foreach (X509Extension x509Extension in certificate.Extensions)
                {
                    AsnEncodedData asnEncodedData = new(x509Extension.Oid, x509Extension.RawData);

                    // Add the name in brackets to match the previous output from X509Certificate.ToString()
                    extensionData.AppendLine($"[{x509Extension.Oid.FriendlyName}]");

                    // Add each line of the data, indented by two spaces to match the output from X509Certificate.ToString()
                    foreach (string dataLine in asnEncodedData.Format(multiLine: true).Split(Environment.NewLine))
                    {
                        extensionData.AppendLine($"  {dataLine}");
                    }
                }

                return extensionData.ToString().Trim();
            }
            catch (Exception ex)
            {
                Logger.LogFault("Failed to parse X.509 extensions from certificate.", ex);