2016-01-04 118 views
6

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

+0

在Windows上,signtool程序允许将2个x509证书添加到文件中以获得authenticode散列。第一个证书是SHA1证书 - 用于向后兼容。然后有一个未认证的属性,它具有第二个SHA 256证书。因此,我想要访问/读取SHA 256指纹?2指纹 – mksteve

+0

?您能否帮助我使用Class或Property(使用C#)?谢谢你回复 – Mahadev

+0

你能解释一下你的任务吗?目前还不清楚你想获得SHA256指纹的地方?从证书本身?签名的内容?其他??? – Crypt32

回答

0
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

1

如果你想获得证书的SHA256指纹,你必须做一些手动工作。内置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")); 
     } 
    } 
} 

,如有必要,你这个代码转换为一个扩展方法。

+0

谢谢你的回复,但是这段代码没有给出正确的SHA-256指纹证书 – Mahadev

+0

你能否说出你的陈述? – Crypt32

+0

转到任何网站(其中有https:\\), 在firefox中打开该网站,查看证书的哪个网站,其中显示了SHA 256指纹和SHA 1指纹。 使用上面的代码比较浏览器中显示的返回字符串和SHA 256指纹。这是显示不同的数据 – Mahadev