Discuss this help topic in SecureBlackbox Forum

Sign document with XML signature handler

To sign a binary document using XML signature handler you need to

  1. load the document into an instance of TElOfficeBinaryDocument class
  2. create an instance of TElOfficeBinaryXMLSignatureHandler class and add it to the document using TElOfficeDocument.AddSignature() method
  3. specify the options of the signature using SignatureInfoV1, DigestMethod and other properties of the handler
  4. call Sign() method of the handler to sign the document
  5. use Close() or Flush() methods of the document to flush the changes

C#:


void SignBinaryXML(string sourceFilename, TElX509Certificate certificate)
{
    using (TElOfficeDocument Document = new TElOfficeDocument())
    {
        Document.Open(sourceFilename);
        if ((Document.DocumentFormat != TSBOfficeDocumentFormat.Binary) || !Document.Signable)
            throw new Exception("Cannot sign document using Binary XML signature handler");

        TElOfficeBinaryXMLSignatureHandler BinXMLSigHandler = new TElOfficeBinaryXMLSignatureHandler();
        Document.AddSignature(BinXMLSigHandler, true);

        BinXMLSigHandler.SignatureInfoV1.Included = true;
        BinXMLSigHandler.SignatureInfoV1.SignatureComments = "Comments";
        BinXMLSigHandler.Sign(certificate);
    }
}
Delphi:

procedure SignBinaryXML(const SourceFilename : string; Certificate : TElX509Certificate);
var
  Document : TElOfficeDocument;
  BinXMLSigHandler : TElOfficeBinaryXMLSignatureHandler;
begin
  Document := TElOfficeDocument.Create(nil);
  try
    Document.Open(SourceFilename);
    if (Document.DocumentFormat <> dfBinary) or (not Document.Signable) then
      raise Exception.Create('Cannot sign document using Binary XML signature handler');

    BinXMLSigHandler := TElOfficeBinaryXMLSignatureHandler.Create(nil);
    Document.AddSignature(BinXMLSigHandler, True);

    BinXMLSigHandler.SignatureInfoV1.Included := true;
    BinXMLSigHandler.SignatureInfoV1.SignatureComments := 'Comments';
    BinXMLSigHandler.Sign(Certificate);
  finally
    FreeAndNil(Document);
  end;
end;

How To articles about MS Office binary documents

Discuss this help topic in SecureBlackbox Forum