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

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkCrypt2 crypt = new CkCrypt2();
    crypt.put_HashAlgorithm("sha256-tree-hash");
    crypt.put_EncodingMode("hexlower");
    //  An empty input has the ordinary SHA-256 empty-input digest.
    //  Handle it explicitly because the tree-hash implementation expects a leaf.
    CkFileAccess files = new CkFileAccess();
    String size = files.fileSizeStr("qa_data/jpg/penguins.jpg");
    if (files.get_LastMethodSuccess() != true) {
        Log.i(TAG, "Could not read file size.");
        return;
        }

    if (size == "0") {
        crypt.put_HashAlgorithm("sha256");
        }

    String hashHex = crypt.hashFileENC("qa_data/jpg/penguins.jpg");
    if (crypt.get_LastMethodSuccess() != true) {
        Log.i(TAG, "Hashing failed; check that the file exists and is readable.");
        return;
        }

    Log.i(TAG, hashHex);

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}