public unsafe string X500DistinguishedNameFormat(byte[] encodedDistinguishedName, bool multiLine) { if (encodedDistinguishedName == null || encodedDistinguishedName.Length == 0) { return(string.Empty); } FormatObjectStringType stringType = multiLine ? FormatObjectStringType.CRYPT_FORMAT_STR_MULTI_LINE : FormatObjectStringType.None; int cbFormat = 0; if (!Interop.crypt32.CryptFormatObject( CertEncodingType.X509_ASN_ENCODING, FormatObjectType.None, stringType, IntPtr.Zero, FormatObjectStructType.X509_NAME, encodedDistinguishedName, encodedDistinguishedName.Length, null, ref cbFormat)) { return(encodedDistinguishedName.ToHexStringUpper()); } int spanLength = (cbFormat + 1) / 2; Span <char> buffer = spanLength <= 256 ? stackalloc char[spanLength] : new char[spanLength]; fixed(char *ptr = &MemoryMarshal.GetReference(buffer)) { if (!Interop.crypt32.CryptFormatObject( CertEncodingType.X509_ASN_ENCODING, FormatObjectType.None, stringType, IntPtr.Zero, FormatObjectStructType.X509_NAME, encodedDistinguishedName, encodedDistinguishedName.Length, (byte *)ptr, ref cbFormat)) { return(encodedDistinguishedName.ToHexStringUpper()); } } return(new string(buffer.Slice(0, (cbFormat / 2) - 1))); }
public string X500DistinguishedNameFormat(byte[] encodedDistinguishedName, bool multiLine) { if (encodedDistinguishedName == null || encodedDistinguishedName.Length == 0) { return(string.Empty); } FormatObjectStringType stringType = multiLine ? FormatObjectStringType.CRYPT_FORMAT_STR_MULTI_LINE : FormatObjectStringType.None; int cbFormat = 0; if (!Interop.crypt32.CryptFormatObject( CertEncodingType.X509_ASN_ENCODING, FormatObjectType.None, stringType, IntPtr.Zero, FormatObjectStructType.X509_NAME, encodedDistinguishedName, encodedDistinguishedName.Length, null, ref cbFormat)) { return(encodedDistinguishedName.ToHexStringUpper()); } StringBuilder sb = new StringBuilder((cbFormat + 1) / 2); if (!Interop.crypt32.CryptFormatObject( CertEncodingType.X509_ASN_ENCODING, FormatObjectType.None, stringType, IntPtr.Zero, FormatObjectStructType.X509_NAME, encodedDistinguishedName, encodedDistinguishedName.Length, sb, ref cbFormat)) { return(encodedDistinguishedName.ToHexStringUpper()); } return(sb.ToString()); }
private static extern unsafe bool CryptFormatObject(CertEncodingType dwCertEncodingType, FormatObjectType dwFormatType, FormatObjectStringType dwFormatStrType, IntPtr pFormatStruct, IntPtr lpszStructType, [In] byte[] pbEncoded, int cbEncoded, byte *pbFormat, [In, Out] ref int pcbFormat);
public static unsafe bool CryptFormatObject(CertEncodingType dwCertEncodingType, FormatObjectType dwFormatType, FormatObjectStringType dwFormatStrType, IntPtr pFormatStruct, FormatObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, byte *pbFormat, ref int pcbFormat) { return(CryptFormatObject(dwCertEncodingType, dwFormatType, dwFormatStrType, pFormatStruct, (IntPtr)lpszStructType, pbEncoded, cbEncoded, pbFormat, ref pcbFormat)); }
private static extern bool CryptFormatObject(CertEncodingType dwCertEncodingType, FormatObjectType dwFormatType, FormatObjectStringType dwFormatStrType, IntPtr pFormatStruct, IntPtr lpszStructType, [In] byte[] pbEncoded, int cbEncoded, [Out] StringBuilder pbFormat, [In, Out] ref int pcbFormat);
public static bool CryptFormatObject(CertEncodingType dwCertEncodingType, FormatObjectType dwFormatType, FormatObjectStringType dwFormatStrType, IntPtr pFormatStruct, FormatObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, StringBuilder pbFormat, ref int pcbFormat) { return CryptFormatObject(dwCertEncodingType, dwFormatType, dwFormatStrType, pFormatStruct, (IntPtr)lpszStructType, pbEncoded, cbEncoded, pbFormat, ref pcbFormat); }