簡體   English   中英

如何獲得SHA256證書指紋?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM