Password Generator & StringGadget() Show/Hide Password Btn

Share your advanced PureBasic knowledge/code with the community.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Password Generator & StringGadget() Show/Hide Password Btn

Post by TI-994A »

A simple routine to conditionally generate random passwords using regular expressions (RegEx), and demonstrate the implementation of a show/hide button for password string fields. Tested on Windows and MacOS, and it should also work on Linux.

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
As always, feedback and suggestions are welcome. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Password Generator & StringGadget() Show/Hide Password B

Post by Kwai chang caine »

Works fine here
Thanks 8)
ImageThe happiness is a road...
Not a destination
Post Reply