Exemplo n.º 1
0
        public void PAdESSignTest()
        {
            byte[] certHas = GetCertHash("3bd3f17836bd00f8a756e6c53fca48539da2f042");

            // PAdES //////////////////

            // Get data to sign from file
            var pdfDataToSign = File.ReadAllBytes(inputPDFFile);

            // PAdES configuration
            var padesSignatureContext = new PAdESSignatureContext
            {
                SigningCertificate = new CertificateIdentifier
                {
                    CertificateHash = certHas
                },
                AdESAttributes = new AdESAttributes
                {
                    Level         = Level.XL,
                    HashAlgorithm = HashAlgorithm.SHA256
                },
                SignerAttributes = new SignerAttributes
                {
                    AuthorName = "Pavel Hryzlík"
                },
                PAdESSpecificAttributes = new PAdESSpecificAttributes {
                }
            };

            try
            {
                // PAdES sign
                var padesResult = adESProcessor.PAdESSign(pdfDataToSign, padesSignatureContext);

                // Save signed data to file
                File.WriteAllBytes(outputPDFFile, padesResult.signedData);
            }
            catch (Exception e)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public AdESResult PAdESSign(byte[] dataToSign, PAdESSignatureContext signatureContext)
        {
            AdESResult result;

#if NET5_0
            using (dynamic wr = CLRLoader.CLRLibrary
                                .CreateCLRInstance(BaseConstants.AdESCLRWrapperClassName))
#else
            using (dynamic wr = CLRLoader.CLRFrameworkLibrary
                                .CreateCLRInstance(BaseConstants.AdESCLRFrameworWrapperClassName))
#endif
            {
                wr.IsDebug = false;

                var coreSignatureContext = signatureContext.ConvertPAdESSignatureContext();
                var coreResult           = wr.PAdESSign(dataToSign, coreSignatureContext);

                result = DataConversion.ConvertAdESResult(coreResult);
            }
            return(result);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            string dir = Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().Location);

            inputCMSFile  = Path.Combine(dir, "..", "..", "..", "..", "..", @"test\AdESNet.TestData\hello.txt");
            outputCMSFile = Path.Combine(dir, "..", "..", "..", "..", "..", @"test\AdESNet.TestData\hello_signed.p7m");

            inputPDFFile  = Path.Combine(dir, "..", "..", "..", "..", "..", @"test\AdESNet.TestData\hello.pdf");
            outputPDFFile = Path.Combine(dir, "..", "..", "..", "..", "..", @"test\AdESNet.TestData\hello_signed.pdf");

            inputXMLFile  = Path.Combine(dir, "..", "..", "..", "..", "..", @"test\AdESNet.TestData\hello.xml");
            outputXMLFile = Path.Combine(dir, "..", "..", "..", "..", "..", @"test\AdESNet.TestData\hello_signed.xml");

            //TestCrypto32();
            //var cert = GetCert();

            // Cpp object
            using var wr = new AdESWrapper { IsDebug = true };

            byte[] certHas = GetCertHash("3bd3f17836bd00f8a756e6c53fca48539da2f042");

            // CAdES //////////////////

            // Get data to sign from file
            var cmdDataToSign = File.ReadAllBytes(inputCMSFile);

            // CAdES configuration
            var cadesSignatureContext = new SignatureContext
            {
                SigningCertificate = new CertificateIdentifier
                {
                    CertificateHash = certHas
                },
                AdESAttributes = new AdESAttributes
                {
                    Level         = Level.XL,
                    HashAlgorithm = HashAlgorithm.SHA256
                },
                SignerAttributes = new SignerAttributes
                {
                    AuthorName = "Pavel Hryzlík"
                }
            };

            // CAdES sign
            var cadesResult = wr.CAdESSign(cmdDataToSign, cadesSignatureContext);

            // Save signed data to file
            File.WriteAllBytes(outputCMSFile, cadesResult.signedData);

            // PAdES //////////////////

            // Get data to sign from file
            var pdfDataToSign = File.ReadAllBytes(inputPDFFile);

            // PAdES configuration
            var padesSignatureContext = new PAdESSignatureContext
            {
                SigningCertificate = new CertificateIdentifier
                {
                    CertificateHash = certHas
                },
                AdESAttributes = new AdESAttributes
                {
                    Level         = Level.XL,
                    HashAlgorithm = HashAlgorithm.SHA256
                },
                SignerAttributes = new SignerAttributes
                {
                    AuthorName = "Pavel Hryzlík"
                },
                PAdESSpecificAttributes = new PAdESSpecificAttributes {
                }
            };

            // PAdES sign
            var padesResult = wr.PAdESSign(pdfDataToSign, padesSignatureContext);

            // Save signed data to file
            File.WriteAllBytes(outputPDFFile, padesResult.signedData);

            // XAdES //////////////////

            // Get data to sign from file
            var xdfDataToSign = File.ReadAllBytes(inputXMLFile);

            // XAdES configuration
            var xadesSignatureContext = new XAdESSignatureContext
            {
                SigningCertificate = new CertificateIdentifier
                {
                    CertificateHash = certHas
                },
                AdESAttributes = new AdESAttributes
                {
                    Level         = Level.XL,
                    HashAlgorithm = HashAlgorithm.SHA256
                },
                SignerAttributes = new SignerAttributes
                {
                    AuthorName = "Pavel Hryzlík"
                },
                XAdESSpecificAttributes = new XAdESSpecificAttributes
                {
                    Packaging = PackagingType.Enveloped
                }
            };

            // XAdES sign
            var xadesResult = wr.XAdESSign(xdfDataToSign, xadesSignatureContext);

            // Save signed data to file
            File.WriteAllBytes(outputXMLFile, xadesResult.signedData);
        }
Exemplo n.º 4
0
 public Task <AdESResult> PAdESSignAsync(byte[] dataToSign, PAdESSignatureContext signatureContext)
 {
     return(Task.FromResult(PAdESSign(dataToSign, signatureContext)));
 }
Exemplo n.º 5
0
 public static dynamic ConvertPAdESSignatureContext(this PAdESSignatureContext pAdESSignatureContext)
 {
     return(AutoMapper.Map(pAdESSignatureContext, typeof(PAdESSignatureContext), Assembly.GetType(BaseConstants.PAdESSignatureContextClassName)));
 }