NYCJUG/2021-11-09/passwordGenerator

From J Wiki
Jump to navigation Jump to search

GUI-based Password Generator

NB.* passwordGenerator.ijs: GUI-based password generator adapted from
NB. https://code.jsoftware.com/wiki/User:Andrew_Nikitin/Literate.

GPSW=: 0 : 0                              NB. Generate password window
pc gpsw escclose;pn "Generate password";
bin v;
    bin v; bin h;
               cc pwdlen static;cn "Password length";
               cc lenInp edit;
	   bin z;
           cc size scrollbar 4 1 0 50 8;
    bin z;
    bin v;bin h;
            cc rbgenAlt radiobutton; cn "Alternating hands";
            cc rbgenRand radiobutton; cn "Generate randomly";
        bin z;
        groupbox "Random Generation Parameters";
        bin h;
            cc ckab checkbox; cn "All below";
            cc ckes checkbox; cn "E&xclude similar characters";
        bin z;
        cc ckal checkbox;cn "&lowercase";
        cc ckau checkbox;cn "&uppercase";
        cc ckad checkbox;cn "&digits (0-9)";
        cc ckah checkbox;cn "&special characters";
        groupboxend;

        bin h;
            cc strength static;cn "Strength: ";
	    cc strenval static;cn "0";
        bin z;
        cc pwdLabel static; cn "Generated password:";
        cc pwd edit readonly;
    bin z; bin z;
cc generatePwd button;cn "&Generate";
pas 3 3;pcenter;
rem form end;
)

gpsw_cancel_button=: wd bind 'pclose;'
gpsw_exit_button=: gpsw_cancel_button
gpsw_close=: gpsw_cancel_button

gpsw_run=: 3 : 0
    wd GPSW
    wd 'pshow;'
)

gpsw_run ''

NB. Poor initialization of the random seed: fix this!
9!:1 [ 60 60 24 #. |. _3 {. <. (6!:0 '')

NB. Initialize default settings:
([: wd 'set ' , ])&> 'rbgenAlt 1';'ckab 1';'ckes 0';'ckal 1';'ckau 1';'ckad 1';'ckah 1';'size 16';'lenInp text 16'

NB. Length or number of characters in the password. Limits set for
NB. scrollbar size implicitly define limits on allowed password sizes.
NB. Length is displayed in static lenInp.

gpsw_size_button=: 3 : 0
  wd 'set lenInp text ',size
  gpsw_generatePassword_button ''
)

gpsw_lenInp_button=: 3 : 0
   newsz=. wd 'get lenInp text'
   wd 'set size ',newsz            NB. Set same value for scrollbar.
   newsz=. wd 'get size value'     NB. scrollbar enforces min/max
   wd 'set lenInp text ',newsz     NB. Ensure actual value shows.
   gpsw_generatePassword_button ''
)

NB. SIMILAR noun lists characters that look similar and can be easily confused
NB. with each other. Definition of "look similar" depends on one's handwriting
NB. and is therefore user configurable. If exclude similar is checked, then
NB. password will not contain any of those characters, so there cannot be mistake
NB. in transcription.

note=. 0 : 0
Note, how SIMILAR is explained here, where it belongs, but inserted in the code in the beginning of the file, so it can be tweaked by the user.
)

SIMILAR=:'OQ0Do';'Il1';'2Zz';'5Ss';'8B';'9g';'6G'

rsel=: ;@:(({~ ?@#)&.>)@(-.&a:)
flt=: ;@[ -. ;@[ -. ]
rrsame =: 3 : 0
   x=.(flt&y)&.>SIMILAR
   (y -. ;x) , rsel x
)

ALPH_LOW=:a.{~ (a.i.'a')+i.26
ALPH_UP=:a.{~ (a.i.'A')+i.26
ALPH_DIG=:a.{~ (a.i.'0')+i.10
ALPH_HEX=:a.{~ (a.i.'a')+i.6

calcStrength=: 3 : 0
   alph=.;("."0 ckal,ckau,ckad,ckah)#(ALPH_LOW;ALPH_UP;ALPH_DIG;ALPH_HEX)
   alph=.alph -.~^:(ckes-:,'1')~ ; (#~ (1 < +/@e.&alph)&>) SIMILAR
   alph=.~.alph,'01'#~0=#alph

NB. Entropy is calculated as {\displaystyle e=\lceil {n\times \log _{2}\#\alpha }\rceil }
NB. It can be used to estimate password "strength" in bits.
NB. Please note that formula is included in its natural form. This removes
NB. limitation of including formulas and diagrams into in-source comment
NB. only in the form of ascii art.
NB. Here "strength" is the negative logarithm of the probability to guess
NB. the password first time.
   alph;<<.(".size)*2^.#alph
)

NB.* gpsw_generatePwd_button: Either generate alternating-hands password OR
NB. select set of characters allowed in the password, weeding out similar
NB. characters if requested.
gpsw_generatePwd_button=: 3 : 0
   svSz=. wd 'get size value'
   if. rbgenAlt do. pwd=. genAltPwd ''
   else.
      'alph strength'=. calcStrength ''
       pwd=. alph {~ ? (". size)##alph
   end.
    wd 'set pwd text ',pwd
    wd 'set strenval text ',":strength
    wd 'set size ',svSz
    wd 'set lenInp text ',svSz
NB.   wdclipwrite pwd  NB. work on this
)

NB. Any click on any of the checkboxes that changes allowed character set generates new password.

gpsw_ckau_button=: gpsw_generatePwd_button
gpsw_ckal_button=: gpsw_generatePwd_button
gpsw_ckah_button=: gpsw_generatePwd_button
gpsw_ckad_button=: gpsw_generatePwd_button
gpsw_ckes_button=: gpsw_generatePwd_button

NB. [Above] (C) 1997 Andrew Nikitin
NB. This is free software. You may use, copy, modify and distribute it at your
NB. own risk.
NB. «Disclaimer»
NB. May contain toxic waste. Do not eat.

NB. [From wiki page] This page was last edited on 8 September 2009, at 03:58.

NB.* LRAltWords.ijs: find words typed by alternating left and right hands; use this
NB. list to help generate easily-typeable, long passwords.

NB.* LRsets: Left/right-hand sets of characters.
LRsets=: 'qwertasdfgzxcvb~`12345!@#$%';'yuiophjklnm67890-_=+\|^&*()[{]};:''",.<>?/'

genAltPwd=: 3 : 0
   svSz=. wd 'get size value' [ svPwd=. wd 'get pwd text' [ 'alph strength'=. calcStrength ''
   wd 'set lenInp text 20'
   wd 'set size 20'
   gpsw_gp_button ''
   seedStr=. wd 'get pwd text'
   wd 'set size ',svSz
   wd 'set lenInp text ',svSz
   wd 'set pwd text ',svPwd
   wd 'set strenval text ',":strength
   nw=. #WORDS
   wrds=. (]{~[:?~#) WORDS{~((<.>:nw^.nn)$nw) #: nn=. (#;LRsets)#.x:(;LRsets) i. tolower 4 (] $~ [ >. [:#]) seedStr
   wrds=. (randUppercase wrds),&.>(#wrds){.makeShims wrds   
   newpwd=. (".size){.;wrds
   wd 'set pwd text ',newpwd
   'alph strength'=. calcStrength ''
   wd 'set strenval text ',":strength
   newpwd
NB.EG genAltPwd ''
)
NB. all together; last1st=. <"1 ([: }: {:&> (,.) 1 |. {.&>) wrds=. (]{~[:?~#) WORDS{~((<.>:nw^.nn)$nw) #: nn=. (#;LRsets)#.x:(;LRsets) i. tolower 4 (] $~ [ >. [:#]) seedStr [ nw=. #WORDS
NB. nonAlphaSets=. ((LRsets e.&.><'0123456789')i.&> 1)}.&.>LRsets [ altOK=. ~:/&>last1st e.&>&.><LRsets [ wrds=. randUppercase wrds

selRand=: ] { ~ [: ? #     NB.* selRand: select 1 item randomly from vec arg.
NB.* makeShims: maintain hand alternation by inserting shims between a word
NB. that ends on one side and a following word the begins on same side; the
NB. shim is on the hand alternating between the two consecutive same hand chars.
makeShims=: 3 : 0
   nonalp=. (-.&.>*./\&.>LRsets e.&.><Alpha_j_)#&.>LRsets
   last1st=. <"1 ([: }: {:&> (,.) 1 |. {.&>) tolower&.>wrds=. y
   juncture=. #.>0{"1 last1st e.&.>/LRsets            NB. (0,3)->needs shim; (1,2)->OK as is.
NB. However, generate double shims for OK transitions and insert a few for variety.
   shims=. selRand&>&.>(juncture{0;0 1;1 0;1){&.><nonalp
   useWhich=. (1) 0}(juncture e. 0 3)+.(#juncture)?@$2   NB. Randomly select but always 1st
   shims=. useWhich#&.>shims                             NB. so we include a non-alpha char.
NB.EG  wrds=. wrds,&.>makeShims wrds
)

randUppercase=: 3 : 0
   wrds=. y
   randUCix=. ?@:#&>wrds                NB. Random letter in each word to uppercase
   ww=. (# ?~ [: >. [: -: #) randUCix   NB. Which words chosen to have letter uppercased
   wh=. ww{randUCix                     NB. Where the letter is for each chosen word
   newLets=. toupper&.>wh{&.>ww{wrds    NB. Upper case letters to insert
   for_ix. i.#wh do.
       wrds=. (<(>ix{newLets)(ix{wh)}>ix{ww{wrds) ix}wrds
   end.
   wrds
)

WORDS=: <;._2 ] LF (] , [ #~ [ ~: [: {: ]) CR-.~0 : 0
akey
akka
algy
ambo
amma
apay
appt
bibb
bibi
bick
biff
bigg
birk
bizz
bkcy
blam
blay
bobo
bock
body
bogo
bogy
boob
boof
bosk
bowk
bowl
boxy
bozo
bubo
buck
buff
bufo
bugi
burn
burp
bury
busk
busy
buzz
byee
bygo
byth
coff
cowk
cowy
coxy
cozy
cuff
cuvy
diff
dixy
doby
doff
dogy
dowp
doxy
dozy
dubb
dyak
ejam
ekka
envy
eyey
eyry
fiqh
firk
firm
fizz
flak
flap
flay
flem
fley
foam
fogo
fogy
fork
form
fowk
fowl
foxy
fozy
fuck
fuff
fugu
fury
fusk
fuzz
gizz
glam
gley
gobi
gobo
goby
gogo
goof
goog
gorp
gory
gowk
gowl
gown
guam
guck
guff
gugu
gurk
gyri
gyro
hajj
hayz
iamb
iffy
jaap
jake
jama
jamb
jams
jaob
jape
jays
jeep
jeux
kaif
kaka
kame
kang
kapa
kapp
kays
keek
keep
kemb
keps
kept
keys
kwhr
lamb
lapp
maam
maja
make
mama
mang
manx
maps
maux
maya
mays
meek
mems
meng
meow
oofy
oozy
oryx
paig
pams
pang
papa
pape
paps
pays
payt
peek
peep
peke
pelf
peng
peps
prig
priv
prix
prob
prof
prog
prow
prox
quam
quay
quem
quey
riff
rixy
rowy
roxy
ruby
sibb
sizy
sizz
skep
skey
spak
spam
spay
spry
subj
suff
suzy
sybo
tiff
tivy
toby
toff
towy
tuff
vivo
vugg
vugh
wham
whap
whey
wick
wiry
wisp
woak
woof
work
worm
wugg
wusp
wuzu
wych
wyss
xxii
yaje
yaka
yaks
yalb
yams
yang
yapa
yapp
yaps
yaya
ywis
ziff
zizz
zobo
zogo
appay
biffy
bivvy
boffo
boggy
booby
boozy
bubby
buffi
buffy
buggy
buzzy
civvy
cobby
cubby
cuffy
divvy
dizzy
dobby
doggy
dubby
embog
embow
embox
figgy
fizzy
flamb
foggy
fubby
fuffy
fuggy
fuzzy
gibbi
gobby
goofy
gowks
gubbo
guffy
gygis
jakey
jambo
kakke
kappa
kappe
kayak
lamby
layby
mambo
mamey
mamma
mangy
mauby
mayey
namby
papey
privy
proxy
ribby
rubby
ruggy
sibby
sobby
soggy
sybow
tibby
tiffy
tizzy
toffy
tubby
vibix
vuggy
wiggy
woofy
woozy
wuzzy
yakka
yquem
bowwow
bywork
formby
kakkak
offkey
syzygy
vivify
)