Permalink
Browse files

example

  • Loading branch information...
yaronn committed May 20, 2012
1 parent bc27292 commit 5374876c587067d02efad60003302e73dfbc58cd
Showing with 72 additions and 21 deletions.
  1. +36 −6 README.md
  2. +16 −0 example/client.pem
  3. +12 −0 example/client_public.pem
  4. +7 −14 { → example}/example.js
  5. +1 −1 index.js
View
@@ -1,17 +1,22 @@
-## Xml-Crypto
-A pure javascript xml digital signature library. Xml encryption is coming soon.
+## xml-crypto
+An xml digital signature library for node. Xml encryption is coming soon. Written is pure javascript!
-For more information visit my [blog](http://webservices20.blogspot.com/).
+For more information visit [my blog](http://webservices20.blogspot.com/).
## Install
Install with [npm](http://github.com/isaacs/npm):
npm install xml-crypto
-A pre requisite it to have [openssl](http://www.openssl.org/) installed and its /bin to be on the system path (this is so that node's built-in crypto module would work - xml-crypto does not use openssl directly)
-I used version 1.0.1c but it should probably work on older versions too.
+You (may) also need an xml parser and I recommend [xmldom](https://github.com/jindw/xmldom)
+
+ npm install xmldom
+
+A pre requisite it to have [openssl](http://www.openssl.org/) installed and its /bin to be on the system path. This is required by node's built-in crypto module - xml-crypto does not use openssl directly. I used version 1.0.1c but it should probably work on older versions too.
## Signing Xml documents
+Use this code:
+
var SignedXml = require('xml-crypto').SignedXml
, FileKeyInfo = require('xml-crypto').FileKeyInfo
, fs = require('fs')
@@ -28,8 +33,33 @@ I used version 1.0.1c but it should probably work on older versions too.
sig.computeSignature(xml)
fs.writeFileSync("signed.xml", sig.getSignedXml())
+The result wil be:
+
+ <library>
+ <book Id="_0">
+ <name>Harry Potter</name>
+ </book>
+ <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
+ <SignedInfo>
+ <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
+ <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
+ <Reference URI="#_0">
+ <Transforms>
+ <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
+ </Transforms>
+ <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
+ <DigestValue>cdiS43aFDQMnb3X8yaIUej3+z9Q=</DigestValue>
+ </Reference>
+ </SignedInfo>
+ <SignatureValue>vhWzpQyIYuncHUZV9W...[long base64 removed]...</SignatureValue>
+ </Signature>
+ </library>
+
+
Notes:
+<script src="https://gist.github.com/2604494.js?file=gistfile1.cs"></script>
+
sig.getSignedXml() returns the original xml document, with the signature pushed as the last child of the root node:
<library>
@@ -48,7 +78,7 @@ This assumes you are not signing the root node but only sub node(s), otherwise i
This sample uses [xmldom](https://github.com/jindw/xmldom) for xml dom manipulation.
You can use whichever dom parser you want.
- var select = require('xml-crypto').SelectNodes
+ var select = require('xml-crypto').xpath.SelectNodes
, dom = require('xmldom').DOMParser
, SignedXml = require('xml-crypto').SignedXml
, FileKeyInfo = require('xml-crypto').FileKeyInfo
View
@@ -0,0 +1,16 @@
+-----BEGIN PRIVATE KEY-----
+MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL4vpoH3H3byehjj
+7RAGxefGRATiq4mXtzc9Q91W7uT0DTaFEbjzVch9aGsNjmLs4QHsoZbuoUmi0st4
+x5z9SQpTAKC/dW8muzacT3E7dJJYh03MAO6RiH4LG34VRTq1SQN6qDt2rCK85eG4
+5NHI4jceptZNu6Zot1zyO5/PYuFpAgMBAAECgYAhspeyF3M/xB7WIixy1oBiXMLY
+isESFAumgfhwU2LotkVRD6rgNl1QtMe3kCNWa9pCWQcYkxeI0IzA+JmFu2shVvoR
+oL7eV4VCe1Af33z24E46+cY5grxNhHt/LyCnZKcitvCcrzXExUc5n6KngX0mMKgk
+W7skZDwsnKzhyUV8wQJBAN2bQMeASQVOqdfqBdFgC/NPnKY2cuDi6h659QN1l+kg
+X3ywdZ7KKftJo1G9l45SN9YpkyEd9zEO6PMFaufJvZUCQQDbtAWxk0i8BT3UTNWC
+T/9bUQROPcGZagwwnRFByX7gpmfkf1ImIvbWVXSpX68/IjbjSkTw1nj/Yj1NwFZ0
+nxeFAkEAzPhRpXVBlPgaXkvlz7AfvY+wW4hXHyyi0YK8XdPBi25XA5SPZiylQfjt
+Z6iN6qSfYqYXoPT/c0/QJR+orvVJNQJBANhRPNXljVTK2GDCseoXd/ZiI5ohxg+W
+UaA/1fDvQsRQM7TQA4NXI7BO/YmSk4rW1jIeOxjiIspY4MFAIh+7UL0CQFL6zTg6
+wfeMlEZzvgqwCGoLuvTnqtvyg45z7pfcrg2cHdgCXIy9kErcjwGiu6BOevEA1qTW
+Rk+bv0tknWvcz/s=
+-----END PRIVATE KEY-----
View
@@ -0,0 +1,12 @@
+-----BEGIN CERTIFICATE-----
+MIIBxDCCAW6gAwIBAgIQxUSXFzWJYYtOZnmmuOMKkjANBgkqhkiG9w0BAQQFADAW
+MRQwEgYDVQQDEwtSb290IEFnZW5jeTAeFw0wMzA3MDgxODQ3NTlaFw0zOTEyMzEy
+MzU5NTlaMB8xHTAbBgNVBAMTFFdTRTJRdWlja1N0YXJ0Q2xpZW50MIGfMA0GCSqG
+SIb3DQEBAQUAA4GNADCBiQKBgQC+L6aB9x928noY4+0QBsXnxkQE4quJl7c3PUPd
+Vu7k9A02hRG481XIfWhrDY5i7OEB7KGW7qFJotLLeMec/UkKUwCgv3VvJrs2nE9x
+O3SSWIdNzADukYh+Cxt+FUU6tUkDeqg7dqwivOXhuOTRyOI3HqbWTbumaLdc8juf
+z2LhaQIDAQABo0swSTBHBgNVHQEEQDA+gBAS5AktBh0dTwCNYSHcFmRjoRgwFjEU
+MBIGA1UEAxMLUm9vdCBBZ2VuY3mCEAY3bACqAGSKEc+41KpcNfQwDQYJKoZIhvcN
+AQEEBQADQQAfIbnMPVYkNNfX1tG1F+qfLhHwJdfDUZuPyRPucWF5qkh6sSdWVBY5
+sT/txBnVJGziyO8DPYdu2fPMER8ajJfl
+-----END CERTIFICATE-----
@@ -1,7 +1,7 @@
-var select = require('./lib/xpath.js').SelectNodes
+var select = require('xml-crypto').xpath.SelectNodes
, dom = require('xmldom').DOMParser
- , SignedXml = require('./lib/signed-xml.js').SignedXml
- , FileKeyInfo = require('./lib/signed-xml.js').FileKeyInfo
+ , SignedXml = require('xml-crypto').SignedXml
+ , FileKeyInfo = require('xml-crypto').FileKeyInfo
, fs = require('fs')
function signXml(xml, xpath, key, dest)
@@ -34,23 +34,16 @@ var xml = "<library>" +
//sign an xml document
signXml(xml,
"//*[local-name(.)='book']",
- "./test/static/client.pem",
- "c:\\temp\\result.xml")
+ "client.pem",
+ "result.xml")
console.log("xml signed succesfully")
-var signedXml = fs.readFileSync("c:\\temp\\result.xml").toString()
+var signedXml = fs.readFileSync("result.xml").toString()
console.log("validating signature...")
//validate an xml document
-if (validateXml(signedXml, "./test/static/client_public.pem"))
+if (validateXml(signedXml, "client_public.pem"))
console.log("signature is valid")
else
console.log("signature not valid")
-
-
-
-
-
-
-
View
@@ -1,2 +1,2 @@
module.exports = require('./lib/signed-xml')
-module.exports = require('./lib/xpath')
+module.exports.xpath = require('./lib/xpath')

0 comments on commit 5374876

Please sign in to comment.