DigitalSignatureUtil class

DigitalSignatureUtil class

Provides methods for signing document. To learn more, visit the Work with Digital Signatures documentation article.

Remarks

Since digital signature works with file content rather than Document Object Model these methods are put into a separate class.

Supported formats are: LoadFormat.DOC, LoadFormat.DOT, LoadFormat.DOCX, LoadFormat.DOTX, LoadFormat.DOCM, LoadFormat.ODT, LoadFormat.OTT.

Methods

NameDescription
load_signatures(file_name)Loads digital signatures from document.
load_signatures(stream)Loads digital signatures from document using stream.
remove_all_signatures(src_file_name, dst_file_name)Removes all digital signatures from source file and writes unsigned file to destination file. The following formats are compatible for digital signature removal: LoadFormat.DOC, LoadFormat.DOT, LoadFormat.DOCX, LoadFormat.DOTX, LoadFormat.DOCM, LoadFormat.ODT, LoadFormat.OTT.
remove_all_signatures(src_stream, dst_stream)Removes all digital signatures from document in source stream and writes unsigned document to destination stream. Output will be written to the start of stream and stream size will be updated with content length.
sign(src_stream, dst_stream, cert_holder, sign_options)Signs source document using given CertificateHolder and SignOptions with digital signature and writes signed document to destination stream. Supported formats are: LoadFormat.DOC, LoadFormat.DOT, LoadFormat.DOCX, LoadFormat.DOTX, LoadFormat.DOCM, LoadFormat.ODT, LoadFormat.OTT.
sign(src_file_name, dst_file_name, cert_holder, sign_options)Signs source document using given CertificateHolder and SignOptions with digital signature and writes signed document to destination file. Supported formats are: LoadFormat.DOC, LoadFormat.DOT, LoadFormat.DOCX, LoadFormat.DOTX, LoadFormat.DOCM, LoadFormat.ODT, LoadFormat.OTT.
sign(src_stream, dst_stream, cert_holder)Signs source document using given CertificateHolder with digital signature and writes signed document to destination stream. Supported formats are: LoadFormat.DOC, LoadFormat.DOT, LoadFormat.DOCX, LoadFormat.DOTX, LoadFormat.DOCM, LoadFormat.ODT, LoadFormat.OTT.
sign(src_file_name, dst_file_name, cert_holder)Signs source document using given CertificateHolder with digital signature and writes signed document to destination file. Supported formats are: LoadFormat.DOC, LoadFormat.DOT, LoadFormat.DOCX, LoadFormat.DOTX, LoadFormat.DOCM, LoadFormat.ODT, LoadFormat.OTT.

Examples

Shows how to load signatures from a digitally signed document.

# There are two ways of loading a signed document's collection of digital signatures using the DigitalSignatureUtil class.
# 1 -  Load from a document from a local file system filename:
digital_signatures = aw.digitalsignatures.DigitalSignatureUtil.load_signatures(MY_DIR + "Digitally signed.docx")

# If this collection is nonempty, then we can verify that the document is digitally signed.
self.assertEqual(1, digital_signatures.count)

# 2 -  Load from a document from a FileStream:
with open(MY_DIR + "Digitally signed.docx", "rb") as stream:
    digital_signatures = aw.digitalsignatures.DigitalSignatureUtil.load_signatures(stream)
    self.assertEqual(1, digital_signatures.count)

Shows how to remove digital signatures from a digitally signed document.

# There are two ways of using the DigitalSignatureUtil class to remove digital signatures
# from a signed document by saving an unsigned copy of it somewhere else in the local file system.
# 1 - Determine the locations of both the signed document and the unsigned copy by filename strings:
aw.digitalsignatures.DigitalSignatureUtil.remove_all_signatures(
    MY_DIR + "Digitally signed.docx",
    ARTIFACTS_DIR + "DigitalSignatureUtil.load_and_remove.from_string.docx")

# 2 - Determine the locations of both the signed document and the unsigned copy by file streams:
with open(MY_DIR + "Digitally signed.docx", "rb") as stream_in:
    with open(ARTIFACTS_DIR + "DigitalSignatureUtil.load_and_remove.from_stream.docx", "wb") as stream_out:
        aw.digitalsignatures.DigitalSignatureUtil.remove_all_signatures(stream_in, stream_out)

# Verify that both our output documents have no digital signatures.
self.assertListEqual([], aw.digitalsignatures.DigitalSignatureUtil.load_signatures(ARTIFACTS_DIR + "DigitalSignatureUtil.load_and_remove.from_string.docx"))
self.assertListEqual([], aw.digitalsignatures.DigitalSignatureUtil.load_signatures(ARTIFACTS_DIR + "DigitalSignatureUtil.load_and_remove.from_stream.docx"))

See Also