|
3 | 3 | using System.Net.Http; |
4 | 4 | using System.Reflection; |
5 | 5 | using System.Runtime; |
6 | | -using System.Security.Cryptography; |
7 | 6 | using System.Security.Cryptography.X509Certificates; |
8 | 7 | using System.Text; |
9 | 8 | using System.Threading; |
10 | 9 | using System.Threading.Tasks; |
11 | 10 | using Digipost.Api.Client.Common; |
12 | 11 | using Microsoft.Extensions.Logging; |
| 12 | +using Org.BouncyCastle.Crypto; |
| 13 | +using Org.BouncyCastle.Crypto.Digests; |
| 14 | +using Org.BouncyCastle.Security; |
13 | 15 |
|
14 | 16 | namespace Digipost.Api.Client.Internal |
15 | 17 | { |
@@ -87,10 +89,13 @@ private static string GetNetCoreVersion() |
87 | 89 |
|
88 | 90 | internal static string ComputeHash(byte[] inputBytes) |
89 | 91 | { |
90 | | - HashAlgorithm hashAlgorithm = new SHA256CryptoServiceProvider(); |
91 | | - var hashedBytes = hashAlgorithm.ComputeHash(inputBytes); |
| 92 | + IDigest digest = new Sha256Digest(); |
| 93 | + var hash = new byte[digest.GetDigestSize()]; |
| 94 | + |
| 95 | + digest.BlockUpdate(inputBytes, 0, inputBytes.Length); |
| 96 | + digest.DoFinal(hash, 0); |
92 | 97 |
|
93 | | - return Convert.ToBase64String(hashedBytes); |
| 98 | + return Convert.ToBase64String(hash); |
94 | 99 | } |
95 | 100 |
|
96 | 101 | internal static string ComputeSignature(string method, Uri uri, string date, string contentSha256Hash, |
@@ -130,12 +135,16 @@ internal static string ComputeSignature(string method, Uri uri, string date, str |
130 | 135 | _logger.LogDebug("=== SIGNATURE DATA END ==="); |
131 | 136 | } |
132 | 137 |
|
133 | | - var rsa2 = businessCertificate.GetRSAPrivateKey(); |
| 138 | + byte[] messageBytes = Encoding.UTF8.GetBytes(messageHeader); |
134 | 139 |
|
135 | | - var sha = SHA256.Create(); |
136 | | - var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(messageHeader)); |
137 | | - var signature = rsa2.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); |
138 | | - var base64Signature = Convert.ToBase64String(signature); |
| 140 | + |
| 141 | + var privKey = DotNetUtilities.GetRsaKeyPair(businessCertificate.GetRSAPrivateKey()); |
| 142 | + |
| 143 | + ISigner signer = SignerUtilities.GetSigner("SHA-256withRSA"); |
| 144 | + signer.Init(true, privKey.Private); |
| 145 | + signer.BlockUpdate(messageBytes, 0, messageBytes.Length); |
| 146 | + |
| 147 | + var base64Signature = Convert.ToBase64String(signer.GenerateSignature()); |
139 | 148 |
|
140 | 149 | return base64Signature; |
141 | 150 | } |
|
0 commit comments