# Generate a Random string out of characters provided # Version 20141208 Thomas Stokkeland (c) License GNU GPL v2 # # Example: # !insertmacro GenerateRandomString "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST1234567890" 15 # pop $0 # Result is put on the stack # # Requirements: # ${rnd} - from http://nsis.sourceforge.net/Rnd # !Macro GenerateRandomString VALID_CHARS STRING_LENGTH # Preserve vars push $5 push $6 push $7 push $8 push $9 # DetailPrint "GenerateRandomString: ${VALID_CHARS} : ${STRING_LENGTH}" StrCpy $5 "" GenerateRandomStringLoop: StrLen $8 ${VALID_CHARS} # Length of available chars IntOp $8 $8 - 1 # reduce by one to work as index for strcpy ${rnd} $9 0 $8 # Get a random number in range StrCpy $6 ${VALID_CHARS} 1 $9 # getting the next character StrCpy $5 $5$6 # adding to result string StrLen $8 $5 # Check if we have enough IntCmpU $8 ${STRING_LENGTH} 0 GenerateRandomStringLoop 0 # restore vars and put our result on the stack pop $9 pop $8 pop $7 pop $6 push $5 exch pop $5 !MacroEnd