background image

Generating an XML Signature

<< genenveloped Example | Creating a Public Key Pair >>
<< genenveloped Example | Creating a Public Key Pair >>
GENENVELOPED
E
XAMPLE
227
The sample program will generate an enveloped signature of the document in the
file
envelope.xml
and store it in the file
envelopedSignature.xml
in the cur-
rent working directory.
Generating an XML Signature
This example shows you how to generate an XML Signature using the XML
Digital Signature API. More specifically, the example generates an enveloped
XML Signature of an XML document. An enveloped signature is a signature that
is contained inside the content that it is signing. The example uses DOM (the
Document Object Model) to parse the XML document to be signed and a JSR
105 DOM implementation to generate the resulting signature.
A basic knowledge of XML Signatures and their different components is helpful
for understanding this section. See
http://www.w3.org/TR/xmldsig-core/
for
more information.
Instantiating the Document to be Signed
First, we use a JAXP
DocumentBuilderFactory
to parse the XML document
that we want to sign. An application obtains the default implementation for
Doc-
umentBuilderFactory
by calling the following line of code:
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
We must also make the factory namespace-aware:
dbf.setNamespaceAware(true);
Next, we use the factory to get an instance of a
DocumentBuilder
, which is used
to parse the document:
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(new FileInputStream(argv[0]));