Posted by a ceTe Software moderator
Hi,
You can verify the signature contents in an existing PDF using DynamicPDF Core Suite for .NET. Here is a code sample. Please note, it will throw an exception if the data is invalid for the given signature.
string input = @"Path for a singed PDF";
// Create a PdfDocument object using the source PDF that is being verified for signatures
PdfDocument pdf = new PdfDocument(input);
// Get the list of PdfSignatureField from the pdf document
List<PdfSignatureField> signedSignatureFields = pdf.GetSignedSignatureFields();
PdfSignatureField pdfSignatureField = signedSignatureFields[0];
// Get the content that is signed using the selected signature field
byte[] signatureContent = pdfSignatureField.SignatureContent;
// Create a byte array to hold the data that is present in the signature range
byte[] dataToVerify = new byte[pdfSignatureField.Range[0].Length + pdfSignatureField.Range[1].Length];
// Get all the PDF file data that is being validated
byte[] fileData = File.ReadAllBytes(input);
// Load the dataToVerify from the PDF, using the range of the signature
Array.Copy(fileData, dataToVerify, pdfSignatureField.Range[0].Length);
Array.Copy(fileData, pdfSignatureField.Range[1].StartIndex, dataToVerify, pdfSignatureField.Range[0].Length, pdfSignatureField.Range[1].Length);
// Create a ContentInfo object using the data to be verified
System.Security.Cryptography.Pkcs.ContentInfo contentInfo = new ContentInfo(dataToVerify);
// Create a SignedCms object using contentInfo
SignedCms signedCms = new SignedCms(contentInfo, true);
// Decode the signature content
signedCms.Decode(signatureContent);
// Verify the signature. This method will throw an exception if the data is invalid for the given signature.
//passing boolean as false will verify both signature and certificate chain.
//passing true will verify only signature contents.
signedCms.CheckSignature(true);
Thanks,
ceTe Software Support Team