| Using
SecureXML
Digital Signature from JavaScript
The process of using SecureXML from JavaScript either on the client
side or on an ASP page on the server side starts with first creating
an instance of the SecureXML Signature object by doing a new ActiveXObjecton
on the client side and Server.CreateObject for server side usage:
var SigObj;
SigObj = new ActiveXObject("XMLSign.Signature") on the
client side and
SigObj = Server.CreateObject("XMLSign.Signature") on an
ASP page for server side usage.
Access to all methods and properties are straight forward as their
names remain exactly the same as in the XMLSign.idl file.
After completion of the SignNow() function, signedXML
var signedXML=null;
var signedXMLFile=null;
var count;
var newImagePath;
function SignNow()
{
var SigDate, ErrorCode;
SigDate = new Date();
// Create an instance of the SecureXML Digital Signature Object
var SigObj = new ActiveXObject("XMLSign.Signature");
// Set a few object properties
SigObj.SignatureID(0) = "MySignature";
SigObj.Properties(0,0) = "SignatureDate = " + SigDate.toString();
// Lets capture a live signature image
SigObj.PhysicalSignatureUsage = 2;
// Select the certificate to be used for subsequent signing.
// You can skip this step if you would rather see a certificate
selection window
SigObj.SetActiveCertificate(SigObj.GetCertificateInfo(0, 1));
// Sign a simple data string
signedXML = SigObj.SignDataStr("This is my input")
ErrorCode = SigObj.GetLastError()
if (ErrorCode != 0) {
if (ErrorCode == 13) {
alert("No Certificate Selected");
}
else {
if (ErrorCode == 45) {
alert("You either do not have signature creation license or
your license has expired");
}
else {
alert("Signature Creation Failed. Last Error = " + ErrorCode
+ " Error Stack = " + SigObj.GetError());
}
}
return false;
}
else { // Signature was created successfully
var uriCount, docUri, uriIndex, imageURI;
// Verify the signature we just created
SigObj.VerifyXMLStr(signedXML);
signatureId = SigObj.SignatureID(0);
// The following code shows how to extract the signature image that
was captured during signature creation
uriCount = SigObj.TotalUriCount;
imageURI = "#SignatureImage_" + signatureId;
for (uriIndex = 0; uriIndex < uriCount; uriIndex++) {
docUri = SigObj.DocumentURI(0,uriIndex);
if (docUri == imageURI) {
newImagePath = SigObj.SignedDocumentPath(0,uriIndex);
// newImagePath now contains the complete path for the file containing
the signature image
break;
}
}
}
return false;
}
|