It is currently Sat Dec 14, 2019 1:33 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Password Generator & StringGadget() Show/Hide Password Btn
PostPosted: Mon Feb 25, 2019 2:40 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2214
Location: Singapore
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:
;==============================================================
;   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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Password Generator & StringGadget() Show/Hide Password B
PostPosted: Thu Feb 28, 2019 1:03 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4548
Location: Lyon - France
Works fine here
Thanks 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Caronte3D and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye