Sample code for 30+ languages & platforms
PureBasic 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 PureBasic Downloads

PureBasic
IncludeFile "CkCrypt2.pb"
IncludeFile "CkFileAccess.pb"

Procedure ChilkatExample()

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCrypt2::setCkHashAlgorithm(crypt, "sha256-tree-hash")
    CkCrypt2::setCkEncodingMode(crypt, "hexlower")
    ;  An empty input has the ordinary SHA-256 empty-input digest.
    ;  Handle it explicitly because the tree-hash implementation expects a leaf.
    files.i = CkFileAccess::ckCreate()
    If files.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    size.s = CkFileAccess::ckFileSizeStr(files,"qa_data/jpg/penguins.jpg")
    If CkFileAccess::ckLastMethodSuccess(files) <> 1
        Debug "Could not read file size."
        CkCrypt2::ckDispose(crypt)
        CkFileAccess::ckDispose(files)
        ProcedureReturn
    EndIf

    If size = "0"
        CkCrypt2::setCkHashAlgorithm(crypt, "sha256")
    EndIf

    hashHex.s = CkCrypt2::ckHashFileENC(crypt,"qa_data/jpg/penguins.jpg")
    If CkCrypt2::ckLastMethodSuccess(crypt) <> 1
        Debug "Hashing failed; check that the file exists and is readable."
        CkCrypt2::ckDispose(crypt)
        CkFileAccess::ckDispose(files)
        ProcedureReturn
    EndIf

    Debug hashHex


    CkCrypt2::ckDispose(crypt)
    CkFileAccess::ckDispose(files)


    ProcedureReturn
EndProcedure