KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Generating random passwords or string values in 4D
PRODUCT: 4D | VERSION: 11.6 | PLATFORM: Mac & Win
Published On: April 9, 2010

The following method can be used to generate a random string value that can be used for a password:

  `  ----------------------------------------------------
  `  Method: GenPassword
  `  
  `  Description: Generate a random password or string   
  `                value and return it in $0
  `  
  `  Optional Parameters: 1
  `      $1=Password Length(default is 8 if omitted)
  `  
  `  ----------------------------------------------------


C_LONGINT($passwordLength;$a;$b;$c;$d)
ARRAY TEXT($AllowedChars_at;0)
C_TEXT($password)
$password:=""

If (Count parameters=1)
  C_LONGINT($1)
  $passwordLength:=$1
Else
  $passwordLength:=8
End if
For ($a;65;90)
  `  Add capitol letters A->Z to the array
  APPEND TO ARRAY($AllowedChars_at;Char($a))
End for
For ($b;97;122)
  `  Add lowercase letters a->z to the array
  APPEND TO ARRAY($AllowedChars_at;Char($b))
End for
For ($c;0;9)
  `  Add numbers 0->9 to the array
  APPEND TO ARRAY($AllowedChars_at;String($c))
End for


  `  The following are non alpha-numeric characters
  `  that you may also want to include

APPEND TO ARRAY($AllowedChars_at;"!")
APPEND TO ARRAY($AllowedChars_at;"@")
APPEND TO ARRAY($AllowedChars_at;"#")
APPEND TO ARRAY($AllowedChars_at;"$")
APPEND TO ARRAY($AllowedChars_at;"%")
APPEND TO ARRAY($AllowedChars_at;"^")
APPEND TO ARRAY($AllowedChars_at;"&")
APPEND TO ARRAY($AllowedChars_at;"*")
APPEND TO ARRAY($AllowedChars_at;"(")
APPEND TO ARRAY($AllowedChars_at;")")
APPEND TO ARRAY($AllowedChars_at;"_")
APPEND TO ARRAY($AllowedChars_at;"+")
APPEND TO ARRAY($AllowedChars_at;"-")


If ($passwordLength>0)
  For ($d;1;$passwordLength)
    $password:=$password+$AllowedChars_at{Random%(Size of array($AllowedChars_at))+1}
  End for
  $0:=$password
Else
  $0:=""
End if


Here is an example of the usage for the above method:

C_TEXT($myPassword)
$myPassword:=GenPassword(13)

The above example will generate a random password 13 characters long and store it in the variable $myPassword.

This approach works for 4D 2004 as well as 4D v11 SQL.