Sample code for 30+ languages & platforms
Xbase++

Example: Hash Text in Chunks

Shows how to generate a final hash, like SHA-256, for a large text by processing it in chunks.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCrypt
LOCAL oBd
LOCAL s
LOCAL i
LOCAL cEncodedHash

nSuccess := 0

oCrypt := CreateObject("Chilkat.Crypt2")
oCrypt:HashAlgorithm := "sha256"
oCrypt:Charset := "utf-8"

oBd := CreateObject("Chilkat.BinData")

s := "The quick brown fox jumped over the lazy dog." + Chr(13) + Chr(10)

//  Accumulate the text in a StringBuilder.  We'll demonstrate hashing the text in chunks,
//  and then also hashing the entire text at once to show the results are the same.

oBd:AppendString(s, "utf-8")
nSuccess := oCrypt:HashBeginString(s)

i := 0
DO WHILE i < 200
    oBd:AppendString(s, "utf-8")
    oCrypt:HashMoreString(s)
    i := i + 1
ENDDO

//  Get the hash in base64 format.
oCrypt:EncodingMode := "base64"

cEncodedHash := oCrypt:HashFinalENC()
? "Hash computed in chunks: " + cEncodedHash

//  Let's alternatively compute the hash of the entire amount of data at once,
//  to show the hash computation is the same:
cEncodedHash := oCrypt:HashBdENC(oBd)
? "Hash computed in 1 step: " + cEncodedHash

//  Output:

//  Hash computed in chunks: unwkVff61k40roRIJizaknreScHaL6frWe37kydXbZQ=
//  Hash computed in 1 step: unwkVff61k40roRIJizaknreScHaL6frWe37kydXbZQ=

oCrypt:destroy()
oBd:destroy()