Exemple #1
0
        public void MergeSignatures()
        {
            if (SignatureFiles.Count < 2)
            {
                MessageBox.Show("Please add at least two CAdES signature files");
                return;
            }

            if (!string.IsNullOrEmpty(EncapsulatedContentFile) && !File.Exists(EncapsulatedContentFile))
            {
                MessageBox.Show($"Data file not found: {EncapsulatedContentFile}");
                return;
            }

            try {
                // Read encapsulated content file

                byte[] encapsulatedContent = null;
                if (!string.IsNullOrEmpty(EncapsulatedContentFile))
                {
                    encapsulatedContent = File.ReadAllBytes(EncapsulatedContentFile);
                }

                // read signature files

                var signatures = new List <CadesSignature>();
                foreach (var signatureFile in SignatureFiles)
                {
                    signatures.Add(CadesSignature.Open(signatureFile));
                }

                // merge signatures

                var mergedSignature = CadesSignatureEditor.MergeSignatures(signatures, encapsulatedContent);

                // save file

                var saveFileDialog = new SaveFileDialog()
                {
                    FileName = $"merged-signature.p7s",
                };

                if (saveFileDialog.ShowDialog() != true)
                {
                    return;
                }

                File.WriteAllBytes(saveFileDialog.FileName, mergedSignature);
                MessageBox.Show($"Merged signature saved on {saveFileDialog.FileName}");
            } catch (Exception ex) {
                MessageBox.Show(ex.ToString(), "An error has occurred");
            }
        }
Exemple #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var detachedSignatures  = Storage.GetSampleCadesDetachedSignaturesToMerge();
            var encapsulatedContent = Storage.GetSampleDocContent();

            // Open each detached signature.
            var signatures = detachedSignatures.Select(ds => Lacuna.Pki.Cades.CadesSignature.Open(ds));

            // Merge signatures using the MergeSignatures() method by passing the list of detached signatures to
            // be merged and the encapsulated content.
            var mergedSignature = CadesSignatureEditor.MergeSignatures(signatures, encapsulatedContent);

            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=merged-signature.p7s");
            Response.BinaryWrite(mergedSignature);
            Response.End();
        }