Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.6.0+

S3 Http: Download and Verify a Full-Object SHA-256 Checksum

See more S3 - HTTP Examples

Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.

Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.

Uses explicit signed Http requests to preserve query parameters, conditional headers, and full responses. The included SigV4 block signs the exact path, sorted encoded query and headers used by this example; it is not a general-purpose HTTP signing library. Keep the clock accurate and do not change signed data after signing. Code requires Chilkat 11.6.0 or later; locally tested with 11.8.0.

For objects uploaded with a FULL_OBJECT SHA-256 checksum, reads HEAD checksum metadata, pins a returned version ID and ETag, downloads using If-Match, and checks returned metadata, actual byte count and the locally computed base64 SHA-256 before renaming. Empty objects are supported. Create qa_output first; existing output paths are not overwritten.

COMPOSITE or absent SHA-256 checksums stop explicitly. Use the companion single-PUT checksum upload to create a suitable object. Requires s3:GetObject/s3:GetObjectVersion and applicable KMS permissions. No multipart ETag-as-MD5 assumption.

AWS reference

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
stringCompare: TChilkatStringBuilder;
credentials: TChilkatJsonObject;
stringMatch1: Integer;
stringMatch2: Integer;
http: TChilkatHttp;
stringMatch3: Integer;
region: WideString;
endpoint: TChilkatStringBuilder;
bucketName: WideString;
objectKey: WideString;
response: TChilkatHttpResponse;
signer: TChilkatCrypt2;
now: TCkDateTime;
timestamp: TChilkatStringBuilder;
canonical: TChilkatStringBuilder;
canonicalHeaders: TChilkatStringBuilder;
signedHeaders: TChilkatStringBuilder;
scope: TChilkatStringBuilder;
stringToSign: TChilkatStringBuilder;
signingKey: TChilkatStringBuilder;
authorization: TChilkatStringBuilder;
url: TChilkatStringBuilder;
query: TChilkatStringBuilder;
number: TChilkatStringBuilder;
host: TChilkatStringBuilder;
uri: TChilkatStringBuilder;
xml: TChilkatXml;
amzDate: WideString;
dateStamp: WideString;
payloadHash: WideString;
canonicalHash: WideString;
derivedKey: WideString;
signature: WideString;
replaceCount: Integer;
versionId: WideString;
etag: WideString;
expectedSize: WideString;
expectedChecksum: WideString;
checksumType: WideString;
returnedVersion: WideString;
files: TCkFileAccess;
finalPath: WideString;
partialPath: WideString;
stringMatch4: Integer;
stringMatch5: Integer;
stringMatch6: Integer;
stringMatch7: Integer;
stringMatch8: Integer;
stringMatch9: Integer;
stringMatch10: Integer;
stringMatch11: Integer;
stringMatch12: Integer;
received: TChilkatMime;
receivedSize: WideString;
receivedHash: WideString;
receivedType: WideString;
stringMatch13: Integer;
stringMatch14: Integer;
stringMatch15: Integer;
stringMatch16: Integer;
stringMatch17: Integer;
stringMatch18: Integer;
actualSize: WideString;
stringMatch19: Integer;
digest: TChilkatCrypt2;
actualChecksum: WideString;
stringMatch20: Integer;

begin
success := 0;

stringCompare := TChilkatStringBuilder.Create(Self);
//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.
//  Load private credentials; never put access keys or session tokens in source.
credentials := TChilkatJsonObject.Create(Self);
credentials.Strict := 1;
success := credentials.LoadFile('qa_data/aws/s3.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load private S3 credentials.');
    Exit;
  end;
stringCompare.SetString(credentials.StringOf('access_key'));
stringMatch1 := stringCompare.ContentsEqual('',1);
if ((credentials.JsonTypeOf('access_key') <> 1) or (stringMatch1 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid access_key.');
    Exit;
  end;
stringCompare.SetString(credentials.StringOf('secret_key'));
stringMatch2 := stringCompare.ContentsEqual('',1);
if ((credentials.JsonTypeOf('secret_key') <> 1) or (stringMatch2 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid secret_key.');
    Exit;
  end;
http := TChilkatHttp.Create(Self);
http.AwsAccessKey := credentials.StringOf('access_key');
http.AwsSecretKey := credentials.StringOf('secret_key');
if (credentials.HasMember('session_token') = 1) then
  begin
    stringCompare.SetString(credentials.StringOf('session_token'));
    stringMatch3 := stringCompare.ContentsEqual('',1);
    if ((credentials.JsonTypeOf('session_token') <> 1) or (stringMatch3 = 1)) then
      begin
        Memo1.Lines.Add('session_token must be a nonempty string when supplied.');
        Exit;
      end;
    http.AwsSessionToken := credentials.StringOf('session_token');
  end;
//  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
//  Set the actual bucket region, not the caller's location.
region := 'us-west-2';
http.AwsRegion := region;
endpoint := TChilkatStringBuilder.Create(Self);
endpoint.Append('s3.');
endpoint.Append(region);
endpoint.Append('.amazonaws.com');
http.AwsEndpoint := endpoint.GetAsString();
http.RequireSslCertVerify := 1;
http.FollowRedirects := 0;
//  A region redirect is not success. Correct the endpoint and signing region.
bucketName := 'your-bucket-name';
objectKey := 'examples/archive.bin';
response := TChilkatHttpResponse.Create(Self);
signer := TChilkatCrypt2.Create(Self);
signer.Charset := 'utf-8';
signer.HashAlgorithm := 'sha256';
signer.EncodingMode := 'hexlower';
signer.MacAlgorithm := 'hmac';
now := TCkDateTime.Create(Self);
timestamp := TChilkatStringBuilder.Create(Self);
canonical := TChilkatStringBuilder.Create(Self);
canonicalHeaders := TChilkatStringBuilder.Create(Self);
signedHeaders := TChilkatStringBuilder.Create(Self);
scope := TChilkatStringBuilder.Create(Self);
stringToSign := TChilkatStringBuilder.Create(Self);
signingKey := TChilkatStringBuilder.Create(Self);
authorization := TChilkatStringBuilder.Create(Self);
url := TChilkatStringBuilder.Create(Self);
query := TChilkatStringBuilder.Create(Self);
number := TChilkatStringBuilder.Create(Self);
host := TChilkatStringBuilder.Create(Self);
uri := TChilkatStringBuilder.Create(Self);
xml := TChilkatXml.Create(Self);

host.Append(bucketName);
host.Append('.');
host.Append(endpoint.GetAsString());
//  Encode the original key exactly once, preserving S3 path separators.
uri.SetString(http.UrlEncode(objectKey));
replaceCount := uri.ReplaceNoCase('%2f','/');
if ((uri.StartsWith('/',1) = 1) or (uri.Contains('/../',1) = 1) or (uri.Contains('/./',1) = 1)) then
  begin
    Memo1.Lines.Add('Use a key without a leading slash or dot path segments for these examples.');
    Exit;
  end;
uri.Prepend('/');
versionId := '';

files := TCkFileAccess.Create(Self);
finalPath := 'qa_output/s3-download.bin';
partialPath := 'qa_output/s3-download.bin.part';
if ((files.FileExists(finalPath) = 1) or (files.FileExists(partialPath) = 1)) then
  begin
    Memo1.Lines.Add('Choose unused output paths.');
    Exit;
  end;
query.Clear();
stringCompare.SetString(versionId);
stringMatch4 := stringCompare.ContentsEqual('',1);
if (stringMatch4 <> 1) then
  begin
    if (query.Length > 0) then
      begin
        query.Append('&');
      end;
    query.Append('versionId=');
    query.Append(http.UrlEncode(versionId));
  end;
http.ClearHeaders();
success := now.SetFromCurrentSystemTime();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read system time.');
    Exit;
  end;
timestamp.SetString(now.GetAsTimestamp(0));
replaceCount := timestamp.Replace('-','');
replaceCount := timestamp.Replace(':','');
amzDate := timestamp.GetAsString();
dateStamp := timestamp.GetRange(0,8,0);
payloadHash := signer.HashStringENC('');
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not hash payload.');
    Exit;
  end;
canonicalHeaders.Clear();
signedHeaders.Clear();
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('host');
canonicalHeaders.Append('host:');
canonicalHeaders.Append(host.GetAsString());
canonicalHeaders.Append(#10);
http.SetRequestHeader('host',host.GetAsString());
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('x-amz-checksum-mode');
canonicalHeaders.Append('x-amz-checksum-mode:');
canonicalHeaders.Append('ENABLED');
canonicalHeaders.Append(#10);
http.SetRequestHeader('x-amz-checksum-mode','ENABLED');
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('x-amz-content-sha256');
canonicalHeaders.Append('x-amz-content-sha256:');
canonicalHeaders.Append(payloadHash);
canonicalHeaders.Append(#10);
http.SetRequestHeader('x-amz-content-sha256',payloadHash);
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('x-amz-date');
canonicalHeaders.Append('x-amz-date:');
canonicalHeaders.Append(amzDate);
canonicalHeaders.Append(#10);
http.SetRequestHeader('x-amz-date',amzDate);
if (credentials.HasMember('session_token') = 1) then
  begin
    if (signedHeaders.Length > 0) then
      begin
        signedHeaders.Append(';');
      end;
    signedHeaders.Append('x-amz-security-token');
    canonicalHeaders.Append('x-amz-security-token:');
    canonicalHeaders.Append(credentials.StringOf('session_token'));
    canonicalHeaders.Append(#10);
    http.SetRequestHeader('x-amz-security-token',credentials.StringOf('session_token'));
  end;
canonical.Clear();
canonical.Append('HEAD' + #10);
canonical.Append(uri.GetAsString());
canonical.Append(#10);
canonical.Append(query.GetAsString());
canonical.Append(#10);
canonical.Append(canonicalHeaders.GetAsString());
canonical.Append(#10);
canonical.Append(signedHeaders.GetAsString());
canonical.Append(#10);
canonical.Append(payloadHash);
canonicalHash := signer.HashStringENC(canonical.GetAsString());
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not hash canonical request.');
    Exit;
  end;
scope.Clear();
scope.Append(dateStamp);
scope.Append('/');
scope.Append(region);
scope.Append('/s3/aws4_request');
stringToSign.Clear();
stringToSign.Append('AWS4-HMAC-SHA256' + #10);
stringToSign.Append(amzDate);
stringToSign.Append(#10);
stringToSign.Append(scope.GetAsString());
stringToSign.Append(#10);
stringToSign.Append(canonicalHash);
signingKey.SetString('AWS4');
signingKey.Append(credentials.StringOf('secret_key'));
success := signer.SetMacKeyString(signingKey.GetAsString());
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
derivedKey := signer.MacStringENC(dateStamp);
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not derive signing key.');
    Exit;
  end;
success := signer.SetMacKeyEncoded(derivedKey,'hex');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
derivedKey := signer.MacStringENC(region);
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not derive signing key.');
    Exit;
  end;
success := signer.SetMacKeyEncoded(derivedKey,'hex');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
derivedKey := signer.MacStringENC('s3');
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not derive signing key.');
    Exit;
  end;
success := signer.SetMacKeyEncoded(derivedKey,'hex');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
derivedKey := signer.MacStringENC('aws4_request');
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not derive signing key.');
    Exit;
  end;
success := signer.SetMacKeyEncoded(derivedKey,'hex');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
signature := signer.MacStringENC(stringToSign.GetAsString());
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not sign request.');
    Exit;
  end;
authorization.Clear();
authorization.Append('AWS4-HMAC-SHA256 Credential=');
authorization.Append(credentials.StringOf('access_key'));
authorization.Append('/');
authorization.Append(scope.GetAsString());
authorization.Append(', SignedHeaders=');
authorization.Append(signedHeaders.GetAsString());
authorization.Append(', Signature=');
authorization.Append(signature);
http.SetRequestHeader('Authorization',authorization.GetAsString());
url.SetString('https://');
url.Append(host.GetAsString());
url.Append(uri.GetAsString());
if (query.Length > 0) then
  begin
    url.Append('?');
    url.Append(query.GetAsString());
  end;
success := http.HttpNoBody('HEAD',url.GetAsString(),response.ControlInterface);
if ((success <> 1) or (response.StatusCode <> 200)) then
  begin
    Memo1.Lines.Add('Request failed or returned unexpected HTTP status: ' + IntToStr(response.StatusCode));
    //  Preserve progress. Inspect full diagnostics privately; do not log secrets.
    Exit;
  end;
etag := response.GetHeaderField('ETag');
stringCompare.SetString(etag);
stringMatch5 := stringCompare.ContentsEqual('',1);
if ((response.LastMethodSuccess <> 1) or (stringMatch5 = 1)) then
  begin
    Memo1.Lines.Add('Missing required response header ETag.');
    Exit;
  end;
expectedSize := response.GetHeaderField('Content-Length');
stringCompare.SetString(expectedSize);
stringMatch6 := stringCompare.ContentsEqual('',1);
if ((response.LastMethodSuccess <> 1) or (stringMatch6 = 1)) then
  begin
    Memo1.Lines.Add('Missing required response header Content-Length.');
    Exit;
  end;
returnedVersion := response.GetHeaderField('x-amz-version-id');
if (response.LastMethodSuccess <> 1) then
  begin
    returnedVersion := '';
  end;
stringCompare.SetString(versionId);
stringMatch7 := stringCompare.ContentsEqual('',1);
if (stringMatch7 = 1) then
  begin
    versionId := returnedVersion;
  end
else
  begin
    stringCompare.SetString(returnedVersion);
    stringMatch8 := stringCompare.ContentsEqual(versionId,1);
    if (stringMatch8 <> 1) then
      begin
        Memo1.Lines.Add('Unexpected object version.');
        Exit;
      end;
  end;
expectedChecksum := response.GetHeaderField('x-amz-checksum-sha256');
stringCompare.SetString(expectedChecksum);
stringMatch9 := stringCompare.ContentsEqual('',1);
if ((response.LastMethodSuccess <> 1) or (stringMatch9 = 1)) then
  begin
    Memo1.Lines.Add('Missing required response header x-amz-checksum-sha256.');
    Exit;
  end;
checksumType := response.GetHeaderField('x-amz-checksum-type');
stringCompare.SetString(checksumType);
stringMatch10 := stringCompare.ContentsEqual('',1);
if ((response.LastMethodSuccess <> 1) or (stringMatch10 = 1)) then
  begin
    Memo1.Lines.Add('Missing required response header x-amz-checksum-type.');
    Exit;
  end;
stringCompare.SetString(checksumType);
stringMatch11 := stringCompare.ContentsEqual('FULL_OBJECT',1);
if (stringMatch11 <> 1) then
  begin
    Memo1.Lines.Add('This example requires a FULL_OBJECT SHA-256 checksum; COMPOSITE needs separate verification.');
    Exit;
  end;
query.Clear();
stringCompare.SetString(versionId);
stringMatch12 := stringCompare.ContentsEqual('',1);
if (stringMatch12 <> 1) then
  begin
    if (query.Length > 0) then
      begin
        query.Append('&');
      end;
    query.Append('versionId=');
    query.Append(http.UrlEncode(versionId));
  end;
http.ClearHeaders();
success := now.SetFromCurrentSystemTime();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read system time.');
    Exit;
  end;
timestamp.SetString(now.GetAsTimestamp(0));
replaceCount := timestamp.Replace('-','');
replaceCount := timestamp.Replace(':','');
amzDate := timestamp.GetAsString();
dateStamp := timestamp.GetRange(0,8,0);
payloadHash := signer.HashStringENC('');
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not hash payload.');
    Exit;
  end;
canonicalHeaders.Clear();
signedHeaders.Clear();
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('host');
canonicalHeaders.Append('host:');
canonicalHeaders.Append(host.GetAsString());
canonicalHeaders.Append(#10);
http.SetRequestHeader('host',host.GetAsString());
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('if-match');
canonicalHeaders.Append('if-match:');
canonicalHeaders.Append(etag);
canonicalHeaders.Append(#10);
http.SetRequestHeader('if-match',etag);
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('x-amz-checksum-mode');
canonicalHeaders.Append('x-amz-checksum-mode:');
canonicalHeaders.Append('ENABLED');
canonicalHeaders.Append(#10);
http.SetRequestHeader('x-amz-checksum-mode','ENABLED');
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('x-amz-content-sha256');
canonicalHeaders.Append('x-amz-content-sha256:');
canonicalHeaders.Append(payloadHash);
canonicalHeaders.Append(#10);
http.SetRequestHeader('x-amz-content-sha256',payloadHash);
if (signedHeaders.Length > 0) then
  begin
    signedHeaders.Append(';');
  end;
signedHeaders.Append('x-amz-date');
canonicalHeaders.Append('x-amz-date:');
canonicalHeaders.Append(amzDate);
canonicalHeaders.Append(#10);
http.SetRequestHeader('x-amz-date',amzDate);
if (credentials.HasMember('session_token') = 1) then
  begin
    if (signedHeaders.Length > 0) then
      begin
        signedHeaders.Append(';');
      end;
    signedHeaders.Append('x-amz-security-token');
    canonicalHeaders.Append('x-amz-security-token:');
    canonicalHeaders.Append(credentials.StringOf('session_token'));
    canonicalHeaders.Append(#10);
    http.SetRequestHeader('x-amz-security-token',credentials.StringOf('session_token'));
  end;
canonical.Clear();
canonical.Append('GET' + #10);
canonical.Append(uri.GetAsString());
canonical.Append(#10);
canonical.Append(query.GetAsString());
canonical.Append(#10);
canonical.Append(canonicalHeaders.GetAsString());
canonical.Append(#10);
canonical.Append(signedHeaders.GetAsString());
canonical.Append(#10);
canonical.Append(payloadHash);
canonicalHash := signer.HashStringENC(canonical.GetAsString());
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not hash canonical request.');
    Exit;
  end;
scope.Clear();
scope.Append(dateStamp);
scope.Append('/');
scope.Append(region);
scope.Append('/s3/aws4_request');
stringToSign.Clear();
stringToSign.Append('AWS4-HMAC-SHA256' + #10);
stringToSign.Append(amzDate);
stringToSign.Append(#10);
stringToSign.Append(scope.GetAsString());
stringToSign.Append(#10);
stringToSign.Append(canonicalHash);
signingKey.SetString('AWS4');
signingKey.Append(credentials.StringOf('secret_key'));
success := signer.SetMacKeyString(signingKey.GetAsString());
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
derivedKey := signer.MacStringENC(dateStamp);
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not derive signing key.');
    Exit;
  end;
success := signer.SetMacKeyEncoded(derivedKey,'hex');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
derivedKey := signer.MacStringENC(region);
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not derive signing key.');
    Exit;
  end;
success := signer.SetMacKeyEncoded(derivedKey,'hex');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
derivedKey := signer.MacStringENC('s3');
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not derive signing key.');
    Exit;
  end;
success := signer.SetMacKeyEncoded(derivedKey,'hex');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
derivedKey := signer.MacStringENC('aws4_request');
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not derive signing key.');
    Exit;
  end;
success := signer.SetMacKeyEncoded(derivedKey,'hex');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set signing key.');
    Exit;
  end;
signature := signer.MacStringENC(stringToSign.GetAsString());
if (signer.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not sign request.');
    Exit;
  end;
authorization.Clear();
authorization.Append('AWS4-HMAC-SHA256 Credential=');
authorization.Append(credentials.StringOf('access_key'));
authorization.Append('/');
authorization.Append(scope.GetAsString());
authorization.Append(', SignedHeaders=');
authorization.Append(signedHeaders.GetAsString());
authorization.Append(', Signature=');
authorization.Append(signature);
http.SetRequestHeader('Authorization',authorization.GetAsString());
url.SetString('https://');
url.Append(host.GetAsString());
url.Append(uri.GetAsString());
if (query.Length > 0) then
  begin
    url.Append('?');
    url.Append(query.GetAsString());
  end;
success := http.Download(url.GetAsString(),partialPath);
if ((success <> 1) or (http.LastStatus <> 200)) then
  begin
    Memo1.Lines.Add('Request failed or returned unexpected HTTP status: ' + IntToStr(http.LastStatus));
    //  Preserve progress. Inspect full diagnostics privately; do not log secrets.
    Exit;
  end;
received := TChilkatMime.Create(Self);
success := received.LoadMime(http.LastResponseHeader);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not parse download headers.');
    Exit;
  end;

receivedSize := received.GetHeaderField('Content-Length');
stringCompare.SetString(receivedSize);
stringMatch13 := stringCompare.ContentsEqual('',1);
if ((received.LastMethodSuccess <> 1) or (stringMatch13 = 1)) then
  begin
    Memo1.Lines.Add('Missing download header Content-Length.');
    Exit;
  end;
receivedHash := received.GetHeaderField('x-amz-checksum-sha256');
stringCompare.SetString(receivedHash);
stringMatch14 := stringCompare.ContentsEqual('',1);
if ((received.LastMethodSuccess <> 1) or (stringMatch14 = 1)) then
  begin
    Memo1.Lines.Add('Missing download header x-amz-checksum-sha256.');
    Exit;
  end;
receivedType := received.GetHeaderField('x-amz-checksum-type');
stringCompare.SetString(receivedType);
stringMatch15 := stringCompare.ContentsEqual('',1);
if ((received.LastMethodSuccess <> 1) or (stringMatch15 = 1)) then
  begin
    Memo1.Lines.Add('Missing download header x-amz-checksum-type.');
    Exit;
  end;
stringCompare.SetString(receivedSize);
stringMatch16 := stringCompare.ContentsEqual(expectedSize,1);
stringCompare.SetString(receivedHash);
stringMatch17 := stringCompare.ContentsEqual(expectedChecksum,1);
stringCompare.SetString(receivedType);
stringMatch18 := stringCompare.ContentsEqual('FULL_OBJECT',1);
if ((stringMatch16 <> 1) or (stringMatch17 <> 1) or (stringMatch18 <> 1)) then
  begin
    Memo1.Lines.Add('Download metadata changed; preserve the temporary file.');
    Exit;
  end;
actualSize := files.FileSizeStr(partialPath);
stringCompare.SetString(actualSize);
stringMatch19 := stringCompare.ContentsEqual(expectedSize,1);
if ((files.LastMethodSuccess <> 1) or (stringMatch19 <> 1)) then
  begin
    Memo1.Lines.Add('Downloaded size mismatch.');
    Exit;
  end;
digest := TChilkatCrypt2.Create(Self);
digest.HashAlgorithm := 'sha256';
digest.EncodingMode := 'base64';
actualChecksum := digest.HashFileENC(partialPath);
stringCompare.SetString(actualChecksum);
stringMatch20 := stringCompare.ContentsEqual(expectedChecksum,1);
if ((digest.LastMethodSuccess <> 1) or (stringMatch20 <> 1)) then
  begin
    Memo1.Lines.Add('Downloaded checksum mismatch.');
    Exit;
  end;
success := files.FileRename(partialPath,finalPath);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Verified download remains at the temporary path.');
    Exit;
  end;
Memo1.Lines.Add('Full-object SHA-256 download verified.');