Java Requires Chilkat v11.6.0+
Java
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.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.FileAccess;
public class ChilkatExample {
public static void main(String[] args) {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Crypt2 crypt = new Crypt2();
crypt.setHashAlgorithm("sha256-tree-hash");
crypt.setEncodingMode("hexlower");
// An empty input has the ordinary SHA-256 empty-input digest.
// Handle it explicitly because the tree-hash implementation expects a leaf.
FileAccess files = new FileAccess();
String size;
try {
size = files.fileSizeStr("qa_data/jpg/penguins.jpg");
} catch (ChilkatException e) {
System.out.println("Could not read file size.");
return;
}
if ("0".equals(size)) {
crypt.setHashAlgorithm("sha256");
}
String hashHex;
try {
hashHex = crypt.hashFileENC("qa_data/jpg/penguins.jpg");
} catch (ChilkatException e) {
System.out.println("Hashing failed; check that the file exists and is readable.");
return;
}
System.out.println(hashHex);
}
}