Skip to content

Commit 90211ee

Browse files
committed
980802: Added workable code.
1 parent 66dc330 commit 90211ee

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

  • Digital Signature/Externally-sign-the-PDF-document-using-IPdfExternalSigner/.NET/Externally-sign-the-PDF-document

Digital Signature/Externally-sign-the-PDF-document-using-IPdfExternalSigner/.NET/Externally-sign-the-PDF-document/Program.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class Program
1111
static void Main(string[] args)
1212
{
1313
// Get the stream from the document
14-
FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Barcode.pdf"), FileMode.Open, FileAccess.Read);
14+
FileStream documentStream = new FileStream(Path.GetFullPath(@"../../../Data/Barcode.pdf"), FileMode.Open, FileAccess.Read);
1515

1616
// Load the existing PDF document
1717
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream);
@@ -29,11 +29,11 @@ static void Main(string[] args)
2929

3030
// Add public certificates
3131
List<X509Certificate2> certificates = new List<X509Certificate2>();
32-
certificates.Add(new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "password123"));
32+
certificates.Add(new X509Certificate2(Path.GetFullPath(@"../../../Data/PDF.pfx"), "password123"));
3333
signature.AddExternalSigner(externalSignature, certificates, null);
3434

3535
// Create file stream
36-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
36+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
3737
{
3838
// Save the PDF document to file stream
3939
loadedDocument.Save(outputFileStream);
@@ -61,21 +61,19 @@ public ExternalSigner(string hashAlgorithm)
6161
public byte[] Sign(byte[] message, out byte[] timeStampResponse)
6262
{
6363
timeStampResponse = null;
64+
X509Certificate2 digitalID = new X509Certificate2(Path.GetFullPath(@"../../../Data/PDF.pfx"), "password123");
6465

65-
FileStream certificateStream = new FileStream("PDF.pfx", FileMode.Open, FileAccess.Read);
66-
X509Certificate2 digitalID = new X509Certificate2(certificateStream, "password123");
67-
68-
if (digitalID.PrivateKey is System.Security.Cryptography.RSACryptoServiceProvider rsaProvider)
66+
if (digitalID.PrivateKey is RSACryptoServiceProvider rsaProvider)
6967
{
7068
return rsaProvider.SignData(message, HashAlgorithm);
7169
}
7270
else if (digitalID.PrivateKey is RSACng rsaCng)
7371
{
74-
return rsaCng.SignData(message, System.Security.Cryptography.HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
72+
return rsaCng.SignData(message, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
7573
}
76-
else if (digitalID.PrivateKey is System.Security.Cryptography.RSAOpenSsl rsaOpenSsl)
74+
else if (digitalID.PrivateKey is RSAOpenSsl rsaOpenSsl)
7775
{
78-
return rsaOpenSsl.SignData(message, System.Security.Cryptography.HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
76+
return rsaOpenSsl.SignData(message, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
7977
}
8078

8179
return null;

0 commit comments

Comments
 (0)