Skip to content

Commit bcbcf09

Browse files
committed
Replace System.Cryptography functionality with BouncyCastle.
1 parent 6d33cfc commit bcbcf09

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Digipost.Api.Client/Internal/AuthenticationHandler.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
using System.Net.Http;
44
using System.Reflection;
55
using System.Runtime;
6-
using System.Security.Cryptography;
76
using System.Security.Cryptography.X509Certificates;
87
using System.Text;
98
using System.Threading;
109
using System.Threading.Tasks;
1110
using Digipost.Api.Client.Common;
1211
using Microsoft.Extensions.Logging;
12+
using Org.BouncyCastle.Crypto;
13+
using Org.BouncyCastle.Crypto.Digests;
14+
using Org.BouncyCastle.Security;
1315

1416
namespace Digipost.Api.Client.Internal
1517
{
@@ -87,10 +89,13 @@ private static string GetNetCoreVersion()
8789

8890
internal static string ComputeHash(byte[] inputBytes)
8991
{
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);
9297

93-
return Convert.ToBase64String(hashedBytes);
98+
return Convert.ToBase64String(hash);
9499
}
95100

96101
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
130135
_logger.LogDebug("=== SIGNATURE DATA END ===");
131136
}
132137

133-
var rsa2 = businessCertificate.GetRSAPrivateKey();
138+
byte[] messageBytes = Encoding.UTF8.GetBytes(messageHeader);
134139

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());
139148

140149
return base64Signature;
141150
}

0 commit comments

Comments
 (0)