VERY Simple password generator

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

VERY Simple password generator

Post by GeoTrail »

I need to change my passwords from time to time and instead of downloading a separate program just for that task, I wrote my own, VERY simple password generator.

You start the program, it asks for the amount of passwords you want to create and the length of the passwords, then they are saved in a text file called Passwords.txt

I have tested the program with 1 million passwords that has a length of 10 characters on my laptop and it took less then 2 seconds to create the passwords file.

Anyways, here's the code:

Code: Select all

;
; Simple password generator written by GeoTrail <geotrail@gmail.com>
;

Declare.s PasswordGenerator(Length.l)
PassFilename$ = "Passwords.txt"

NoOfPwd.l = Val(InputRequester("Password Generator", "Enter number of passwords to create:", Str(1000)))
PwdLen.l  = Val(InputRequester("Password Generator", "Enter length of passwords to create:", Str(10)))

If NoOfPwd >= 1 And PwdLen >= 1

  If FileSize(PassFilename$) = -1
  
    OverWrite:

    If CreateFile(0, PassFilename$)
    
      For c=1 To NoOfPwd
      
        WriteStringN(0, PasswordGenerator(PwdLen))
      
      Next
      
      CloseFile(0)
      
      MessageRequester("Finished", "Successfully created file named " + Chr(34) + PassFilename$ + Chr(34) + Chr(13) + "containing "+Str(NoOfPwd)+" passwords with a length of "+Str(PwdLen)+" characters." + Chr(13) + Chr(13) + "This small util was written by GeoTrail.no")
      
    EndIf
    
  Else
  
    If MessageRequester("File exist", "A password file already exists, do you want to overwrite it?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
    
      Goto OverWrite
    
    EndIf
  
  EndIf

Else

  MessageRequester("Error", "You must enter at least 1 on both requests.")

EndIf

;
End


Procedure.s PasswordGenerator(Length.l)

  For x=1 To Length
    
      i = Random(62)
    
      Select i
      
        Case 1
          String$ + "a"
        Case 2
          String$ + "b"
        Case 3
          String$ + "c"
        Case 4
          String$ + "d"
        Case 5
          String$ + "e"
        Case 6
          String$ + "f"
        Case 7
          String$ + "g"
        Case 8
          String$ + "h"
        Case 9
          String$ + "i"
        Case 10
          String$ + "j"
        Case 11
          String$ + "k"
        Case 12
          String$ + "l"
        Case 13
          String$ + "m"
        Case 14
          String$ + "n"
        Case 15
          String$ + "o"
        Case 16
          String$ + "p"
        Case 17
          String$ + "q"
        Case 18
          String$ + "r"
        Case 19
          String$ + "s"
        Case 20
          String$ + "t"
        Case 21
          String$ + "u"
        Case 22
          String$ + "v"
        Case 23
          String$ + "w"
        Case 24
          String$ + "x"
        Case 25
          String$ + "y"
        Case 26
          String$ + "z"
        Case 27
          String$ + "A"
        Case 28
          String$ + "B"
        Case 29
          String$ + "C"
        Case 30
          String$ + "D"
        Case 31
          String$ + "E"
        Case 32
          String$ + "F"
        Case 33
          String$ + "G"
        Case 34
          String$ + "H"
        Case 35
          String$ + "I"
        Case 36
          String$ + "J"
        Case 37
          String$ + "K"
        Case 38
          String$ + "L"
        Case 39
          String$ + "M"
        Case 40
          String$ + "N"
        Case 41
          String$ + "O"
        Case 42
          String$ + "P"
        Case 43
          String$ + "Q"
        Case 44
          String$ + "R"
        Case 45
          String$ + "S"
        Case 46
          String$ + "T"
        Case 47
          String$ + "U"
        Case 48
          String$ + "V"
        Case 49
          String$ + "W"
        Case 50
          String$ + "X"
        Case 51
          String$ + "Y"
        Case 52
          String$ + "Z"
        Case 53
          String$ + "0"
        Case 54
          String$ + "1"
        Case 55
          String$ + "2"
        Case 56
          String$ + "3"
        Case 57
          String$ + "4"
        Case 58
          String$ + "5"
        Case 59
          String$ + "6"
        Case 60
          String$ + "7"
        Case 61
          String$ + "8"
        Case 62
          String$ + "9"
      
      EndSelect
  
  Next

  ProcedureReturn String$
EndProcedure

I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

One i used recently:

Code: Select all



;Return a random number in a specified range
Procedure RandomFromRange(Maximum.l, Minimum.l)
	ProcedureReturn Maximum - Random(Maximum - Minimum)
EndProcedure


CreateFile(1, "Serial Numbers.txt")
	For x = 1 To 1000
		For y = 1 To 4
			Serial.s + Chr(RandomFromRange(90, 65))
		Next y
		Serial + "-"
		For y = 1 To 4
			Serial.s + Chr(RandomFromRange(90, 65))
		Next y
		Serial + "-"
		For y = 1 To 4
			Serial.s + Chr(RandomFromRange(90, 65))
		Next y
		Serial + "-"
		For y = 1 To 4
			Serial.s + Chr(RandomFromRange(90, 65))
		Next y
		WriteStringN(1, Serial)
		Serial = ""
	Next x
CloseFile(1)
--Kale

Image
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Reading this thread reminded me of some old PHP code I had created and felt appropriate to share here,
I ported it to PureBasic pretty much verbatim (amazing how easy it is to do PB<>PHP porting)
http://www.purebasic.fr/english/viewtopic.php?t=27711

Have fun with it!
Post Reply