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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoCrypt
    Handle hoFiles
    String sSize
    String sHashHex
    Boolean bTemp1

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Set ComHashAlgorithm Of hoCrypt To "sha256-tree-hash"
    Set ComEncodingMode Of hoCrypt To "hexlower"
    //  An empty input has the ordinary SHA-256 empty-input digest.
    //  Handle it explicitly because the tree-hash implementation expects a leaf.
    Get Create (RefClass(cComCkFileAccess)) To hoFiles
    If (Not(IsComObjectCreated(hoFiles))) Begin
        Send CreateComObject of hoFiles
    End
    Get ComFileSizeStr Of hoFiles "qa_data/jpg/penguins.jpg" To sSize
    Get ComLastMethodSuccess Of hoFiles To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Could not read file size."
        Procedure_Return
    End

    If (sSize = "0") Begin
        Set ComHashAlgorithm Of hoCrypt To "sha256"
    End

    Get ComHashFileENC Of hoCrypt "qa_data/jpg/penguins.jpg" To sHashHex
    Get ComLastMethodSuccess Of hoCrypt To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Hashing failed; check that the file exists and is readable."
        Procedure_Return
    End

    Showln sHashHex


End_Procedure