Code: Select all
;==============================================================
; Simple Password Generator using Regular Expressions
; with StringGadget() Show/Hide Password Button
;
; tested & working with PureBasic v5.70 LTS beta 4 (x64)
; running on Windows 10 and MacOS High Sierra
;
; by TI-994A - free to use, improve, share...
;
; 25th February 2019
;==============================================================
Enumeration
#mainWin
#pwString
#pwLength
#pwLengthLabel
#pwShowHide
#pwGenFrame
#pwGenerate
#alphaCheck
#alphaCapsCheck
#numericsCheck
#specialCharsCheck
EndEnumeration
Procedure.s passwordGenerator()
passwordLength = Val(GetGadgetText(#pwLength))
If passwordLength < 8 Or passwordLength > 20
If passwordLength < 8
passwordLength = 8
Else
passwordLength = 20
EndIf
SetGadgetText(#pwLength, Str(passwordLength))
EndIf
If GetGadgetState(#alphaCheck)
chrExp$ + "a-z"
regExp$ + "(?=.*[a-z])"
EndIf
If GetGadgetState(#alphaCapsCheck)
chrExp$ + "A-Z"
regExp$ + "(?=.*[A-Z])"
EndIf
If GetGadgetState(#numericsCheck)
chrExp$ + "0-9"
regExp$ + "(?=.*[0-9])"
EndIf
If GetGadgetState(#specialCharsCheck)
chrExp$ + "\~\!\@\#\$\%\&\*\-\_\=\+\?"
regExp$ + "(?=.*[~!@#$%&*-_=+\?])"
EndIf
chrExp$ = "[" + chrExp$ + "]"
regExp$ = "^" + regExp$ + ".*$"
If CreateRegularExpression(0, regExp$)
If CreateRegularExpression(1, chrExp$)
While Not MatchRegularExpression(0, password$)
password$ = ""
For i = 1 To passwordLength
While Not MatchRegularExpression(1, Chr(seed))
seed = Random(126, 32)
Wend
password$ + Chr(seed)
seed = 0
Next i
Wend
FreeRegularExpression(0)
FreeRegularExpression(1)
EndIf
EndIf
ProcedureReturn password$
EndProcedure
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#mainWin, 0, 0, 350, 330, "Simple Password Generator", wFlags)
StringGadget(#pwString, 20, 20, 230, 30, "", #PB_String_Password)
ButtonGadget(#pwShowHide, 250, 20, 80, 30, "show/hide", #PB_Button_Toggle)
FrameGadget(#pwGenFrame, 20, 70, 310, 240, "Password Generator")
StringGadget(#pwLength, 40, 100, 30, 20, "8", #PB_String_Numeric)
TextGadget(#pwLengthLabel, 80, 100, 250, 30, "Password Length")
CheckBoxGadget(#alphaCheck, 40, 130, 250, 20, " Alphabets (a to z)")
CheckBoxGadget(#alphaCapsCheck, 40, 160, 250, 20, " Capital Alphabets (A to Z)")
CheckBoxGadget(#numericsCheck, 40, 190, 250, 20, " Numericals (0 - 9)")
CheckBoxGadget(#specialCharsCheck, 40, 220, 250, 20, " Special Characters (~!@#$%&*-_=+?)")
ButtonGadget(#pwGenerate, 40, 260, 270, 30, "GENERATE PASSWORD")
SetActiveGadget(1)
SetGadgetState(#alphaCheck, #PB_Checkbox_Checked)
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case #pwShowHide, #pwGenerate
passwordShow = 0
If EventGadget() = #pwGenerate
SetGadgetState(#pwShowHide, 1)
password$ = passwordGenerator()
Else
password$ = GetGadgetText(#pwString)
If GetGadgetState(#pwShowHide) = 0
passwordShow = #PB_String_Password
EndIf
EndIf
StringGadget(#pwString, 20, 20, 230, 30, password$, passwordShow)
EndSelect
EndSelect
Until appQuit