// Checks if two certificates have the same public key, keyalg, and keyparam.
internal static bool PublicKeyEquals( X509Certificate cert1, X509Certificate cert2 )
{
if (cert1 == null)
{
return (cert2 == null);
}
else if (cert2 == null)
{
return false;
}
byte[] publicKey1 = cert1.GetPublicKey();
String keyAlg1 = cert1.GetKeyAlgorithm();
byte[] keyAlgParam1 = cert1.GetKeyAlgorithmParameters();
byte[] publicKey2 = cert2.GetPublicKey();
String keyAlg2 = cert2.GetKeyAlgorithm();
byte[] keyAlgParam2 = cert2.GetKeyAlgorithmParameters();
// Keys are most likely to be different of the three components,
// so check them first
int len = publicKey1.Length;
if (len != publicKey2.Length) return(false);
for (int i = 0; i < len; i++) {
if (publicKey1[i] != publicKey2[i]) return(false);
}
if (!(keyAlg1.Equals(keyAlg2))) return(false);
len = keyAlgParam1.Length;
if (keyAlgParam2.Length != len) return(false);
for (int i = 0; i < len; i++) {
if (keyAlgParam1[i] != keyAlgParam2[i]) return(false);
}
return true;
}