4D v16.3

Generate digest

Home

 
4D v16.3
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, 2 = 4D 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, the following algorithms are available: MD5 (Message Digest 5), SHA-1 (Secure Hash 1) and 4D (internal algorithm). 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.
  • 4D designates the internal algorithm used by 4D to encrypt user passwords. This algorithm is particularly useful in the context of the On 4D Mobile Authentication database method when you want to use your own list of users.

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. 

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 theme:

Constant Type Value Comment
4D digest Longint 2 Internal algorithm of 4D. Used by 4D to encrypt user passwords. This algorithm is particularly useful in the context of the On 4D Mobile Authentication database method when you want to use your own list of users.
MD5 digest Longint 0 Message Digest 5 algorithm. A series of 128 bits returned as a string of 32 hexadecimal characters.
SHA1 digest Longint 1 Secure Hash 1 algorithm. A series of 160 bits returned as a string of 40 hexadecimal characters.

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"

This example only accepts the "admin" user with the password "123" that does not match a 4D user:

  //On REST Authentication database method
 C_TEXT($1;$2)
 C_BOOLEAN($0;$3)
  //$1: user
  //$2: password
  //$3: digest mode
 If($1="admin")
    If($3)
       $0:=($2=Generate digest("123";4D digest))
    Else
       $0:=($2="123")
    End if
 Else
    $0:=False
 End if

 
PROPERTIES 

Product: 4D
Theme: Tools
Number: 1147

This command can be run in preemptive processes

 
HISTORY 

Created: 4D v13
Modified: 4D v14

 
ARTICLE USAGE

4D Language Reference ( 4D v16)
4D Language Reference ( 4D v16.1)
4D Language Reference ( 4D v16.2)
4D Language Reference ( 4D v16.3)