DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoCrypt
Variant vBd
Handle hoBd
String s
Integer i
String sEncodedHash
Move False To iSuccess
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Set ComHashAlgorithm Of hoCrypt To "sha256"
Set ComCharset Of hoCrypt To "utf-8"
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Move "The quick brown fox jumped over the lazy dog." + (character(13)) + (character(10)) To s
// 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.
Get ComAppendString Of hoBd s "utf-8" To iSuccess
Get ComHashBeginString Of hoCrypt s To iSuccess
Move 0 To i
While (i < 200)
Get ComAppendString Of hoBd s "utf-8" To iSuccess
Get ComHashMoreString Of hoCrypt s To iSuccess
Move (i + 1) To i
Loop
// Get the hash in base64 format.
Set ComEncodingMode Of hoCrypt To "base64"
Get ComHashFinalENC Of hoCrypt To sEncodedHash
Showln "Hash computed in chunks: " sEncodedHash
// Let's alternatively compute the hash of the entire amount of data at once,
// to show the hash computation is the same:
Get pvComObject of hoBd to vBd
Get ComHashBdENC Of hoCrypt vBd To sEncodedHash
Showln "Hash computed in 1 step: " sEncodedHash
// Output:
// Hash computed in chunks: unwkVff61k40roRIJizaknreScHaL6frWe37kydXbZQ=
// Hash computed in 1 step: unwkVff61k40roRIJizaknreScHaL6frWe37kydXbZQ=
End_Procedure