如何获取 SHA256 证书指纹?

How to get SHA256 certificate thumbprint?

如何获取证书的SHA256指纹?。 SHA 256 证书有两个指纹,我可以检索主要指纹但不能检索 SHA256。

如果你想获得证书的 SHA256 指纹,你必须做一些手工工作。 Built-in Thumbprint 属性 仅适用于 SHA1。

你必须使用 SHA256 class 并计算证书内容的哈希值:

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.

这个帖子对我也有帮助。 Hashing text with SHA-256 at Windows Forms