Node.js
Node.js
Validate a .pkpass Archive
See more Digital Signatures Examples
Opens a .pkpass archive (which is just a .zip renamed to .pkpass) and validates the contents. The hashes in the manifest are compared with the computed hash values for each individual file. If all computed hash values match, then the signature is verified.Chilkat Node.js Downloads
NODEJS_PRELUDE
function chilkatExample() {
var success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
var crypt = new chilkat.Crypt2();
var zip = new chilkat.Zip();
success = zip.OpenZip("qa_data/pkpass/invalid.pkpass");
if (success == false) {
console.log(zip.LastErrorText);
return;
}
// Get the contents of the manifest.json file, which contains something like this:
// {
// "icon.png" : "0296b01347b3173e98438a003b0e88986340b2d8",
// "logo.png" : "25de09e2d3b01ce1fe00c2ca9a90a2be1aaa05cf",
// "icon@2x.png" : "5afd9585b08c65fdf105a90c8bd643407cba2787",
// "pass.json" : "145ea5a5db784fff485126c77ecf7a1fc2a88ee7",
// "strip@2x.png" : "468fa7bc93e6b55342b56fda09bdce7c829d7d46",
// "strip.png" : "736d01f84cb73d06e8a9932e43076d68f19461ff"
// }
var ent = new chilkat.ZipEntry();
success = zip.EntryOf("manifest.json",ent);
if (success == false) {
console.log(zip.LastErrorText);
return;
}
// Get the exact content of the manifest.json for later signature verification.
var bdManifest = new chilkat.BinData();
success = ent.UnzipToBd(bdManifest);
var json = new chilkat.JsonObject();
json.EmitCompact = false;
json.Load(ent.UnzipToString(0,"utf-8"));
console.log(json.Emit());
// For each file in the JSON, get the filename and hex hash value.
crypt.EncodingMode = "hexlower";
crypt.HashAlgorithm = "sha1";
var someHashesFailed = false;
var filename;
var sbHashHex = new chilkat.StringBuilder();
var bdFileData = new chilkat.BinData();
var numMembers = json.Size;
var i = 0;
while (i < numMembers) {
filename = json.NameAt(i);
sbHashHex.Clear();
sbHashHex.Append(json.StringAt(i));
success = zip.EntryOf(filename,ent);
if (success == false) {
console.log(zip.LastErrorText);
return;
}
// Get the data for this file.
bdFileData.Clear();
success = ent.UnzipToBd(bdFileData);
var computedHashHex = crypt.HashBdENC(bdFileData);
if (sbHashHex.ContentsEqual(computedHashHex,false) == false) {
console.log("Computed hash does not match stored hash for " + filename);
console.log(" computed: " + computedHashHex);
console.log(" stored: " + sbHashHex.GetAsString());
someHashesFailed = true;
}
else {
console.log("hash verified for " + filename + "(" + computedHashHex + ")");
}
i = i+1;
}
if (someHashesFailed == true) {
console.log("Some hashes failed.");
return;
}
// Let's verify the signature..
// First get the signature.
success = zip.EntryOf("signature",ent);
if (success == false) {
console.log(zip.LastErrorText);
return;
}
var bdSignature = new chilkat.BinData();
success = ent.UnzipToBd(bdSignature);
// Show the contents of the signature in base64 encoding.
console.log("Signature:");
console.log(bdSignature.GetEncoded("base64_mime"));
console.log("----");
// Verify the signature against the manifest.json
crypt.EncodingMode = "base64";
var verified = crypt.VerifyBdENC(bdManifest,bdSignature.GetEncoded("base64"));
if (verified == false) {
console.log(crypt.LastErrorText);
}
console.log("signature verified = " + verified);
}
chilkatExample();