简体   繁体   English

在c#中使用SHA 256的哈希X509证书

[英]Hash X509 certificate with SHA 256 in c#

I work on an EBICS implementation in C# and I need to send to my bank the hash of my three certificates in SHA256 format in order to enable EBICS link with it. 我在C#中使用EBICS实现,我需要向我的银行发送SHA256格式的三个证书的哈希值,以便启用EBICS链接。

I generated them in C# with BouncyCastle and now I have a X509Certificate2 object. 我使用BouncyCastle在C#中生成它们,现在我有一个X509Certificate2对象。

So to hash my certificate I used the following code : 为了散列我的证书,我使用了以下代码:

String HashCertificate = Certificat.GetCertHashString();

And he return me the following result : 他还给我以下结果:

21952A5F79CA3232A656794EE4532BECF5AE3960 21952A5F79CA3232A656794EE4532BECF5AE3960

But the length don't match with the lenght of the hash of the bank certificate : 但是长度与银行证书的散列长度不匹配:

57436AD3D09989ED74F4FCCDBF7668C43F8BF87C933F90B065ED442A22E5B0BF 57436AD3D09989ED74F4FCCDBF7668C43F8BF87C933F90B065ED442A22E5B0BF

So I think the GetCertHashString() function return the hash of the certificate in SHA1 format and I have no idea how I can hash it in SHA256 . 所以我认为GetCertHashString()函数以SHA1格式返回证书的哈希值,我不知道如何在SHA256中哈希它。

Can you help me please ? 你能帮我吗 ?

Thank you in advance 先感谢您

As MSDN says GetCertHashString method always 由于MSDN总是 GetCertHashString方法

Returns the SHA1 hash value for the X.509v3 certificate as a hexadecimal string. 以十六进制字符串形式返回X.509v3证书的SHA1哈希值。

regardless of signature algorithm since it is Windows specific thumbprint used internally in certifcates store. 无论签名算法如何,因为它是certifcates商店内部使用的Windows特定指纹。

You can calculate any other hash by accessing certificate content from its RawData property, eg for SHA256: 您可以通过从其RawData属性访问证书内容来计算任何其他哈希,例如SHA256:

using (var hasher = SHA256.Create())
{
    var hash = hasher.ComputeHash(cert.RawData);
    Console.WriteLine(BitConverter.ToString(hash));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM