[英]How to get SHA256 certificate thumbprint?
如何获得证书的SHA256指纹? SHA 256证书有两个指纹,我能够检索主指纹但不能检索SHA256。
如果您想获得证书的SHA256指纹,您必须做一些手动工作。 内置Thumbprint属性仅限SHA1。
哟必须使用SHA256类并计算证书内容的哈希值:
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace MyNamespace {
class MyClass {
public static String GetSha2Thumbprint(X509Certificate2 cert) {
Byte[] hashBytes;
using (var hasher = new SHA256Managed()) {
hashBytes = hasher.ComputeHash(cert.RawData);
}
return hashBytes.Aggregate(String.Empty, (str, hashByte) => str + hashByte.ToString("x2"));
}
}
}
并在必要时将此代码转换为扩展方法。
public static String GetSha2Thumbprint(X509Certificate2 cert)
{
Byte[] hashBytes;
using (var hasher = new SHA256Managed())
{
hashBytes = hasher.ComputeHash(cert.RawData);
}
string result = BitConverter.ToString(hashBytes)
// this will remove all the dashes in between each two haracters
.Replace("-", string.Empty).ToLower();
return result;
}
After getting the Hashbytes , you have to do the bit convertion.
这篇文章也对我有所帮助。 在Windows窗体中使用SHA-256哈希文本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.