Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.6.0+

Compute Glacier SHA-256 Tree Hash

See more Amazon Glacier Examples

For existing Amazon Glacier vault customers. The standalone vault service is closed to new customers. These examples use its REST API version 2012-06-01, not the S3 bucket APIs used by S3 Glacier storage classes. See AWS service guidance.

The tree hash hashes 1 MiB leaves, then combines pairs of raw SHA-256 digests until one digest remains; an unpaired digest is carried to the next level. Chilkat performs this calculation for the complete file. It is distinct from the ordinary linear SHA-256 used to sign the payload.

AWS reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
oleobject loo_Crypt
oleobject loo_Files
string ls_Size
string ls_HashHex

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Crypt.HashAlgorithm = "sha256-tree-hash"
loo_Crypt.EncodingMode = "hexlower"
//  An empty input has the ordinary SHA-256 empty-input digest.
//  Handle it explicitly because the tree-hash implementation expects a leaf.
loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")

ls_Size = loo_Files.FileSizeStr("qa_data/jpg/penguins.jpg")
if loo_Files.LastMethodSuccess <> 1 then
    Write-Debug "Could not read file size."
    destroy loo_Crypt
    destroy loo_Files
    return
end if

if ls_Size = "0" then
    loo_Crypt.HashAlgorithm = "sha256"
end if

ls_HashHex = loo_Crypt.HashFileENC("qa_data/jpg/penguins.jpg")
if loo_Crypt.LastMethodSuccess <> 1 then
    Write-Debug "Hashing failed; check that the file exists and is readable."
    destroy loo_Crypt
    destroy loo_Files
    return
end if

Write-Debug ls_HashHex


destroy loo_Crypt
destroy loo_Files