Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Zip,
Chilkat.JsonObject,
Chilkat.StringBuilder,
Chilkat.BinData,
Chilkat.ZipEntry,
Chilkat.Crypt2;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
crypt: TCrypt2;
zip: TZip;
ent: TZipEntry;
bdManifest: TBinData;
json: TJsonObject;
someHashesFailed: Boolean;
filename: string;
sbHashHex: TStringBuilder;
bdFileData: TBinData;
numMembers: Integer;
i: Integer;
computedHashHex: string;
bdSignature: TBinData;
verified: Boolean;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
crypt := TCrypt2.Create;
zip := TZip.Create;
success := zip.OpenZip('qa_data/pkpass/invalid.pkpass');
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
// 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"
// }
ent := TZipEntry.Create;
success := zip.EntryOf('manifest.json',ent);
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
// Get the exact content of the manifest.json for later signature verification.
bdManifest := TBinData.Create;
success := ent.UnzipToBd(bdManifest);
json := TJsonObject.Create;
json.EmitCompact := False;
json.Load(ent.UnzipToString(0,'utf-8'));
WriteLn(json.Emit());
// For each file in the JSON, get the filename and hex hash value.
crypt.EncodingMode := 'hexlower';
crypt.HashAlgorithm := 'sha1';
someHashesFailed := False;
sbHashHex := TStringBuilder.Create;
bdFileData := TBinData.Create;
numMembers := json.Size;
i := 0;
while i < numMembers do
begin
filename := json.NameAt(i);
sbHashHex.Clear();
sbHashHex.Append(json.StringAt(i));
success := zip.EntryOf(filename,ent);
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
// Get the data for this file.
bdFileData.Clear();
success := ent.UnzipToBd(bdFileData);
computedHashHex := crypt.HashBdENC(bdFileData);
if (sbHashHex.ContentsEqual(computedHashHex,False) = False) then
begin
WriteLn('Computed hash does not match stored hash for ' + filename);
WriteLn(' computed: ' + computedHashHex);
WriteLn(' stored: ' + sbHashHex.GetAsString());
someHashesFailed := True;
end
else
begin
WriteLn('hash verified for ' + filename + '(' + computedHashHex + ')');
end;
i := i + 1;
end;
if (someHashesFailed = True) then
begin
WriteLn('Some hashes failed.');
Exit;
end;
// Let's verify the signature..
// First get the signature.
success := zip.EntryOf('signature',ent);
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
bdSignature := TBinData.Create;
success := ent.UnzipToBd(bdSignature);
// Show the contents of the signature in base64 encoding.
WriteLn('Signature:');
WriteLn(bdSignature.GetEncoded('base64_mime'));
WriteLn('----');
// Verify the signature against the manifest.json
crypt.EncodingMode := 'base64';
verified := crypt.VerifyBdENC(bdManifest,bdSignature.GetEncoded('base64'));
if (verified = False) then
begin
WriteLn(crypt.LastErrorText);
end;
WriteLn('signature verified = ' + verified);
crypt.Free;
zip.Free;
ent.Free;
bdManifest.Free;
json.Free;
sbHashHex.Free;
bdFileData.Free;
bdSignature.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.