Password Generator

Everything else that doesn't fall into one of the other PB categories.
AZJIO
Addict
Addict
Posts: 2254
Joined: Sun May 14, 2017 1:48 am

Password Generator

Post by AZJIO »

There was nothing to do.

Code: Select all

EnableExplicit

Define UserIntLang

CompilerSelect #PB_Compiler_OS
	CompilerCase #PB_OS_Windows
		Define *Lang
		If OpenLibrary(0, "kernel32.dll")
			*Lang = GetFunction(0, "GetUserDefaultUILanguage")
			If *Lang And CallFunctionFast(*Lang) = 1049 ; ru
				UserIntLang = 1
			EndIf
			CloseLibrary(0)
		EndIf
	CompilerCase #PB_OS_Linux
		If ExamineEnvironmentVariables()
			While NextEnvironmentVariable()
				If Left(EnvironmentVariableName(), 4) = "LANG" And Left(EnvironmentVariableValue(), 2) = "ru"
					UserIntLang = 1
					Break
				EndIf
			Wend
		EndIf
CompilerEndSelect

#CountStrLang = 8
Global Dim Lng.s(#CountStrLang)
Lng(1) = "Password Generator"
Lng(2) = "Password"
Lng(3) = "Numbers"
Lng(4) = "Signs"
Lng(5) = "Both cases"
Lng(6) = "UPPERCASE"
Lng(7) = "Lowercase"
Lng(8) = "Length"

If UserIntLang = 1
	Lng(1) = "Генератор пароля"
	Lng(2) = "Пароль"
	Lng(3) = "Цифры"
	Lng(4) = "Знаки"
	Lng(5) = "Оба регистра"
	Lng(6) = "ЗАГЛАВНЫЕ"
	Lng(7) = "строчные"
	Lng(8) = "Длина"
EndIf


#Window = 0

Enumeration
	#btn
	#chNum
	#chSmb
	#OptL1
	#OptL2
	#OptL3
	#spNMin
	#spNMax
	#spSMin
	#spSMax
	#strPass
	#strSmb
	#spLen
	#txtLen
EndEnumeration



Procedure GenPass()
	Protected NewList Pass.s()
	Protected NewMap Dupl.s()
	Protected NewMap DuplNum()
	Protected NewMap DuplPos()
	Protected smb$, Result$
	Protected min, max, CountNum, CountSmb, i, j, pos, Opt, a, PassLen, tmp$, tmp
	Protected *elem1, *elem2
	
; 	smb$ = "@#%^&*_+-=<>?.,!"
	smb$ = GetGadgetText(#strSmb)
	If Len(smb$) = 0
		ProcedureReturn
	EndIf
	
	PassLen = Val(GetGadgetText(#spLen))
	If PassLen < 4
		ProcedureReturn
	EndIf
	
	If GetGadgetState(#OptL1) & #PB_Checkbox_Checked
		Opt = #OptL1
	ElseIf GetGadgetState(#OptL2) & #PB_Checkbox_Checked
		Opt = #OptL2
	ElseIf GetGadgetState(#OptL3) & #PB_Checkbox_Checked
		Opt = #OptL3
	EndIf
	For i=1 To 100
		AddElement(Pass())
		Select Opt
			Case #OptL1
				If Random(1, 0)
; 					tmp$ = Chr(Random(90, 65))
					tmp$ = Chr(CryptRandom(25) + 65)
					If Not FindMapElement(Dupl(), tmp$)
						AddMapElement(Dupl(), tmp$, #PB_Map_NoElementCheck)
						Pass() = tmp$
						a + 1
					EndIf
				Else
; 					tmp$ = Chr(Random(122, 97))
					tmp$ = Chr(CryptRandom(25) + 97)
					If Not FindMapElement(Dupl(), tmp$)
						AddMapElement(Dupl(), tmp$, #PB_Map_NoElementCheck)
						Pass() = tmp$
						a + 1
					EndIf
				EndIf
			Case #OptL2
				tmp$ = Chr(CryptRandom(25) + 65)
				If Not FindMapElement(Dupl(), tmp$)
					AddMapElement(Dupl(), tmp$, #PB_Map_NoElementCheck)
					Pass() = tmp$
					a + 1
				EndIf
			Case #OptL3
; 				tmp$ = Chr(Random(122, 97))
				tmp$ = Chr(CryptRandom(25) + 97)
				If Not FindMapElement(Dupl(), tmp$)
					AddMapElement(Dupl(), tmp$, #PB_Map_NoElementCheck)
					Pass() = tmp$
					a + 1
				EndIf
		EndSelect
		If a > PassLen
			Break
		EndIf
	Next
	
	
	If GetGadgetState(#chNum) & #PB_Checkbox_Checked
		min = Val(GetGadgetText(#spNMin))
		max = Val(GetGadgetText(#spNMax))
		If max < 1 Or min < 1
			ProcedureReturn
		EndIf
		CountNum = Random(max, min)
		a = 0
		For i=1 To 100
			*elem1 = AddElement(Pass())
			tmp$ = Str(CryptRandom(9))
			If FindMapElement(DuplNum(), tmp$)
				Continue
			Else
				AddMapElement(DuplNum(), tmp$, #PB_Map_NoElementCheck)
			EndIf
			Pass() = tmp$
			For j = 1 To 100 ; принудительно ограничиваем число итераций на случай занятых всех позиций
				tmp = Random(PassLen, 1)
				tmp$ = Str(tmp)
				If Not FindMapElement(DuplPos(), tmp$)
					AddMapElement(DuplPos(), tmp$, #PB_Map_NoElementCheck)
					Break
				EndIf
			Next
			pos = tmp
			*elem2 = SelectElement(Pass(), pos)
			; 		InsertElement(Pass())
			SwapElements(Pass() , *elem1 , *elem2)
			a + 1
			If a = CountNum
				Break
			EndIf
		Next
	EndIf
	
	If GetGadgetState(#chSmb) & #PB_Checkbox_Checked
		min = Val(GetGadgetText(#spSMin))
		max = Val(GetGadgetText(#spSMax))
		If max < 1 Or min < 1
			ProcedureReturn
		EndIf
		CountSmb = Random(max, min)
		ClearMap(DuplNum()) 
		a = 0
		For i=1 To 100
			*elem1 = AddElement(Pass())
			tmp$ = Mid(smb$, Random(Len(smb$), 1), 1)
			If FindMapElement(DuplNum(), tmp$)
				Continue
			Else
				AddMapElement(DuplNum(), tmp$, #PB_Map_NoElementCheck)
			EndIf
			Pass() = tmp$
			For j = 1 To 100 ; принудительно ограничиваем число итераций на случай занятых всех позиций
				tmp = Random(PassLen, 1)
				tmp$ = Str(tmp)
				If Not FindMapElement(DuplPos(), tmp$)
					AddMapElement(DuplPos(), tmp$, #PB_Map_NoElementCheck)
					Break
				EndIf
			Next
			pos = tmp
			*elem2 = SelectElement(Pass(), pos)
			; 		InsertElement(Pass())
			SwapElements(Pass() , *elem1 , *elem2)
			a + 1
			If a = CountNum
				Break
			EndIf
		Next
	EndIf
	
	i = 0
	ForEach Pass()
		Result$ + Pass()
		i + 1
		If i = PassLen
			Break
		EndIf
	Next
	
	SetGadgetText(#strPass, Result$)
	
;     ProcedureReturn Result$
EndProcedure



;-┌──GUI──┐
If OpenWindow(#Window, 0, 0, 250, 340, Lng(1), #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	
	StringGadget(#strPass, 15, 17, 226, 30, Lng(2))
	CheckBoxGadget(#chNum, 15, 68, 120, 22, Lng(3))
	SetGadgetState(#chNum, 1)
	SpinGadget(#spNMin, 149, 66, 45, 24, 0, 16, #PB_Spin_Numeric)
	SpinGadget(#spNMax, 200, 66, 45, 24, 0, 16, #PB_Spin_Numeric)
	SetGadgetState(#spNMin , 2)
	SetGadgetState(#spNMax , 5)
; 	SetGadgetText(#spNMin , "2")
; 	SetGadgetText(#spNMax , "5")
	CheckBoxGadget(#chSmb, 15, 95, 120, 22, Lng(4))
	SetGadgetState(#chSmb, 1)
	SpinGadget(#spSMin, 149, 91, 45, 24, 0, 16, #PB_Spin_Numeric)
	SpinGadget(#spSMax, 200, 92, 45, 24, 0, 16, #PB_Spin_Numeric)
	SetGadgetState(#spSMin , 2)
	SetGadgetState(#spSMax , 5)
	StringGadget(#strSmb, 15, 126, 227, 24, "@#%^&*_+-=<>?.,!")
	OptionGadget(#OptL1, 15, 160, 120, 22, Lng(5))
	OptionGadget(#OptL2, 15, 187, 120, 22, Lng(6))
	OptionGadget(#OptL3, 15, 214, 120, 22, Lng(7))
	SetGadgetState(#OptL1, 1)
	TextGadget(#txtLen, 14, 242, 121, 22, Lng(8))
	SpinGadget(#spLen, 143, 240, 46, 24, 4, 16, #PB_Spin_Numeric)
	SetGadgetState(#spLen , 16)
	ButtonGadget(#btn, 76, 285, 107, 42, "OK")
	
	OpenCryptRandom()

;-┌──Loop──┐
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Gadget
				Select EventGadget()
					Case #btn
						GenPass()
				EndSelect
			Case #PB_Event_CloseWindow
				CloseWindow(#Window)
				CloseCryptRandom()
				End
		EndSelect
	ForEver
EndIf
User avatar
blueb
Addict
Addict
Posts: 1125
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Password Generator

Post by blueb »

I hear you.. I was bored, so yesterday I created (with AI) a complete appointment reminder program. haha

Trouble is I have about 20 - 30 of these 'focused' programs. I'd like to share them, give them away, etc. but the forum.. only accepts a limited amount of source code, and most don't fit into the limits. What we need is an outlet for this.. something that allows larger source, icons, etc. :!:
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Post Reply