C Requires Chilkat v11.6.0+
C
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 C Downloads
#include <C_CkCrypt2.h>
#include <C_CkFileAccess.h>
void ChilkatSample(void)
{
HCkCrypt2 crypt;
HCkFileAccess files;
const char *size;
const char *hashHex;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
crypt = CkCrypt2_Create();
CkCrypt2_putHashAlgorithm(crypt,"sha256-tree-hash");
CkCrypt2_putEncodingMode(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 = CkFileAccess_Create();
size = CkFileAccess_fileSizeStr(files,"qa_data/jpg/penguins.jpg");
if (CkFileAccess_getLastMethodSuccess(files) != TRUE) {
printf("Could not read file size.\n");
CkCrypt2_Dispose(crypt);
CkFileAccess_Dispose(files);
return;
}
if (size == "0") {
CkCrypt2_putHashAlgorithm(crypt,"sha256");
}
hashHex = CkCrypt2_hashFileENC(crypt,"qa_data/jpg/penguins.jpg");
if (CkCrypt2_getLastMethodSuccess(crypt) != TRUE) {
printf("Hashing failed; check that the file exists and is readable.\n");
CkCrypt2_Dispose(crypt);
CkFileAccess_Dispose(files);
return;
}
printf("%s\n",hashHex);
CkCrypt2_Dispose(crypt);
CkFileAccess_Dispose(files);
}