Skip to content

Commit 95a4f47

Browse files
committed
update docs
1 parent b5aa833 commit 95a4f47

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ The `SignedXml` constructor provides an abstraction for sign and verify xml docu
261261
- `keyInfoAttributes` - object - default `{}` - a hash of attributes and values `attrName: value` to add to the KeyInfo node
262262
- `getKeyInfoContent` - function - default `noop` - a function that returns the content of the KeyInfo node
263263
- `getCertFromKeyInfo` - function - default `SignedXml.getCertFromKeyInfo` - a function that returns the certificate from the `<KeyInfo />` node
264+
- `getObjectContent` - function - default `noop` - a function that returns the content of the `<Object/>` nodes
264265

265266
#### API
266267

@@ -272,6 +273,7 @@ To sign xml documents:
272273
- `xpath` - a string containing a XPath expression referencing a xml element
273274
- `transforms` - an array of [transform algorithms](#canonicalization-and-transformation-algorithms), the referenced element will be transformed for each value in the array
274275
- `digestAlgorithm` - one of the supported [hashing algorithms](#hashing-algorithms)
276+
- `isSignatureReference` - boolean - default `false` - indicates whether the target of this reference is located inside the `<Signature>` element (e.g. an `<Object>`)
275277
- `computeSignature(xml, [options])` - compute the signature of the given xml where:
276278
- `xml` - a string containing a xml document
277279
- `options` - an object with the following properties:
@@ -534,6 +536,44 @@ sig.computeSignature(xml, {
534536
});
535537
```
536538

539+
### how to add custom Objects to the signature
540+
541+
Use the `getObjectContent` option when creating a SignedXml instance to add custom Objects to the signature. You can also reference these Objects in your signature by setting `isSignatureReference` to `true` when adding a reference.
542+
543+
```javascript
544+
var SignedXml = require("xml-crypto").SignedXml,
545+
fs = require("fs");
546+
547+
var xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";
548+
549+
const sig = new SignedXml({
550+
privateKey: fs.readFileSync("client.pem"),
551+
canonicalizationAlgorithm: "http://www.w3.org/2001/10/xml-exc-c14n#",
552+
signatureAlgorithm: "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256",
553+
getObjectContent: () => [
554+
{
555+
content: "<TestObject>Test data in Object</TestObject>",
556+
attributes: {
557+
Id: "Object1",
558+
MimeType: "text/xml",
559+
},
560+
},
561+
],
562+
});
563+
564+
// Add a reference to the Object element
565+
sig.addReference({
566+
xpath: "//*[@Id='Object1']",
567+
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
568+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
569+
// IMPORTANT: Set isSignatureReference to true to indicate this is a reference to an element inside the Signature
570+
isSignatureReference: true,
571+
});
572+
573+
sig.computeSignature(xml);
574+
fs.writeFileSync("signed.xml", sig.getSignedXml());
575+
```
576+
537577
### more examples (_coming soon_)
538578

539579
## Development

0 commit comments

Comments
 (0)