KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Determine two files are the same using MD5 digest key
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Win
Published On: April 25, 2014

A way to determine two files' data integrity for similarity would be to use MD5. MD5 is a message-digest algorithm that uses cryptographic function which produces a 128-bit (16-byte) hash value. 4D has a command Generate Digest to generate an MD5 hash key. The method below is an example of checking two files and returning a boolean as a result:

// ----------------------------------------------------------
// Name: md5_file_verify
// Description: Determines two files are identical using the
// MD5 digest.
//
// Input:
// $1 (text) - Path of the first file
// $2 (text) - Path of the second file
//
// Output:
// $0 (boolean)- true (files are the same) or false (files are not the same)
// ---------------------------------------------------------------------------
C_BOOLEAN($0)
C_TEXT($1;$2;$file1_path_t;$file2_path_t)
C_TEXT($md5_result1;$md5_result2)
C_BLOB($file_blob1;$file_blob2)

If (Count parameters>=2)
   $file1_path_t:=$1
   $file2_path_t:=$2

   If (Test path name($file1_path_t)=Is a document)& \
      (Test path name($file2_path_t)=Is a document)

      DOCUMENT TO BLOB($file1_path_t;$file_blob1)
      DOCUMENT TO BLOB($file2_path_t;$file_blob2)

      $md5_result1:=Generate digest($file_blob1;MD5 digest) // create digest key
      $md5_result2:=Generate digest($file_blob2;MD5 digest) // create digest key

      $0:=($md5_result1=$md5_result2) // Compare if they are the same
   End if
End if


See Also: