如何获取证书的SHA-256指纹?
SHA-256证书有两个指纹,我能够检索到主要指纹,但无法检索到SHA-256指纹。
如何获取证书的SHA-256指纹?
SHA-256证书有两个指纹,我能够检索到主要指纹,但无法检索到SHA-256指纹。
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 characters
.Replace("-", string.Empty).ToLower();
return result;
}
在获取哈希字节后,您需要进行位转换。
这篇文章也对我有所帮助:在Windows Forms中使用SHA-256散列文本