Page 1 of 1

Password generator

Posted: Wed Jan 24, 2007 8:54 am
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)

Posted: Wed Jan 24, 2007 10:21 pm
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.

Posted: Wed Jan 24, 2007 10:25 pm
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)

Posted: Thu Jan 25, 2007 11:12 pm
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)