Sample code for 30+ languages & platforms
PowerBuilder

Dropbox Content Hash

See more Dropbox Examples

Demonstrates how to compute the Dropbox content_hash of a file. (This is the "content_hash" found in the Dropbox FileMetadata object.

Note: This example requires Chilkat v9.5.0.79 or greater because it uses the fac.ReadBlockBd method.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Crypt
oleobject loo_Fac
oleobject loo_Bd
oleobject loo_BdHashes
integer li_BlockSize
integer li_NumBlocks
integer i
string ls_HashHex
string ls_Content_hash

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// To calculate the content_hash of a file:
// Split the file into blocks of 4 MB (4,194,304 or 4 * 1024 * 1024 bytes). The last block (if any) may be smaller than 4 MB.
// Compute the hash of each block using SHA-256.
// Concatenate the hash of all blocks in the binary format to form a single binary string.
// Compute the hash of the concatenated string using SHA-256. Output the resulting hash in hexadecimal format.

loo_Crypt.HashAlgorithm = "sha256"
loo_Crypt.EncodingMode = "hex_lower"

// We're going to calculate the content_hash for the Milky Way JPG image linked here:  https://www.dropbox.com/developers/reference/content-hash

loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

loo_BdHashes = create oleobject
li_rc = loo_BdHashes.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Fac.OpenForRead("qa_data/jpg/milky-way-nasa.jpg")
if li_Success = 0 then
    Write-Debug loo_Fac.LastErrorText
    destroy loo_Crypt
    destroy loo_Fac
    destroy loo_Bd
    destroy loo_BdHashes
    return
end if

li_BlockSize = 4194304
li_NumBlocks = loo_Fac.GetNumBlocks(li_BlockSize)

i = 0
do while (i < li_NumBlocks)
    // Read the next 4MB block into bd.
    loo_Bd.Clear()
    li_Success = loo_Fac.ReadBlockBd(i,li_BlockSize,loo_Bd)

    ls_HashHex = loo_Crypt.HashBdENC(loo_Bd)
    Write-Debug string(i) + ": " + ls_HashHex
    loo_BdHashes.AppendEncoded(ls_HashHex,"hex_lower")

    i = i + 1
loop

loo_Fac.FileClose()

// Hash the concatenated SHA-256 hashes.
ls_Content_hash = loo_Crypt.HashBdENC(loo_BdHashes)
Write-Debug "content_hash = " + ls_Content_hash


destroy loo_Crypt
destroy loo_Fac
destroy loo_Bd
destroy loo_BdHashes