This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v19.8
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, 3 = SHA-256 digest, 4 = SHA-512 digest
Operator in Encode digest in Base64URL
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.

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 Value Comment
4D digest 2

Internal algorithm of 4D. Used by 4D to encrypt user passwords. This algorithm is particularly useful in the context of the On REST Authentication database method when you want to use your own list of users.

MD5 digest 0 Message Digest 5 algorithm. A series of 128 bits returned as a string of 32 hexadecimal characters.
SHA1 digest 1 Secure Hash 1 algorithm. A series of 160 bits returned as a string of 40 hexadecimal characters.
SHA256 digest 3 (SHA-2 family) SHA-256 is a series of 256 bits returned as a string of 64 hexadecimal characters.
SHA512 digest 4 (SHA-2 family) SHA-512 is a series of 512 bits returned as a string of 128 hexadecimal characters.

Note: It is not recommended to use MD5 and SHA algorithms to handle passwords; if you need to check passwords, you are advised to use Generate password hash and Verify password hash commands.

By default if the * parameter is omitted, the returned digest is encoded in hexadecimal. Pass the * parameter if you want it to be encoded in Base64URL. 

The value returned for the same object is the same on all the platforms (macOS/Windows). 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 a string value (for example "d41d8cd98f00b204e9800998ecf8427e" for MD5).

This example compares two images using the MD5 algorithm:

 C_PICTURE($vPict1;$vPict2)
 C_BLOB($FirstBlob;$SecondBlob)
 READ PICTURE FILE("c:\\myPhotos\\photo1.png")
 If(OK=1)
    READ PICTURE FILE("c:\\myPhotos\\photo2.png")
    If(OK=1)
       PICTURE TO BLOB($vPict1;$FirstBlob;".png")
       PICTURE TO BLOB($vPict2;$SecondBlob;".png")
 
       $MD5_1:=Generate digest($FirstBlob;MD5 digest)
       $MD5_2:=Generate digest($SecondBlob;MD5 digest)
 
       If($MD5_1#$MD5_2)
          ALERT("These two images are different.")
       Else
          ALERT("These two images are identical.")
       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



See also 

BASE64 DECODE
BASE64 ENCODE
Generate password hash
Secured Protocol
WEB Validate digest

 
PROPERTIES 

Product: 4D
Theme: Tools
Number: 1147

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v13
Modified: 4D v14
Modified: 4D v16 R5
Modified: 4D v18 R4

 
ARTICLE USAGE

4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)