hi all,
Sorry I'm not good in VS C#, I just want to complete my job by importing C# Certificate class library into PB and hope anyone can help me. I get an solution from some where that can hashing Certificate. Would like to know how we can convert it into Class Library and import into PB. For this, what value should we pass in for Certificate object "X509Certificate2" ?
Below is the script from C# :
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
public class XadesSigner
{
public static byte[] SignDigest(string base64Digest, X509Certificate2 certificate)
{
if (string.IsNullOrEmpty(base64Digest) || certificate == null)
{
throw new ArgumentNullException("Digest and certificate must not be null or empty");
}
// Convert base64 digest string to byte array
byte[] digest = Convert.FromBase64String(base64Digest);
// Get the private key from the certificate
using (RSA rsa = certificate.GetRSAPrivateKey())
{
if (rsa == null)
{
throw new InvalidOperationException("Certificate does not have an RSA private key");
}
// Sign the digest using the RSA private key
byte[] signedDigest = rsa.SignHash(digest, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
return signedDigest;
}
}
public static void Main()
{
// Example base64-encoded digest value (SHA-256 hash of the data to be signed)
string base64Digest = "<enter DocDigest String>";
// Path to the certificate file
string certificatePath = @"<enter path to PFX file>";
// Load the certificate from a file or certificate store
X509Certificate2 certificate = new X509Certificate2(certificatePath, "<Enter PFX Password>");
// Sign the digest
byte[] signedDigest = SignDigest(base64Digest, certificate);
// Print the signed digest in base64
Console.WriteLine(Convert.ToBase64String(signedDigest));
}
}
regards,
Dev Ong
If the certificate is a file, then I suggest you modify the C# method a little bit to instead of passing the certificate as parameter, you pass the path of the certificate. Also, instead of returning a byte[] you pass it back out through a reference parameter. After those changes you will be able to import the C# class into PB by using the .NET Assembly Importer Tool: https://docs.appeon.com/pb2022r3/application_techniques/ch05s01s01.html
You can load the certificate into a X509Certificate2 object with the following method: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.createfrompemfile?view=net-6.0