Password generator

From Rosetta Code
Revision as of 03:30, 25 April 2016 by Rdm (talk | contribs) (J: eliminate misleading visual pun for glib readers)
Password generator is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 excluding white-space, backslash and grave: !"#$%&'()*+,-./:;<=>?@[]^_{|}~


The generated password(s) must include at least one lower-case letter, one upper-case letter and one character from digits and 'other'. 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).

J

Implementation:

<lang J>thru=: <. + i.@(+*)@-~ chr=: a.&i.

L=: lower=: 'a' thru&.chr 'z' U=: upper=: 'A' thru&.chr 'Z' D=: digit=: '0' thru&.chr '9' O=: other=: ('!' thru&.chr '~')-.L,U,D,'`\'

pwgen =:verb define :: pwhelp

 assert. (i.0)-:$y
 sizes=. #/.~ (i.4),?(y-4)#4 NB. how many of each?
 NB. pick allocated characters from each group, then shuffle 
 (?~@# { ]) ;sizes (?@(##) { ])each L;U;D;O
 pwgen"0 x#y

)

pwhelp =:echo bind (noun define)

 [x] pwgen y - generates passwords of length y
 optional x says how many to generate (if you want more than 1)
 y must be at least 4 because passwords must contain
 four different kinds of characters.

)</lang>

Example use (from J command line):

<lang J> pwgen

 [x] pwgen y - generates passwords of length y
 optional x says how many to generate (if you want more than 1)
 y must be at least 4 because passwords must contain
 four different kinds of characters.
  pwgen 10

oUVT[+l2pk

  5 pwgen 10

456u;mJ#{Y WT^bf2|50) r&fLIY0IvW 4UZQC5!y:8 I}G86(;1Ob</lang>