Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - Digital Signatures

JScript Source: KeyFromHMACSecret.js

var xmldoc, xmldsig
var szResult = "";

NOKEYINFO = 0;
KEYVALUE  = 1;
CERTIFICATES = 2;
PURGE     = 4;

DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'";
sigTmp = "signature-template-enveloping-hmac-sha1.xml";
sigOut = "signature-enveloping-hmac-sha1.xml";
hmacSecret = "c2VjcmV0";
hmacLength =  -1;

if (InitXML()) 
{
  if (LoadXML(sigTmp)) {
    SignXML();
  }

  if (LoadXML(sigOut)) {
    VerifyXML();
  }
}

/////////  Helper functions: /////////
function alert(str) { WScript.echo(str); } 

///////// Set signature for signing. ////////
function InitXML()
{
   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;
   return true;
}

function LoadXML(file)
{
   if (xmldoc == null) {
     alert("must instantiate xml dom\n");
     return false;
   }
  
   if (!xmldoc.load(file)) {
     alert("Can't load "+ file + "\n");
     return false;
   }
   xmldoc.setProperty("SelectionNamespaces", DSIGNS);
   xmldsig.signature = xmldoc.selectSingleNode(".//ds:Signature");
   return true;
}

function SignXML()
{
   if (!xmldsig.signature) 
   {
      alert("Invalid signature template\n");
      return false;
   }

   var oKey = xmldsig.createKeyFromHMACSecret(hmacSecret, hmacLength);
   if (!oKey)
   {
      alert("Failed to create key from HMAC secret value\n");
      return false;
   }

   var oSignedKey = xmldsig.sign(oKey,KEYVALUE);
   if (!oSignedKey) 
   {
      alert("signing failed.\n");
      return false;
   }
   xmldoc.save(sigOut);
   alert("The data referenced in the signature template was "+
         "signed successfully.\nResultant signature:\n\n"+
         xmldoc.xml + "\n");
   return true;
}

function VerifyXML()
{
   if (!xmldsig.signature) {
      alert("Invalid signature.\n");
      return false;
   }
   var oKey = xmldsig.createKeyFromHMACSecret(hmacSecret, hmacLength);
   if (!oKey)
   {
      alert("Failed to create key from HMAC secret value\n");
      return false;
   }

   var oVerifiedKey = xmldsig.verify(oKey); 
   if (oVerifiedKey == null) {
      alert("Signature Verification failed.\n");
   }
   alert("\nThe data referenced in the signature object " +
            "was verified successfully.\n");
   return true;
}

Try It!

  1. Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
  2. Copy the signature template resource file, and paste it into a text editor. Save the file as signature-template-enveloping-hmac-sha1.xml.
  3. Copy the JScript listing above, and paste it into a code editor. Save the file as KeyFromHMACSecretjs, in the same directory where you saved signature-template-enveloping-hmac-sha1.xml.
  4. From a command prompt, navigate to this directory, then type "cscript KeyFromHMACSecret.js".
    Note   Under operating systems other than Windows 2000 or Windows XP, you might need to install Windows Scripting Host (to run wscript.exe or cscript.exe), if it is not already installed.
  5. Verify that your output is similar to that listed in the Output topic.