AutoIt
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
$oCrypt = ObjCreate("Chilkat.Crypt2")
$oZip = ObjCreate("Chilkat.Zip")
$bSuccess = $oZip.OpenZip("qa_data/pkpass/invalid.pkpass")
If ($bSuccess = False) Then
ConsoleWrite($oZip.LastErrorText & @CRLF)
Exit
EndIf
; 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"
; }
$oEnt = ObjCreate("Chilkat.ZipEntry")
$bSuccess = $oZip.EntryOf("manifest.json",$oEnt)
If ($bSuccess = False) Then
ConsoleWrite($oZip.LastErrorText & @CRLF)
Exit
EndIf
; Get the exact content of the manifest.json for later signature verification.
$oBdManifest = ObjCreate("Chilkat.BinData")
$bSuccess = $oEnt.UnzipToBd($oBdManifest)
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.EmitCompact = False
$oJson.Load($oEnt.UnzipToString(0,"utf-8"))
ConsoleWrite($oJson.Emit() & @CRLF)
; For each file in the JSON, get the filename and hex hash value.
$oCrypt.EncodingMode = "hexlower"
$oCrypt.HashAlgorithm = "sha1"
Local $bSomeHashesFailed = False
Local $sFilename
$oSbHashHex = ObjCreate("Chilkat.StringBuilder")
$oBdFileData = ObjCreate("Chilkat.BinData")
Local $iNumMembers = $oJson.Size
Local $i = 0
While $i < $iNumMembers
$sFilename = $oJson.NameAt($i)
$oSbHashHex.Clear
$oSbHashHex.Append($oJson.StringAt($i))
$bSuccess = $oZip.EntryOf($sFilename,$oEnt)
If ($bSuccess = False) Then
ConsoleWrite($oZip.LastErrorText & @CRLF)
Exit
EndIf
; Get the data for this file.
$oBdFileData.Clear()
$bSuccess = $oEnt.UnzipToBd($oBdFileData)
Local $sComputedHashHex = $oCrypt.HashBdENC($oBdFileData)
If ($oSbHashHex.ContentsEqual($sComputedHashHex,False) = False) Then
ConsoleWrite("Computed hash does not match stored hash for " & $sFilename & @CRLF)
ConsoleWrite(" computed: " & $sComputedHashHex & @CRLF)
ConsoleWrite(" stored: " & $oSbHashHex.GetAsString() & @CRLF)
$bSomeHashesFailed = True
Else
ConsoleWrite("hash verified for " & $sFilename & "(" & $sComputedHashHex & ")" & @CRLF)
EndIf
$i = $i + 1
Wend
If ($bSomeHashesFailed = True) Then
ConsoleWrite("Some hashes failed." & @CRLF)
Exit
EndIf
; Let's verify the signature..
; First get the signature.
$bSuccess = $oZip.EntryOf("signature",$oEnt)
If ($bSuccess = False) Then
ConsoleWrite($oZip.LastErrorText & @CRLF)
Exit
EndIf
$oBdSignature = ObjCreate("Chilkat.BinData")
$bSuccess = $oEnt.UnzipToBd($oBdSignature)
; Show the contents of the signature in base64 encoding.
ConsoleWrite("Signature:" & @CRLF)
ConsoleWrite($oBdSignature.GetEncoded("base64_mime") & @CRLF)
ConsoleWrite("----" & @CRLF)
; Verify the signature against the manifest.json
$oCrypt.EncodingMode = "base64"
Local $bVerified = $oCrypt.VerifyBdENC($oBdManifest,$oBdSignature.GetEncoded("base64"))
If ($bVerified = False) Then
ConsoleWrite($oCrypt.LastErrorText & @CRLF)
EndIf
ConsoleWrite("signature verified = " & $bVerified & @CRLF)