Post by David Drake on Feb 27, 2020 6:36:02 GMT -5
I created the code below for the Rosetta Code task "Password generator" (found at rosettacode.org/wiki/Password_generator). The task has a surprisingly long list of requirements:
Create a password generation program which will generate passwords containing random ASCII characters from the following groups:
lower-case letters: a ──► z
upper-case letters: A ──► Z
digits: 0 ──► 9
other printable characters: !"#$%&'()*+,-./:;<=>?@[]^_{|}~
(the above character list excludes white-space, backslash and grave)
The generated password(s) must include at least one (of each of the four groups):
lower-case letter,
upper-case letter,
digit (numeral), and
one "other" character.
The user must be able to specify the password length and the number of passwords to generate.
The passwords should be displayed or written to a file, one per line.
The randomness should be from a system source or library.
The program should implement a help option or button which should describe the program and options when invoked.
You may also allow the user to specify a seed value, and give the option of excluding visually similar characters.
For example: Il1 O0 5S 2Z where the characters are:
capital eye, lowercase ell, the digit one
capital oh, the digit zero
the digit five, capital ess
the digit two, capital zee
My code does everything except allow the user to specify a seed value.
I'm sure the character checking algorithm could be better. I look forward to seeing your comments and suggestions.
Create a password generation program which will generate passwords containing random ASCII characters from the following groups:
lower-case letters: a ──► z
upper-case letters: A ──► Z
digits: 0 ──► 9
other printable characters: !"#$%&'()*+,-./:;<=>?@[]^_{|}~
(the above character list excludes white-space, backslash and grave)
The generated password(s) must include at least one (of each of the four groups):
lower-case letter,
upper-case letter,
digit (numeral), and
one "other" character.
The user must be able to specify the password length and the number of passwords to generate.
The passwords should be displayed or written to a file, one per line.
The randomness should be from a system source or library.
The program should implement a help option or button which should describe the program and options when invoked.
You may also allow the user to specify a seed value, and give the option of excluding visually similar characters.
For example: Il1 O0 5S 2Z where the characters are:
capital eye, lowercase ell, the digit one
capital oh, the digit zero
the digit five, capital ess
the digit two, capital zee
My code does everything except allow the user to specify a seed value.
I'm sure the character checking algorithm could be better. I look forward to seeing your comments and suggestions.
[variables]
dim w$(92),pw$(10)
for a = 33 to 126 'load available characters into array
if a<>92 and a<>96 then
ct=ct+1
w$(ct)=chr$(a)
end if
next a
[start]
input "Number of passwords to generate (enter 0 for help): ";n
if n=0 then 'show help
print "======================================================"
print "This program generates passwords of random characters."
print "When prompted, enter the number of passwords you would"
print "like to create, press enter, then enter the number of"
print "characters you would like in the generated passwords."
print "======================================================"
print ""
goto [start]
end if
input "Length of password (n>4):";p
if p<4 then p=4
print
print "Creating ";n;" passwords of length ";p
print
print " Generated passwords"
print "======================="
for a = 1 to n
[tryagain]
pw$=""
for b = 1 to p 'create string of random characters from loaded array
pw$=pw$+w$(int(rnd(1)*92)+1)
next b
'test for 1 uppercase, 1 lowercase, 1 digit, 1 other
'test for other characters
if fail(33,47,pw$)=1 and fail(58,64,pw$)=1 and fail(91,96,pw$) and fail(123,126,pw$) then [tryagain]
'test for digits
if fail(48,57,pw$)=1 then [tryagain]
'test for lower case
if fail(97,122,pw$)=1 then [tryagain]
'test for upper case
if fail(65,90,pw$)=1 then [tryagain]
'check for visually similar characters
if sim(pw$,"I","1")=1 then [tryagain]
if sim(pw$,"I","l")=1 then [tryagain]
if sim(pw$,"1","l")=1 then [tryagain]
if sim(pw$,"O","0")=1 then [tryagain]
if sim(pw$,"5","S")=1 then [tryagain]
if sim(pw$,"2","Z")=1 then [tryagain]
print pw$
next a
print "======================="
print
input "Create more passwords (Y/N)?";yn$
if left$(lower$(yn$),1)="y" then goto [start]
print
print "Goodbye"
end
function fail(min,max,p$)'check for existance of a character
for c = min to max
if instr(p$,chr$(c))<>0 then
fail=0
exit for
else
fail=1
end if
next c
end function
function sim(pw$,a$,b$)'check for visually similar characters
if instr(pw$,a$)<>0 and instr(pw$,b$)<>0 then sim=1
end function