cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
crarun9111
Level 3

How to create a random password using install script

Hello All,

I am new to install shield so apologies in advance if my question is vague or repetitive.

i want to create a random password using install script, is there any function or code snippet which anyone can help me with.

Thanks in advance for your replies.

Regards,
CR:)
Labels (1)
0 Kudos
(2) Replies
Cary_R
Level 11

This gets asked periodically.

You'll basically have to do it in an external *.dll of some sort, or prototype the rand/srand functions in msvcrt.dll and call it from InstallScript

check out:

https://stackoverflow.com/questions/45296986/random-password-creation-using-install-shield-install-script

http://www.cplusplus.com/reference/cstdlib/rand/

You'll basically generate an integer between 0 and 93, add 33 and treat it as a Char. Repeat the process X times for however long a password you want, appending char by char to a String variable.

0 Kudos
ch_eng
Level 7

CR,

Cary is correct - it cannot be done solely in InstallScript since there is no Random function, but it can be done with InstallScript and a batch file if you don't want to go the external DLL route.

Here is an example. You will need to put the attached .bat file in the Support Files for your project:

@echo OFF
setlocal EnableDelayedExpansion
cls
:: %1 should be # of loops to do
:: %2 should be temp filename to store output
:: code based off of:
:: https://stackoverflow.com/questions/5777400/how-to-use-random-in-batch-script
:: 33 and 126 are hardcoded due to the desired ASCII range; can be changed
:: http://www.asciitable.com/
:: delete the output file if it already exists; ignore errors
del %2 >NUL 2>&1
:: start off with a "new" random
echo !RANDOM!
:: loop for the desired number of times
for /L %%a in (1 1 %1) do (
call:rand 33 126
echo !RAND_NUM!>>%2
)
::pause
exit /b

:: rand()
:: %1 is min, %2 is max.
:: RAND_NUM is set to a random number from min through max.
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
exit /b


and include this file (or add these functions to an existing file in your project)
[CODE]//===========================================================================
//
// File Name: Random.rul
//
// Description: utility functions for generating random string of characters.
// requires external .bat file to actually generate the random numbers.
//===========================================================================

// main function
prototype STRING GetRandomCharacters( byval NUMBER ); // nNumChars
// helper function
prototype STRING Chr( byval STRING ); // szASCII


//---------------------------------------------------------------------------
//get specified number of random characters from subset of ASCII table
function STRING GetRandomCharacters( nNumChars )
STRING szNumChars, szBatFile, szTempFile, szLine, szRandomString, svError;
NUMBER nFileHandle, nFileOpen, nResult;
begin

szBatFile = "random.bat"; // file assumed to be in this project's Support Files
szTempFile = "rnd.txt";

NumToStr( szNumChars, nNumChars );

// "/k" is for debugging
//LaunchAppAndWait( WINSYSDIR ^ "cmd.exe", "/k \"" + SUPPORTDIR ^ szBatFile + "\" " + szNumChars + " " + FOLDER_TEMP ^ szTempFile, WAIT );
LaunchAppAndWait( WINSYSDIR ^ "cmd.exe", "/c \"" + SUPPORTDIR ^ szBatFile + "\" " + szNumChars + " " + FOLDER_TEMP ^ szTempFile, WAIT | LAAW_OPTION_HIDDEN );

szRandomString = "";

// make sure the file exists before trying file operations
if Is( FILE_EXISTS, FOLDER_TEMP ^ szTempFile ) then

OpenFileMode( FILE_MODE_NORMAL ); // open file as read-only
nFileOpen = OpenFile( nFileHandle, FOLDER_TEMP, szTempFile );

if nFileOpen = 0 then

nResult = GetLine( nFileHandle, szLine );
while ( nResult = 0 )
szRandomString = szRandomString + Chr( szLine ); //get ASCII char equivalent of the random #
// get next line of the file:
nResult = GetLine( nFileHandle, szLine );
endwhile;

CloseFile( nFileHandle );
else
svError = "Error: unable to open \"" + FOLDER_TEMP ^ szTempFile + "\"";
MessageBox( svError, INFORMATION ); // debug
endif;

else
svError = "Error: \"" + FOLDER_TEMP ^ szTempFile + "\" not found!";
MessageBox( svError, INFORMATION ); // debug
endif;

try
DeleteFile( FOLDER_TEMP ^ szTempFile ); // cleanup: delete rnd.txt
catch
endcatch;

return szRandomString;

end;

//---------------------------------------------------------------------------
//convert a decimal number as string) representing an ASCII character to that character
//mimics the "Chr" function in VB (for a limited subset of the ASCII table)
function STRING Chr( szASCII )
STRING svASCIIval;
begin

switch( szASCII )
case "32":
svASCIIval = " ";
case "33":
svASCIIval = "!";
case "34":
svASCIIval = "\""; //note": double quote character has to be escaped
case "35":
svASCIIval = "#";
case "36":
svASCIIval = "$";
case "37":
svASCIIval = "%";
case "38":
svASCIIval = "&";
case "39":
svASCIIval = "'";
case "40":
svASCIIval = "(";
case "41":
svASCIIval = ")";
case "42":
svASCIIval = "*";
case "43":
svASCIIval = "+";
case "44":
svASCIIval = ",";
case "45":
svASCIIval = "-";
case "46":
svASCIIval = ".";
case "47":
svASCIIval = "/";
case "48":
svASCIIval = "0";
case "49":
svASCIIval = "1";
case "50":
svASCIIval = "2";
case "51":
svASCIIval = "3";
case "52":
svASCIIval = "4";
case "53":
svASCIIval = "5";
case "54":
svASCIIval = "6";
case "55":
svASCIIval = "7";
case "56":
svASCIIval = "8";
case "57":
svASCIIval = "9";
case "58":
svASCIIval = ":";
case "59":
svASCIIval = ";";
case "60":
svASCIIval = "<";
case "61":
svASCIIval = "=";
case "62":
svASCIIval = ">";
case "63":
svASCIIval = "?";
case "64":
svASCIIval = "@";
case "65":
svASCIIval = "A";
case "66":
svASCIIval = "B";
case "67":
svASCIIval = "C";
case "68":
svASCIIval = "D";
case "69":
svASCIIval = "E";
case "70":
svASCIIval = "F";
case "71":
svASCIIval = "G";
case "72":
svASCIIval = "H";
case "73":
svASCIIval = "I";
case "74":
svASCIIval = "J";
case "75":
svASCIIval = "K";
case "76":
svASCIIval = "L";
case "77":
svASCIIval = "M";
case "78":
svASCIIval = "N";
case "79":
svASCIIval = "O";
case "80":
svASCIIval = "P";
case "81":
svASCIIval = "Q";
case "82":
svASCIIval = "R";
case "83":
svASCIIval = "S";
case "84":
svASCIIval = "T";
case "85":
svASCIIval = "U";
case "86":
svASCIIval = "V";
case "87":
svASCIIval = "W";
case "88":
svASCIIval = "X";
case "89":
svASCIIval = "Y";
case "90":
svASCIIval = "Z";
case "91":
svASCIIval = "[";
case "92":
svASCIIval = "\\"; //note": backslash has to be escaped
case "93":
svASCIIval = "]";
case "94":
svASCIIval = "^";
case "95":
svASCIIval = "_";
case "96":
svASCIIval = "`";
case "97":
svASCIIval = "a";
case "98":
svASCIIval = "b";
case "99":
svASCIIval = "c";
case "100":
svASCIIval = "d";
case "101":
svASCIIval = "e";
case "102":
svASCIIval = "f";
case "103":
svASCIIval = "g";
case "104":
svASCIIval = "h";
case "105":
svASCIIval = "i";
case "106":
svASCIIval = "j";
case "107":
svASCIIval = "k";
case "108":
svASCIIval = "l";
case "109":
svASCIIval = "m";
case "110":
svASCIIval = "n";
case "111":
svASCIIval = "o";
case "112":
svASCIIval = "p";
case "113":
svASCIIval = "q";
case "114":
svASCIIval = "r";
case "115":
svASCIIval = "s";
case "116":
svASCIIval = "t";
case "117":
svASCIIval = "u";
case "118":
svASCIIval = "v";
case "119":
svASCIIval = "w";
case "120":
svASCIIval = "x";
case "121":
svASCIIval = "y";
case "122":
svASCIIval = "z";
case "123":
svASCIIval = "{";
case "124":
svASCIIval = "|";
case "125":
svASCIIval = "}";
case "126":
svASCIIval = "~";
default:
//not sure what the default value should be
svASCIIval = "";
endswitch;

return svASCIIval;
end;[/CODE]

then call the GetRandomCharacters function like this:

szRandomString = GetRandomCharacters( nNumChars );
MessageBox( szRandomString, INFORMATION ); //debug


HTH
0 Kudos