public byte[] ComputeCapiSha1OfPublicKey(PublicKey key)
        {
            unsafe
            {
                fixed(byte *pszOidValue = key.Oid.ValueAsAscii())
                {
                    byte[] encodedParameters = key.EncodedParameters.RawData;
                    fixed(byte *pEncodedParameters = encodedParameters)
                    {
                        byte[] encodedKeyValue = key.EncodedKeyValue.RawData;
                        fixed(byte *pEncodedKeyValue = encodedKeyValue)
                        {
                            Interop.Crypt32.CERT_PUBLIC_KEY_INFO publicKeyInfo = new Interop.Crypt32.CERT_PUBLIC_KEY_INFO()
                            {
                                Algorithm = new Interop.Crypt32.CRYPT_ALGORITHM_IDENTIFIER()
                                {
                                    pszObjId   = new IntPtr(pszOidValue),
                                    Parameters = new Interop.Crypt32.DATA_BLOB(new IntPtr(pEncodedParameters), (uint)encodedParameters.Length),
                                },

                                PublicKey = new Interop.Crypt32.CRYPT_BIT_BLOB()
                                {
                                    cbData      = encodedKeyValue.Length,
                                    pbData      = new IntPtr(pEncodedKeyValue),
                                    cUnusedBits = 0,
                                },
                            };

                            int cb = 20;

                            byte[] buffer = new byte[cb];
                            if (!Interop.Crypt32.CryptHashPublicKeyInfo(IntPtr.Zero, AlgId.CALG_SHA1, 0, Interop.Crypt32.CertEncodingType.All, ref publicKeyInfo, buffer, ref cb))
                            {
                                throw Marshal.GetHRForLastWin32Error().ToCryptographicException();
                            }
                            if (cb < buffer.Length)
                            {
                                byte[] newBuffer = new byte[cb];
                                Buffer.BlockCopy(buffer, 0, newBuffer, 0, cb);
                                buffer = newBuffer;
                            }
                            return(buffer);
                        }
                    }
                }
            }
        }
Beispiel #2
0
 public static partial bool CryptHashPublicKeyInfo(IntPtr hCryptProv, int algId, int dwFlags, Interop.Crypt32.CertEncodingType dwCertEncodingType, ref Interop.Crypt32.CERT_PUBLIC_KEY_INFO pInfo, byte[] pbComputedHash, ref int pcbComputedHash);