Password generator

Share your advanced PureBasic knowledge/code with the community.
User avatar
Azul
Enthusiast
Enthusiast
Posts: 109
Joined: Fri Dec 29, 2006 9:50 pm
Location: Finland

Password generator

Post by Azul »

Code updated for 5.20+

This is just 10-min-made password generator.

Code: Select all

Procedure.s mkpass(password_length)
  chars$="abcdefghijklmnopqrstuvwxyz" ; possible characters, keep these lcase
  For a=1 To password_length
    Select Random(1) ; 0 = char, 1 = digit
      Case 1 ; is digit
        pass$+Str(Random(9))
      Case 0 ; is character
        position=Random(Len(chars$)) ; random character selector
        char_case = Random(10)
        If char_case<5 ; less than 5 is ucase
          pass$+UCase(Mid(chars$,position,1))
        Else
          pass$+Mid(chars$,position,1)
        EndIf
    EndSelect
  Next
  ProcedureReturn pass$
EndProcedure

Debug mkpass(12)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Hi,

I'm wondering how this should be used? :)

I mean if we generate a password, then how do we use this password? Without a facility to validate the password I'm not sure as to the use.

I can see that this code could be used to generate a random 'user name', and then you'd use something like an MD5 hash to generate a password etc.
I may look like a mule, but I'm not a complete ass.
User avatar
Azul
Enthusiast
Enthusiast
Posts: 109
Joined: Fri Dec 29, 2006 9:50 pm
Location: Finland

Post by Azul »

I have used similar (my friend coded it to linux) to create automatic passwords which will be mailed to users after printed to paper :) (automatic user creation and password printing system)
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

Hmm, that's an OK procedure but you can neaten up a couple things: first, making a string of the alphabet and using Mid() to pick out letters isn't very neat, and second, doing a Random(10) and checking if it is less than 5 is a 50% chance, why not just use Random(1) and check if it is 0 or 1? Something like this (also if you have PB4.00+ you can make uppercases optional):

Code: Select all

Procedure.s MakePass(Length.l, IncludeUpperCase = #False)
	Define i.l, Result.s, Char.l
	For i = 1 To Length
		Char = Random(35)
		If Char < 10
			Result + Chr(Char + 48)
		Else
			If IncludeUpperCase And Random(1)
				Result + Chr(Char + 55)
			Else
				Result + Chr(Char + 87)
			EndIf
		EndIf 
	Next i
	ProcedureReturn Result
EndProcedure

Debug MakePass(12)
Debug MakePass(12, #True)
Post Reply