I'm having a hard time getting the day and time to digitally sign a PDF document for P7S.
Follow the documentation: link
There are also references on this site:
Someone has already gone through this and can help me?
Remember to add reference to System.Security
:
using System.IO;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApplication
{
class AssinaDigitalmente
{
public static void Main(string[] args)
{
string certificadoPath = args[0];
string pdfPath = args[1];
byte[] pdfBytes = File.ReadAllBytes(pdfPath);
byte[] pdfAssinado = Assina(certificadoPath, pdfBytes);
File.WriteAllBytes(Path.Combine(Path.GetDirectoryName(pdfPath), Path.GetFileNameWithoutExtension(pdfPath) + ".p7s"), pdfAssinado);
}
public static byte[] Assina(string certificadoPath, byte[] meusBytes)
{
ContentInfo info = new ContentInfo(meusBytes);
SignedCms signedCms = new SignedCms(info);
CmsSigner cmsSigner = new CmsSigner(X509Certificate2.CreateFromCertFile(certificadoPath) as X509Certificate2);
signedCms.ComputeSignature(cmsSigner);
return signedCms.Encode();
}
}
}