JScript Source: KeyFromCSP.js
var xmldoc, xmldsig, dsigKey var szResult = ""; KEYVALUE = 1; DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'"; PROV_RSA_FULL = 1; // Change this key container name to your own if necessary. RSA_KEY = "MyRSAFullKeys"; csp = PROV_RSA_FULL; key = RSA_KEY; sig = "signature_template.rsa.xml" if (loadSignature(sig)) { if (signWithKey(csp, key)) { alert(szResult); } } ///////// Helper functions: ///////// function alert(str) { WScript.echo(str); } ///////// Set signature for signing. //////// function loadSignature(file) { try { xmldoc = new ActiveXObject("Msxml2.DOMDOcument.5.0"); xmldsig= new ActiveXObject("Msxml2.MXDigitalSignature.5.0"); } catch (e) { alert("Installation of mxsml5 is required to run this app.\n"); return false; } xmldoc.async = false; xmldoc.preserveWhiteSpace = true; xmldoc.validateOnParse = false; if (!xmldoc.load(file)) { alert("Can't load "+ file + "\n"); return false; } szResult += "\nInput signature template:\n\n" + xmldoc.xml; xmldoc.setProperty("SelectionNamespaces", DSIGNS); xmldsig.signature = xmldoc.selectSingleNode(".//ds:Signature"); return true; } function signWithKey(dwCspType, szKeyContainer) { if (!xmldsig.signature) { alert("Must set signature template before attempting signing.\n"); return false; } var oKey = xmldsig.createKeyFromCSP(dwCspType,"",szKeyContainer,0); var oSignedKey = xmldsig.sign(oKey, KEYVALUE); if (oSignedKey == null) { alert("Signing failed.\n"); } else { szResult += ("\nThe data referenced in the signature template " + "was signed successfully.\n" + "Resultant signature:\n\n" + xmldoc.xml ); } return true; }
Try It!
- Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
- Copy the XML signature template from Resource: signature_template.itext.dsa.xml and paste it into a text file. Save the file as signature_template.rsa.xml.
- Copy the JScript listing above, and paste it into a text file. Save the file as signature.js, in the same directory where you saved signature_template.rsa.xml.
- From a command prompt, navigate to this directory, then type "cscript signingLetter.js".
Note Under operating systems other than Windows 2000 or Windows XP, you might need to install Windows Scripting Host (to run wscript.exe and cscript.exe), if it is not already installed.
- Verify that your output is similar to that listed in the Output topic.