本文整理汇总了C#中X509Certificate2.GetCertHash方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2.GetCertHash方法的具体用法?C# X509Certificate2.GetCertHash怎么用?C# X509Certificate2.GetCertHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509Certificate2
的用法示例。
在下文中一共展示了X509Certificate2.GetCertHash方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestByteArrayConstructor
public static void TestByteArrayConstructor()
{
byte[] expectedThumbPrint = new byte[]
{
0x10, 0x8e, 0x2b, 0xa2, 0x36, 0x32, 0x62, 0x0c,
0x42, 0x7c, 0x57, 0x0b, 0x6d, 0x9d, 0xb5, 0x1a,
0xc3, 0x13, 0x87, 0xfe,
};
using (X509Certificate2 c = new X509Certificate2(TestData.MsCertificate))
{
IntPtr h = c.Handle;
object ignored;
Assert.NotEqual(IntPtr.Zero, h);
byte[] actualThumbprint = c.GetCertHash();
Assert.Equal(expectedThumbPrint, actualThumbprint);
c.Dispose();
// For compat reasons, Dispose() acts like the now-defunct Reset() method rather than causing ObjectDisposedExceptions.
h = c.Handle;
Assert.Equal(IntPtr.Zero, h);
Assert.Throws<CryptographicException>(() => c.GetCertHash());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithm());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParameters());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
Assert.Throws<CryptographicException>(() => c.GetPublicKey());
Assert.Throws<CryptographicException>(() => c.GetSerialNumber());
Assert.Throws<CryptographicException>(() => ignored = c.Issuer);
Assert.Throws<CryptographicException>(() => ignored = c.Subject);
}
}
示例2: TestDefaultConstructor
public static void TestDefaultConstructor()
{
using (X509Certificate2 c = new X509Certificate2())
{
IntPtr h = c.Handle;
object ignored;
Assert.Equal(IntPtr.Zero, h);
Assert.Throws<CryptographicException>(() => c.GetCertHash());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithm());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParameters());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
Assert.Throws<CryptographicException>(() => c.GetPublicKey());
Assert.Throws<CryptographicException>(() => c.GetSerialNumber());
Assert.Throws<CryptographicException>(() => ignored = c.Issuer);
Assert.Throws<CryptographicException>(() => ignored = c.Subject);
Assert.Throws<CryptographicException>(() => ignored = c.RawData);
Assert.Throws<CryptographicException>(() => ignored = c.Thumbprint);
Assert.Throws<CryptographicException>(() => ignored = c.SignatureAlgorithm);
Assert.Throws<CryptographicException>(() => ignored = c.HasPrivateKey);
Assert.Throws<CryptographicException>(() => ignored = c.Version);
Assert.Throws<CryptographicException>(() => ignored = c.Archived);
Assert.Throws<CryptographicException>(() => c.Archived = false);
Assert.Throws<CryptographicException>(() => c.FriendlyName = "Hi");
Assert.Throws<CryptographicException>(() => ignored = c.SubjectName);
Assert.Throws<CryptographicException>(() => ignored = c.IssuerName);
Assert.Throws<CryptographicException>(() => ignored = c.PrivateKey);
}
}
示例3: UseAfterDispose
public static void UseAfterDispose()
{
using (X509Certificate2 c = new X509Certificate2(TestData.MsCertificate))
{
IntPtr h = c.Handle;
// Do a couple of things that would only be true on a valid certificate, as a precondition.
Assert.NotEqual(IntPtr.Zero, h);
byte[] actualThumbprint = c.GetCertHash();
c.Dispose();
// For compat reasons, Dispose() acts like the now-defunct Reset() method rather than
// causing ObjectDisposedExceptions.
h = c.Handle;
Assert.Equal(IntPtr.Zero, h);
// State held on X509Certificate
Assert.ThrowsAny<CryptographicException>(() => c.GetCertHash());
Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithm());
Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithmParameters());
Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
Assert.ThrowsAny<CryptographicException>(() => c.GetPublicKey());
Assert.ThrowsAny<CryptographicException>(() => c.GetSerialNumber());
Assert.ThrowsAny<CryptographicException>(() => c.Issuer);
Assert.ThrowsAny<CryptographicException>(() => c.Subject);
Assert.ThrowsAny<CryptographicException>(() => c.NotBefore);
Assert.ThrowsAny<CryptographicException>(() => c.NotAfter);
// State held on X509Certificate2
Assert.ThrowsAny<CryptographicException>(() => c.RawData);
Assert.ThrowsAny<CryptographicException>(() => c.SignatureAlgorithm);
Assert.ThrowsAny<CryptographicException>(() => c.Version);
Assert.ThrowsAny<CryptographicException>(() => c.SubjectName);
Assert.ThrowsAny<CryptographicException>(() => c.IssuerName);
Assert.ThrowsAny<CryptographicException>(() => c.PublicKey);
Assert.ThrowsAny<CryptographicException>(() => c.Extensions);
#if netstandard17
Assert.ThrowsAny<CryptographicException>(() => c.PrivateKey);
#endif
}
}
示例4: TestConstructor
public static void TestConstructor()
{
byte[] expectedThumbprint = "71cb4e2b02738ad44f8b382c93bd17ba665f9914".HexToByteArray();
using (var c = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
{
string subject = c.Subject;
Assert.Equal("CN=MyName", subject);
byte[] thumbPrint = c.GetCertHash();
Assert.Equal(expectedThumbprint, thumbPrint);
}
}
示例5: TestThumbprint
public static void TestThumbprint()
{
byte[] expectedThumbprint = "108e2ba23632620c427c570b6d9db51ac31387fe".HexToByteArray();
string expectedThumbPrintString = "108E2BA23632620C427C570B6D9DB51AC31387FE";
using (var c = new X509Certificate2(TestData.MsCertificate))
{
byte[] thumbPrint = c.GetCertHash();
Assert.Equal(expectedThumbprint, thumbPrint);
Assert.Equal(expectedThumbPrintString, c.Thumbprint);
}
}
示例6: TestByteArrayConstructor_PEM
public static void TestByteArrayConstructor_PEM()
{
byte[] expectedThumbPrint =
{
0x10, 0x8e, 0x2b, 0xa2, 0x36, 0x32, 0x62, 0x0c,
0x42, 0x7c, 0x57, 0x0b, 0x6d, 0x9d, 0xb5, 0x1a,
0xc3, 0x13, 0x87, 0xfe,
};
using (X509Certificate2 cert = new X509Certificate2(TestData.MsCertificatePemBytes))
{
IntPtr h = cert.Handle;
Assert.NotEqual(IntPtr.Zero, h);
byte[] actualThumbprint = cert.GetCertHash();
Assert.Equal(expectedThumbPrint, actualThumbprint);
}
}
示例7: VerifyDefaultConstructor
private static void VerifyDefaultConstructor(X509Certificate2 c)
{
IntPtr h = c.Handle;
object ignored;
Assert.Equal(IntPtr.Zero, h);
Assert.ThrowsAny<CryptographicException>(() => c.GetCertHash());
Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithm());
Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithmParameters());
Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
Assert.ThrowsAny<CryptographicException>(() => c.GetPublicKey());
Assert.ThrowsAny<CryptographicException>(() => c.GetSerialNumber());
Assert.ThrowsAny<CryptographicException>(() => ignored = c.Issuer);
Assert.ThrowsAny<CryptographicException>(() => ignored = c.Subject);
Assert.ThrowsAny<CryptographicException>(() => ignored = c.RawData);
Assert.ThrowsAny<CryptographicException>(() => ignored = c.Thumbprint);
Assert.ThrowsAny<CryptographicException>(() => ignored = c.SignatureAlgorithm);
Assert.ThrowsAny<CryptographicException>(() => ignored = c.HasPrivateKey);
Assert.ThrowsAny<CryptographicException>(() => ignored = c.Version);
Assert.ThrowsAny<CryptographicException>(() => ignored = c.Archived);
Assert.ThrowsAny<CryptographicException>(() => c.Archived = false);
Assert.ThrowsAny<CryptographicException>(() => c.FriendlyName = "Hi");
Assert.ThrowsAny<CryptographicException>(() => ignored = c.SubjectName);
Assert.ThrowsAny<CryptographicException>(() => ignored = c.IssuerName);
#if netstandard17
Assert.ThrowsAny<CryptographicException>(() => c.GetCertHashString());
Assert.ThrowsAny<CryptographicException>(() => c.GetEffectiveDateString());
Assert.ThrowsAny<CryptographicException>(() => c.GetExpirationDateString());
Assert.ThrowsAny<CryptographicException>(() => c.GetPublicKeyString());
Assert.ThrowsAny<CryptographicException>(() => c.GetRawCertData());
Assert.ThrowsAny<CryptographicException>(() => c.GetRawCertDataString());
Assert.ThrowsAny<CryptographicException>(() => c.GetSerialNumberString());
#pragma warning disable 0618
Assert.ThrowsAny<CryptographicException>(() => c.GetIssuerName());
Assert.ThrowsAny<CryptographicException>(() => c.GetName());
#pragma warning restore 0618
#endif
}
示例8: TestHandleCtor
public static void TestHandleCtor()
{
IntPtr pCertContext = IntPtr.Zero;
byte[] rawData = TestData.MsCertificate;
unsafe
{
fixed (byte* pRawData = rawData)
{
CRYPTOAPI_BLOB certBlob = new CRYPTOAPI_BLOB() { cbData = rawData.Length, pbData = pRawData };
bool success = CryptQueryObject(
CertQueryObjectType.CERT_QUERY_OBJECT_BLOB,
ref certBlob,
ExpectedContentTypeFlags.CERT_QUERY_CONTENT_FLAG_CERT,
ExpectedFormatTypeFlags.CERT_QUERY_FORMAT_FLAG_BINARY,
0,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
out pCertContext
);
if (!success)
{
int hr = Marshal.GetHRForLastWin32Error();
throw new CryptographicException(hr);
}
}
}
// Now, create an X509Certificate around our handle.
using (X509Certificate2 c = new X509Certificate2(pCertContext))
{
// And release our ref-count on the handle. X509Certificate better be maintaining its own.
CertFreeCertificateContext(pCertContext);
// Now, test various properties to make sure the X509Certificate actually wraps our CERT_CONTEXT.
IntPtr h = c.Handle;
Assert.Equal(pCertContext, h);
pCertContext = IntPtr.Zero;
#if netstandard17
Assert.Equal(rawData, c.GetRawCertData());
Assert.Equal(rawData, c.GetRawCertDataString().HexToByteArray());
#endif
string issuer = c.Issuer;
Assert.Equal(
"CN=Microsoft Code Signing PCA, O=Microsoft Corporation, L=Redmond, S=Washington, C=US",
issuer);
byte[] expectedPublicKey = (
"3082010a0282010100e8af5ca2200df8287cbc057b7fadeeeb76ac28533f3adb" +
"407db38e33e6573fa551153454a5cfb48ba93fa837e12d50ed35164eef4d7adb" +
"137688b02cf0595ca9ebe1d72975e41b85279bf3f82d9e41362b0b40fbbe3bba" +
"b95c759316524bca33c537b0f3eb7ea8f541155c08651d2137f02cba220b10b1" +
"109d772285847c4fb91b90b0f5a3fe8bf40c9a4ea0f5c90a21e2aae3013647fd" +
"2f826a8103f5a935dc94579dfb4bd40e82db388f12fee3d67a748864e162c425" +
"2e2aae9d181f0e1eb6c2af24b40e50bcde1c935c49a679b5b6dbcef9707b2801" +
"84b82a29cfbfa90505e1e00f714dfdad5c238329ebc7c54ac8e82784d37ec643" +
"0b950005b14f6571c50203010001").HexToByteArray();
byte[] publicKey = c.GetPublicKey();
Assert.Equal(expectedPublicKey, publicKey);
byte[] expectedThumbPrint = "108e2ba23632620c427c570b6d9db51ac31387fe".HexToByteArray();
byte[] thumbPrint = c.GetCertHash();
Assert.Equal(expectedThumbPrint, thumbPrint);
}
}
示例9: TestNullConstructorArguments
public static void TestNullConstructorArguments()
{
Assert.Throws<ArgumentNullException>(() => new X509Certificate2((string)null));
Assert.Throws<ArgumentException>(() => new X509Certificate2(IntPtr.Zero));
Assert.Throws<ArgumentException>(() => new X509Certificate2((byte[])null, (string)null));
Assert.Throws<ArgumentException>(() => new X509Certificate2(Array.Empty<byte>(), (string)null));
Assert.Throws<ArgumentException>(() => new X509Certificate2((byte[])null, (string)null, X509KeyStorageFlags.DefaultKeySet));
Assert.Throws<ArgumentException>(() => new X509Certificate2(Array.Empty<byte>(), (string)null, X509KeyStorageFlags.DefaultKeySet));
// A null string password does not throw
using (new X509Certificate2(TestData.MsCertificate, (string)null)) { }
using (new X509Certificate2(TestData.MsCertificate, (string)null, X509KeyStorageFlags.DefaultKeySet)) { }
#if netstandard17
Assert.Throws<ArgumentNullException>(() => X509Certificate.CreateFromCertFile(null));
Assert.Throws<ArgumentNullException>(() => X509Certificate.CreateFromSignedFile(null));
Assert.Throws<ArgumentNullException>("cert", () => new X509Certificate2((X509Certificate2)null));
Assert.Throws<ArgumentException>("handle", () => new X509Certificate2(IntPtr.Zero));
// A null SecureString password does not throw
using (new X509Certificate2(TestData.MsCertificate, (SecureString)null)) { }
using (new X509Certificate2(TestData.MsCertificate, (SecureString)null, X509KeyStorageFlags.DefaultKeySet)) { }
#endif
// For compat reasons, the (byte[]) constructor (and only that constructor) treats a null or 0-length array as the same
// as calling the default constructor.
{
using (X509Certificate2 c = new X509Certificate2((byte[])null))
{
IntPtr h = c.Handle;
Assert.Equal(IntPtr.Zero, h);
Assert.ThrowsAny<CryptographicException>(() => c.GetCertHash());
}
}
{
using (X509Certificate2 c = new X509Certificate2(Array.Empty<byte>()))
{
IntPtr h = c.Handle;
Assert.Equal(IntPtr.Zero, h);
Assert.ThrowsAny<CryptographicException>(() => c.GetCertHash());
}
}
}
示例10: TestCopyConstructor_Pal
public static void TestCopyConstructor_Pal()
{
using (var c1 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
using (var c2 = new X509Certificate2(c1))
{
Assert.Equal(c1.GetCertHash(), c2.GetCertHash());
Assert.Equal(c1.GetKeyAlgorithm(), c2.GetKeyAlgorithm());
Assert.Equal(c1.GetKeyAlgorithmParameters(), c2.GetKeyAlgorithmParameters());
Assert.Equal(c1.GetKeyAlgorithmParametersString(), c2.GetKeyAlgorithmParametersString());
Assert.Equal(c1.GetPublicKey(), c2.GetPublicKey());
Assert.Equal(c1.GetSerialNumber(), c2.GetSerialNumber());
Assert.Equal(c1.Issuer, c2.Issuer);
Assert.Equal(c1.Subject, c2.Subject);
Assert.Equal(c1.RawData, c2.RawData);
Assert.Equal(c1.Thumbprint, c2.Thumbprint);
Assert.Equal(c1.SignatureAlgorithm.Value, c2.SignatureAlgorithm.Value);
Assert.Equal(c1.HasPrivateKey, c2.HasPrivateKey);
Assert.Equal(c1.Version, c2.Version);
Assert.Equal(c1.Archived, c2.Archived);
Assert.Equal(c1.SubjectName.Name, c2.SubjectName.Name);
Assert.Equal(c1.IssuerName.Name, c2.IssuerName.Name);
Assert.Equal(c1.GetCertHashString(), c2.GetCertHashString());
Assert.Equal(c1.GetEffectiveDateString(), c2.GetEffectiveDateString());
Assert.Equal(c1.GetExpirationDateString(), c2.GetExpirationDateString());
Assert.Equal(c1.GetPublicKeyString(), c2.GetPublicKeyString());
Assert.Equal(c1.GetRawCertData(), c2.GetRawCertData());
Assert.Equal(c1.GetRawCertDataString(), c2.GetRawCertDataString());
Assert.Equal(c1.GetSerialNumberString(), c2.GetSerialNumberString());
#pragma warning disable 0618
Assert.Equal(c1.GetIssuerName(), c2.GetIssuerName());
Assert.Equal(c1.GetName(), c2.GetName());
#pragma warning restore 0618
}
}
示例11: TestNullConstructorArguments
public static void TestNullConstructorArguments()
{
Assert.Throws<ArgumentException>(() => new X509Certificate2((byte[])null, (String)null));
Assert.Throws<ArgumentException>(() => new X509Certificate2(new byte[0], (String)null));
Assert.Throws<ArgumentException>(() => new X509Certificate2((byte[])null, (String)null, X509KeyStorageFlags.DefaultKeySet));
Assert.Throws<ArgumentException>(() => new X509Certificate2(new byte[0], (String)null, X509KeyStorageFlags.DefaultKeySet));
// For compat reasons, the (byte[]) constructor (and only that constructor) treats a null or 0-length array as the same
// as calling the default constructor.
{
using (X509Certificate2 c = new X509Certificate2((byte[])null))
{
IntPtr h = c.Handle;
Assert.Equal(IntPtr.Zero, h);
Assert.Throws<CryptographicException>(() => c.GetCertHash());
}
}
{
using (X509Certificate2 c = new X509Certificate2(new byte[0]))
{
IntPtr h = c.Handle;
Assert.Equal(IntPtr.Zero, h);
Assert.Throws<CryptographicException>(() => c.GetCertHash());
}
}
}
示例12: TestLoadSignedFile
public static void TestLoadSignedFile()
{
// X509Certificate2 can also extract the certificate from a signed file.
string path = Path.Combine("TestData", "Windows6.1-KB3004361-x64.msu");
if (!File.Exists(path))
throw new Exception(string.Format("Test infrastructure failure: Expected to find file \"{0}\".", path));
using (X509Certificate2 c = new X509Certificate2(path))
{
string issuer = c.Issuer;
Assert.Equal(
"CN=Microsoft Code Signing PCA, O=Microsoft Corporation, L=Redmond, S=Washington, C=US",
issuer);
string subject = c.Subject;
Assert.Equal(
"CN=Microsoft Corporation, OU=MOPR, O=Microsoft Corporation, L=Redmond, S=Washington, C=US",
subject);
byte[] expectedThumbprint = "67b1757863e3eff760ea9ebb02849af07d3a8080".HexToByteArray();
byte[] actualThumbprint = c.GetCertHash();
Assert.Equal(expectedThumbprint, actualThumbprint);
}
}
示例13: UseAfterDispose
public static void UseAfterDispose()
{
using (X509Certificate2 c = new X509Certificate2(TestData.MsCertificate))
{
IntPtr h = c.Handle;
// Do a couple of things that would only be true on a valid certificate, as a precondition.
Assert.NotEqual(IntPtr.Zero, h);
byte[] actualThumbprint = c.GetCertHash();
c.Dispose();
// For compat reasons, Dispose() acts like the now-defunct Reset() method rather than
// causing ObjectDisposedExceptions.
h = c.Handle;
Assert.Equal(IntPtr.Zero, h);
Assert.Throws<CryptographicException>(() => c.GetCertHash());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithm());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParameters());
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
Assert.Throws<CryptographicException>(() => c.GetPublicKey());
Assert.Throws<CryptographicException>(() => c.GetSerialNumber());
Assert.Throws<CryptographicException>(() => c.Issuer);
Assert.Throws<CryptographicException>(() => c.Subject);
}
}
示例14: TestConstructor_SecureString
public static void TestConstructor_SecureString(X509KeyStorageFlags keyStorageFlags)
{
using (SecureString password = TestData.CreatePfxDataPasswordSecureString())
using (var c = new X509Certificate2(TestData.PfxData, password, keyStorageFlags))
{
byte[] expectedThumbprint = "71cb4e2b02738ad44f8b382c93bd17ba665f9914".HexToByteArray();
string subject = c.Subject;
Assert.Equal("CN=MyName", subject);
byte[] thumbPrint = c.GetCertHash();
Assert.Equal(expectedThumbprint, thumbPrint);
}
}
示例15: TestLoadSignedFile
public static void TestLoadSignedFile()
{
// X509Certificate2 can also extract the certificate from a signed file.
string path = Path.Combine("TestData", "Windows6.1-KB3004361-x64.msu");
if (!File.Exists(path))
throw new Exception(string.Format("Test infrastructure failure: Expected to find file \"{0}\".", path));
using (X509Certificate2 c = new X509Certificate2(path))
{
string issuer = c.Issuer;
Assert.Equal(
"CN=Microsoft Code Signing PCA, O=Microsoft Corporation, L=Redmond, S=Washington, C=US",
issuer);
#if netstandard17
#pragma warning disable 0618
Assert.Equal(c.Issuer, c.GetIssuerName());
#pragma warning restore 0618
#endif
string subject = c.Subject;
Assert.Equal(
"CN=Microsoft Corporation, OU=MOPR, O=Microsoft Corporation, L=Redmond, S=Washington, C=US",
subject);
#if netstandard17
#pragma warning disable 0618
Assert.Equal(subject, c.GetName());
#pragma warning restore 0618
#endif
string expectedThumbprintHash = "67B1757863E3EFF760EA9EBB02849AF07D3A8080";
byte[] expectedThumbprint = expectedThumbprintHash.HexToByteArray();
byte[] actualThumbprint = c.GetCertHash();
Assert.Equal(expectedThumbprint, actualThumbprint);
#if netstandard17
string actualThumbprintHash = c.GetCertHashString();
Assert.Equal(expectedThumbprintHash, actualThumbprintHash);
#endif
}
}