public void Sign_NoCertificatePassed_ExcelWorkbookSigned() { const string path = "Resources\\UnsignedWorkbook.xlsx"; using MemoryStream stream = FileCloner.CopyFileStreamToMemoryStream(path); using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true)) { DigitalSignatureManager.Sign(spreadsheetDocument.Package); } using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, false)) { VerifyResult verifyResult = DigitalSignatureManager.VerifySignature(spreadsheetDocument.Package); Assert.Equal(VerifyResult.Success, verifyResult); } File.WriteAllBytes("SignedWorkbook.xlsx", stream.ToArray()); }
public void Sign_UnsignedWorkbook_SuccessfullySigned() { // Load our unsigned Excel workbook into a MemoryStream for processing. const string path = "Resources\\UnsignedWorkbook.xlsx"; using MemoryStream stream = FileCloner.ReadAllBytesToMemoryStream(path); // Open the SpreadsheetDocument on the MemoryStream and sign it, using // the first digital signature certificate that we find. using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true)) { X509Certificate2 certificate = DigitalSignatureManager.GetDigitalSignatureCertificates().First(); DigitalSignatureManager.Sign(spreadsheetDocument, certificate); } // Save the signed Excel workbook to disk. When you open this workbook, // Excel should tell you that it has found a valid signature. File.WriteAllBytes("SignedWorkbook.xlsx", stream.ToArray()); }