4D v13.4

Generate digest

Home

 
4D v13.4
Generate digest

Generate digest 


 

Generate digest ( param ; algorithm ) -> Function result 
Parameter Type   Description
param  BLOB, Text variable in Blob or text for which to get digest key
algorithm  Longint in Algorithm used to return key:
0 = MD5 Digest, 1 = SHA1 Digest
Function result  Text in Value of digest key

The Generate digest command returns the digest key of a BLOB or text after application of an encryption algorithm.

In 4D, two such algorithms are available: MD5 (Message Digest 5) and SHA-1 (Secure Hash 1). These algorithms are different hash functions:

  • MD5 is a series of 16 bytes returned as a string of 32 hexadecimal characters.
  • SHA-1 is a series of 20 bytes returned as a string of 40 hexadecimal characters.

The value returned for the same object is the same on all the platforms (Mac/Windows, 32 or 64 bits). The calculation is performed based on the representation in UTF-8 of the text passed in the parameter, regardless of the database operating mode (Unicode or ASCII compatibility). 

Note: If you use the command with an empty text/BLOB, it does not return void but returns the following value: "d41d8cd98f00b204e9800998ecf8427e" (MD5) or "da39a3ee5e6b4b0d3255bfef95601890afd80709" (SHA-1).

Pass a Text or BLOB field or variable in the param parameter. The Generate digest function returns the digest key as a string.

In the algorithm parameter, pass a value designating which hash function to use. Use one of the following constants, found in the Digest Type theme:

Constant Type Value Comment
MD5 Digest Longint 0 Use the MD5 algorithm
SHA1 Digest Longint 1 Use the SHA-1algorithm

If the calculation of the digest key is not performed correctly, the function generates an error that you can intercept using the ON ERR CALL command and the function returns an empty string.

This example compares two documents using the MD5 algorithm:

 PLATFORM PROPERTIES($Platf;$Syst;$vlMachine)
  // Open the first document as read-only
 $Same:=True
 $vhDocRef1:=Open document("";"*";Read Mode))
 If(OK=1) // If a document is selected
    DOCUMENT TO BLOB(Document;$FirstBlob// Load document
    If(OK=1)
       If($Platf=Mac OS)
          DOCUMENT TO BLOB(Document;$FirstBlobRF;*)
  // Under Mac OS, load resource fork
          $MD5_1RF:=Generate digest($FirstBlobRF;MD5 Digest)
       End if
 
  // Open the second document as read-only
       $vhDocRef2:=Open document("";"*";Read Mode))
       If(OK=1)
          DOCUMENT TO BLOB(Document;$SecondBlob)
          If(OK=1)
             If($Platf=Mac OS)
                DOCUMENT TO BLOB(Document;$SecondBlobRF;*)
                $MD5_2RF:=Generate digest($SecondBlobRF;MD5 Digest)
                If($MD5_1RF#$MD5_2RF// Compare digests
                   $Same:=False
                End if
             End if
             $MD5_1:=Generate digest($FirstBlob;MD5 Digest)
             $MD5_2:=Generate digest($SecondBlob;MD5 Digest)
             If(($MD5_1#$MD5_2)|($Same=False))
                ALERT("These two documents are different.")
             End if
          End if
       End if
    End if
 End if

These examples illustrate how to retrieve the digest key of a text:

 $key1:=Generate digest("The quick brown fox jumps over the lazy dog.";MD5 Digest)
  // $key1 is "e4d909c290d0fb1ca068ffaddf22cbd0"
 $key2:=Generate digest("The quick brown fox jumps over the lazy dog.";SHA1 Digest)
  // $key2 is "408d94384216f890ff7a0c3528e8bed1e0b01621"

 
PROPERTIES 

Product: 4D
Theme: Tools
Number: 1147

 
INDEX

Alphabetical list of commands

 
HISTORY 

New
Created: 4D v13