I’m trying to add an external signature with iText 7.
I have implemented IExternalSignature this way:
public byte[] Sign(byte[] bytes)
{
try
{
String HashToBeSigned = ComputeHash(bytes);
string signature = here goes the call to a server, that gives back the signature BASE64 encoded
return Encoding.UTF8.GetBytes(Base64.Decode(signature));
}
catch (Exception e)
{
return null;
}
}
private String ComputeHash(byte[] message)
{
try
{
SHA256Managed hashstring = new SHA256Managed();
byte[] hash = hashstring.ComputeHash(message);
string hashBase64 = Convert.ToBase64String(hash);
return hashBase64;
}
catch (Exception e)
{
return null;
}
}
And the Call of signDetached is:
public static async Task SignPdfFile(String accessToken, String credentialId, String pin, String otp, String inPath, String outPath)
{
try
{
PdfReader reader = new PdfReader(inPath);
PdfSigner signer = new PdfSigner(reader, new FileStream(outPath,FileMode.Create), false);
PdfSignatureAppearance appearance = signer.GetSignatureAppearance()
.SetReason("Reason")
.SetLocation("EN")
.SetReuseAppearance(false);
Rectangle rect = new Rectangle(36, 648, 200, 100);
appearance.SetPageRect(rect).SetPageNumber(1);
signer.SetFieldName("sig");
IExternalSignature pks = new CSCPAdESSignature(accessToken, credentialId, pin, otp);
X509Certificate [] chain = await CSC_API_Utils.GetCertChainAsync(accessToken, credentialId);
ICrlClient signingCertCrl = new CrlClientOnline(chain);
List crlList = new List ();
crlList.Add(signingCertCrl);
signer.SignDetached( pks, chain, crlList, null, null, 0, PdfSigner.CryptoStandard.CADES);
}
catch (Exception e)
{
}
}
At the end, I get PDF modified or corrupted after the document is signed