KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Compare the Digest of Two Inputs
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: May 6, 2024

Below is a utility method to compare the digest of any two values or variables, which can be used to perform a deep comparison:

// Method: compareDigest
//
// Parameters:
// -$inA_v : First Variant to compare
// -$inB_v : Second Variant to compare
// -$digest_l : OPTIONAL, Specific Digest to use, defaults to MD5
//       Check Generate digest command for digest options
//
// -$out_b : Returns a boolean True if first and second inputs match and False if not
//
//======================================================================================

#DECLARE($inA_v; $inB_v : Variant; $digest_l : Integer) : Boolean

var $blobA; $blobB : Blob
VARIABLE TO BLOB($inA_v; $blobA)
VARIABLE TO BLOB($inB_v; $blobB)

return /*IF*/ BLOB size($blobA)=BLOB size($blobB) ? \
   /*True :*/Generate digest($blobA; $digest_l)=Generate digest($blobB; $digest_l) : \
   /*False :*/False


Below is a version for versions of 4D that do not support the ternary operator:
// Method: compareDigest
//
// Parameters:
// -$inA_v : First Variant to compare
// -$inB_v : Second Variant to compare
// -$digest_l : OPTIONAL, Specific Digest to use, defaults to MD5
// Check Generate digest command for digest options
//
// -$out_b : Returns a boolean True if first and second inputs match and False if not
//
//======================================================================================

#DECLARE($inA_v; $inB_v : Variant; $digest_l : Integer) : Boolean

var $blobA; $blobB : Blob
VARIABLE TO BLOB($inA_v; $blobA)
VARIABLE TO BLOB($inB_v; $blobB)

If (BLOB size($blobA)=BLOB size($blobB))
   return Generate digest($blobA; $digest_l)=Generate digest($blobB; $digest_l)
Else
   return False
End if