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

Code before:
                }
            }

            X509Certificate2 certificate;
            try
            {
                certificate = new X509Certificate2(Convert.FromBase64String(publicCert), password);
                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();

Review comment (refactor):
Should we move this block to a separate (private) method? I think it would improve readability and also would make it so that you could conditionally concatenate to the existing `decoded` value if a non-null/empty string return is handed back from that function.

Code after:
                }
            }

            X509Certificate2 certificate;
            try
            {
                certificate = new X509Certificate2(Convert.FromBase64String(publicCert), password);
                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}");
                    }