There are many really good tools capable of generating random passwords with different complexity and purposes. Some of them are integrated in password managers, some embedded in the browser others available programmatically from the cli with no need to use an online password generator.
I guess there are several thousands functions or blog posts on the internet around this topic, but I think that I can add some interesting information that will be useful to everybody at any level of experience, so let’s start!
PWGEN
My absolute favourite is pwgen that creates a lot of possible passwords meeting my needs , e.g with these options secure, unambiguous and containing symbols with the exact length of 10 characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$: pwgen -sBy 10 Y+dJ4FH>7F Ppfff]M7'3 c~T3MP)eiR @4XA@,cjd/ <ax,^)-:T4 }C4yPpUe=: 7&,4Jpc?j* &7@JEE(~<e ^a{X:C4A*r Ra|u7xX{&a /]T~-$3HH( +CHkYTjr$4 x3>|kvK{$C }u`X|LJ\P4 ,<9'\<z;"W vsC'abp*3u !@9/nwq<E! AM)H#r9@(# "+iPb{,3|; 4X9Ea<qgTV =mC/_F39kq }]|Tn3|#/P g[UJa-k9]& 9P#>V$~A(t v+v.9em@EL <nXP4RUnrT d(W\,%9?~x d$.e4Ni;pH z!w3w$EU9_ a$3nPd`Xcg ?Ep7VdL&(L Prt^N97[vT M"b~VL?:9n )3EXWfH@N| W;3\%nPpx* ;4pm.#o]LH qH$>TV_p94 tAP!{j(9=j 3Aqu@9)+vv Le]7?C'moT R4d.q\7;~k {Mb|W'Wr{4 \>z94VCNTe Vh9)/H.NAd F@W@f7]]v$ |Les`!\}Y3 'c]Ws4L4?f ,H`9i,&>f, /pm4Ldd;>_ kC#4EhR.tn .9goCTLFz~ A[&j97>`J| %4`7%rU?F] e{):n&4!4X )M7<|XaUR" /:_3h%Lh~c 3g%JAeqppg E3$X,?Cmu\ =PH"7Vg+_L r9NjK\n7Hk V9n@+!~%'h 4v#HcX:i%? h%4Pqeh?7) h\7'#ML-t& Fe=C4U\:4, |77!fk<E\s ,3@xn|U)`Y W.qYRAq'3o i}u,{3M'Ls +Y9TuF]~ck @v4}_Nc&gd ,pysKWk7`` KN^f4HJ^[q }7]dx9Y^u' "e-hkU'w#3 ,#7TrkysWC ot:R]yX_/7 XCg3&PiEnj hRR*\V]z3j tX?>+9!?dP +c"$?hzAE7 X3sRs+M?E= a%T|w4#AfA x)ncj_$7RM F%&)kq)v9N }a4LVsd|w) oU%$wmL+/7 {Xi7gko{}_ WA9`c^ptFb z@~?+"4?)A L+sfx_HEd4 H}r-M7=M]' >x^<j\[4HE %y~X3eh_u/ HP-3ywAb=( zA|9,s+a3/ $i4qy&{#P` 3!^w<w~(Ra -^;=Y4pE$X KW#HVuT7"j jH}:~#N3sc q!_}@:3Fpv [~.mHzYUM4 A@iNuL4]`P NfcR.9{qR~ ]]'fv7U'i9 r7C{Reo^[# (ccRv*M4e> <7A#-'d{_a n$9ziJ|mm| mo]3kY/fzV 3(/%33;e,A LU"$M`[3>j Nbxa<4v(KX tq9Kr@V-/< 7cjqU*dp;A v/TJJRWN7: ps%ea3jTik $+t%3E;7!U T~\TzaP\9U X(}(f9]3t_ N3K3*%d)}7 4re[-!X,K' .q#99g?KqE 4"cAR]{azb }@[`A3F)ze KK&~Me&_$4 )U4|mj&g\| f"=xn7"@uM <^xoY~?9?/ .C+4=|oFep F44T?`3A^] &44+:f=\Pa `Xh4?J!gH7 }"cXT7H|pk KjtU3>"}zV 4UJR+%YY77 9Pq@@T\Ue~ #'o44~A%CJ kR':yV:h3/ |
Or 3 password 50 character long and so on:
1 2 3 4 |
$ pwgen -sBy 50 3 /{3>w<~hV>a4$C!%H!=^f&U\aMc,$y~Y!d+(K<$}x%V$~p'R|? z7;[RAktU7y-p@*AK]\*c#;4CuW|hF+iv"-FR%nU'^_p)aH~i] =T)C{^hK(dp_=ci*ft7~7TT|nVid-^?](aN%Yy[s&{d7u#<Pvk |
Please check the man page for pwgen for all the possible options.
This tool is so simple and efficient. I use it in every OS, with Windows and WSL is straightforward to use it (and simple to get it with any package manager)within the cmd prompt or from powershell adding just WSL before pwgen command to have the same result. With MacOS and home-brew is just as simple to install it.
When do I need to generate password?
This question is interesting. A password should expire (frequently) and should be unique (as definition of secret and security). For this reason creating a new password and having the ability to generate it must simple and fast as possible.
There are many password manager tools available and most of them will provide or suggest a randomly generated password for you. Also Google Chrome can generate a password with a right-click on the password field, mozilla firefox has many add-ins or plugins with a similar feature.
In almost every company the most popular use for generating random password is more likely a part of an on-boarding script for generate AD Users or an automated password reset, when is required to generate a random password and not to depend from third-party modules if possible or desirable.
I prefer to re-use code as especially aiming for a robust solution and focus my efforts on solving problems and write code that is not already part of a library/api or module, but to create a lightweight password generator are just few lines of code.
My PowerShell simplified version of pwgen
This powershell script generates a password. It’s a simple function that has an integer parameter for the password length (between 8 and 32 characters), generates a random choice from any available common ascii code and validate the result with a Regular Expression , just to check if the password is strong enough to meet the common password complexity requirements of Active Directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# Paolo Frigo, 2018 # https://www.scriptinglibrary.com Function New-RandomPassword{ Param( [ValidateRange(8, 32)] [int] $Length = 16 ) $AsciiCharsList = @() foreach ($a in (33..126)){ $AsciiCharsList += , [char][byte]$a } #RegEx for checking general AD Complex passwords $RegEx = "(?=^.{8,32}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*" do { $Password = "" $loops = 1..$Length Foreach ($loop in $loops) { $Password += $AsciiCharsList | Get-Random } } until ($Password -match $RegEx ) return $Password } # This example just print a random password # Write-Output "Generate Random Password: $(New-RandomPassword(10))" |
And if I want a result like formatted like pwgen I just need 2 nested for loops:
1 2 3 4 5 6 7 8 |
#Generate a passwords similar to pwgen" for ($i = 0; $i -lt (12); $i++) { $line = "" for ($j = 0; $j -lt (5); $j++) { $line += "$(New-RandomPassword(10)) " } Write-Output $line } |
This is the end result, very similar indeed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
PS /> ./New-RandomPassword.ps1 @m#w"$K9jX ,<5,*'Q{hp ^3Td&t,e$I C&wNkE.}1{ DT!%+jMom4 AT1M*LF4!/ h]KiooN9$0 ^5tM@E+qxB FG+Rut$6\P i0Z-n#xYO' U]^xu9+b'` ~(l?z71'&x gV20RQkh%, j^di<I4#J& 6sVMx`%Dmy G@oKZ1-d_g +Ea5iw1Qf% 67dNU$7]Rq =#s{(X8'VK :6@f<60!(Y *^QBT51&m? P,5FH%3&um (|~K4]!:bP &8DFgc"%f& z5&iGsH:nx HX*!5rW!DW STECR2^5%* VM9'(,Wx1! mgrI2\-v@i x32$6.$331 %p1:~Guf2+ :%n=tw^+a7 3Ks73J@s|* N5[+}&ndNR >"*GroBR3B `Jkm\jk3#$ *56Hw[;(YB ,O|$bM0k5s %`5W@oyNma \X}+s6"yY# [\<Jf!94[9 x~i-,$M3/@ &]MVC][N2H XAH},5x1#{ +n^3g'juMU 2?9$:a;{cp o_pvN6hd$A $\6V50ew`: kb5?&el?c7 k"$HhwTM-9 ;L1DH%s-t? }?h~1L@uoo 79v!wp]ZMJ C;c*O<0S+B e8^kRnT0-g bZ[,}q[\1# h4&;>^0Z>< #]5^rztu8i R_}^{Wat3S |LQENG&jH4 |
As usual, you can find this on my github repository.
Pester
Adding several unit tests for an automated script is key for a robust application, in this case the script is simple, but the results is really interesting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
Invoke-Pester .\New-RandomPassword.Tests.ps1 -CodeCoverage .\New-RandomPassword.ps1 Describing New-RandomPassword [+] Should generate a String 133ms [+] Should throw an exeption if the password lenght (0) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (1) is out of the accepted range (8-32) 21ms [+] Should throw an exeption if the password lenght (33) is out of the accepted range (8-32) 19ms [+] Should throw an exeption if the password lenght (34) is out of the accepted range (8-32) 21ms [+] Should throw an exeption if the password lenght (35) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (36) is out of the accepted range (8-32) 17ms [+] Should throw an exeption if the password lenght (37) is out of the accepted range (8-32) 15ms [+] Should throw an exeption if the password lenght (38) is out of the accepted range (8-32) 15ms [+] Should throw an exeption if the password lenght (39) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (40) is out of the accepted range (8-32) 19ms [+] Should throw an exeption if the password lenght (41) is out of the accepted range (8-32) 14ms [+] Should throw an exeption if the password lenght (42) is out of the accepted range (8-32) 24ms [+] Should throw an exeption if the password lenght (43) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (44) is out of the accepted range (8-32) 21ms [+] Should throw an exeption if the password lenght (45) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (46) is out of the accepted range (8-32) 20ms [+] Should throw an exeption if the password lenght (47) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (48) is out of the accepted range (8-32) 15ms [+] Should throw an exeption if the password lenght (49) is out of the accepted range (8-32) 17ms [+] Should throw an exeption if the password lenght (50) is out of the accepted range (8-32) 21ms [+] Should throw an exeption if the password lenght (51) is out of the accepted range (8-32) 20ms [+] Should throw an exeption if the password lenght (52) is out of the accepted range (8-32) 27ms [+] Should throw an exeption if the password lenght (53) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (54) is out of the accepted range (8-32) 22ms [+] Should throw an exeption if the password lenght (55) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (56) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (57) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (58) is out of the accepted range (8-32) 19ms [+] Should throw an exeption if the password lenght (59) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (60) is out of the accepted range (8-32) 19ms [+] Should throw an exeption if the password lenght (61) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (62) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (63) is out of the accepted range (8-32) 17ms [+] Should throw an exeption if the password lenght (64) is out of the accepted range (8-32) 17ms [+] Should throw an exeption if the password lenght (65) is out of the accepted range (8-32) 23ms [+] Should throw an exeption if the password lenght (66) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (67) is out of the accepted range (8-32) 19ms [+] Should throw an exeption if the password lenght (68) is out of the accepted range (8-32) 15ms [+] Should throw an exeption if the password lenght (69) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (70) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (71) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (72) is out of the accepted range (8-32) 17ms [+] Should throw an exeption if the password lenght (73) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (74) is out of the accepted range (8-32) 17ms [+] Should throw an exeption if the password lenght (75) is out of the accepted range (8-32) 14ms [+] Should throw an exeption if the password lenght (76) is out of the accepted range (8-32) 20ms [+] Should throw an exeption if the password lenght (77) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (78) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (79) is out of the accepted range (8-32) 20ms [+] Should throw an exeption if the password lenght (80) is out of the accepted range (8-32) 21ms [+] Should throw an exeption if the password lenght (81) is out of the accepted range (8-32) 15ms [+] Should throw an exeption if the password lenght (82) is out of the accepted range (8-32) 19ms [+] Should throw an exeption if the password lenght (83) is out of the accepted range (8-32) 15ms [+] Should throw an exeption if the password lenght (84) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (85) is out of the accepted range (8-32) 14ms [+] Should throw an exeption if the password lenght (86) is out of the accepted range (8-32) 20ms [+] Should throw an exeption if the password lenght (87) is out of the accepted range (8-32) 14ms [+] Should throw an exeption if the password lenght (88) is out of the accepted range (8-32) 22ms [+] Should throw an exeption if the password lenght (89) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (90) is out of the accepted range (8-32) 19ms [+] Should throw an exeption if the password lenght (91) is out of the accepted range (8-32) 17ms [+] Should throw an exeption if the password lenght (92) is out of the accepted range (8-32) 17ms [+] Should throw an exeption if the password lenght (93) is out of the accepted range (8-32) 21ms [+] Should throw an exeption if the password lenght (94) is out of the accepted range (8-32) 13ms [+] Should throw an exeption if the password lenght (95) is out of the accepted range (8-32) 15ms [+] Should throw an exeption if the password lenght (96) is out of the accepted range (8-32) 19ms [+] Should throw an exeption if the password lenght (97) is out of the accepted range (8-32) 16ms [+] Should throw an exeption if the password lenght (98) is out of the accepted range (8-32) 18ms [+] Should throw an exeption if the password lenght (99) is out of the accepted range (8-32) 21ms [+] Should throw an exeption if the password lenght (100) is out of the accepted range (8-32) 15ms [+] Should generate a password 8 character long 27ms [+] Should generate a password 9 character long 26ms [+] Should generate a password 10 character long 24ms [+] Should generate a password 11 character long 29ms [+] Should generate a password 12 character long 25ms [+] Should generate a password 13 character long 25ms [+] Should generate a password 14 character long 28ms [+] Should generate a password 15 character long 37ms [+] Should generate a password 16 character long 24ms [+] Should generate a password 17 character long 33ms [+] Should generate a password 18 character long 31ms [+] Should generate a password 19 character long 30ms [+] Should generate a password 20 character long 33ms [+] Should generate a password 21 character long 33ms [+] Should generate a password 22 character long 34ms [+] Should generate a password 23 character long 31ms [+] Should generate a password 24 character long 29ms [+] Should generate a password 25 character long 55ms [+] Should generate a password 26 character long 30ms [+] Should generate a password 27 character long 31ms [+] Should generate a password 28 character long 33ms [+] Should generate a password 29 character long 33ms [+] Should generate a password 30 character long 37ms [+] Should generate a password 31 character long 37ms [+] Should generate a password 32 character long 33ms [+] Should generate a password complex enough for AD (1/300 attempt) 33ms [+] Should generate a password complex enough for AD (2/300 attempt) 25ms [+] Should generate a password complex enough for AD (3/300 attempt) 30ms [+] Should generate a password complex enough for AD (4/300 attempt) 33ms [+] Should generate a password complex enough for AD (5/300 attempt) 34ms [+] Should generate a password complex enough for AD (6/300 attempt) 30ms [+] Should generate a password complex enough for AD (7/300 attempt) 30ms [+] Should generate a password complex enough for AD (8/300 attempt) 24ms [+] Should generate a password complex enough for AD (9/300 attempt) 35ms [+] Should generate a password complex enough for AD (10/300 attempt) 24ms [+] Should generate a password complex enough for AD (11/300 attempt) 39ms [+] Should generate a password complex enough for AD (12/300 attempt) 24ms [+] Should generate a password complex enough for AD (13/300 attempt) 30ms [+] Should generate a password complex enough for AD (14/300 attempt) 25ms [+] Should generate a password complex enough for AD (15/300 attempt) 34ms [+] Should generate a password complex enough for AD (16/300 attempt) 24ms [+] Should generate a password complex enough for AD (17/300 attempt) 26ms [+] Should generate a password complex enough for AD (18/300 attempt) 28ms [+] Should generate a password complex enough for AD (19/300 attempt) 32ms [+] Should generate a password complex enough for AD (20/300 attempt) 26ms [+] Should generate a password complex enough for AD (21/300 attempt) 39ms [+] Should generate a password complex enough for AD (22/300 attempt) 27ms [+] Should generate a password complex enough for AD (23/300 attempt) 37ms [+] Should generate a password complex enough for AD (24/300 attempt) 32ms [+] Should generate a password complex enough for AD (25/300 attempt) 35ms [+] Should generate a password complex enough for AD (26/300 attempt) 25ms [+] Should generate a password complex enough for AD (27/300 attempt) 29ms [+] Should generate a password complex enough for AD (28/300 attempt) 35ms [+] Should generate a password complex enough for AD (29/300 attempt) 32ms [+] Should generate a password complex enough for AD (30/300 attempt) 32ms [+] Should generate a password complex enough for AD (31/300 attempt) 33ms [+] Should generate a password complex enough for AD (32/300 attempt) 31ms [+] Should generate a password complex enough for AD (33/300 attempt) 36ms [+] Should generate a password complex enough for AD (34/300 attempt) 33ms [+] Should generate a password complex enough for AD (35/300 attempt) 24ms [+] Should generate a password complex enough for AD (36/300 attempt) 34ms [+] Should generate a password complex enough for AD (37/300 attempt) 27ms [+] Should generate a password complex enough for AD (38/300 attempt) 27ms [+] Should generate a password complex enough for AD (39/300 attempt) 32ms [+] Should generate a password complex enough for AD (40/300 attempt) 22ms [+] Should generate a password complex enough for AD (41/300 attempt) 27ms [+] Should generate a password complex enough for AD (42/300 attempt) 32ms [+] Should generate a password complex enough for AD (43/300 attempt) 26ms [+] Should generate a password complex enough for AD (44/300 attempt) 41ms [+] Should generate a password complex enough for AD (45/300 attempt) 31ms [+] Should generate a password complex enough for AD (46/300 attempt) 28ms [+] Should generate a password complex enough for AD (47/300 attempt) 30ms [+] Should generate a password complex enough for AD (48/300 attempt) 32ms [+] Should generate a password complex enough for AD (49/300 attempt) 29ms [+] Should generate a password complex enough for AD (50/300 attempt) 32ms [+] Should generate a password complex enough for AD (51/300 attempt) 26ms [+] Should generate a password complex enough for AD (52/300 attempt) 27ms [+] Should generate a password complex enough for AD (53/300 attempt) 29ms [+] Should generate a password complex enough for AD (54/300 attempt) 37ms [+] Should generate a password complex enough for AD (55/300 attempt) 33ms [+] Should generate a password complex enough for AD (56/300 attempt) 29ms [+] Should generate a password complex enough for AD (57/300 attempt) 29ms [+] Should generate a password complex enough for AD (58/300 attempt) 24ms [+] Should generate a password complex enough for AD (59/300 attempt) 33ms [+] Should generate a password complex enough for AD (60/300 attempt) 24ms [+] Should generate a password complex enough for AD (61/300 attempt) 32ms [+] Should generate a password complex enough for AD (62/300 attempt) 33ms [+] Should generate a password complex enough for AD (63/300 attempt) 26ms [+] Should generate a password complex enough for AD (64/300 attempt) 34ms [+] Should generate a password complex enough for AD (65/300 attempt) 24ms [+] Should generate a password complex enough for AD (66/300 attempt) 33ms [+] Should generate a password complex enough for AD (67/300 attempt) 25ms [+] Should generate a password complex enough for AD (68/300 attempt) 43ms [+] Should generate a password complex enough for AD (69/300 attempt) 22ms [+] Should generate a password complex enough for AD (70/300 attempt) 42ms [+] Should generate a password complex enough for AD (71/300 attempt) 28ms [+] Should generate a password complex enough for AD (72/300 attempt) 36ms [+] Should generate a password complex enough for AD (73/300 attempt) 26ms [+] Should generate a password complex enough for AD (74/300 attempt) 31ms [+] Should generate a password complex enough for AD (75/300 attempt) 33ms [+] Should generate a password complex enough for AD (76/300 attempt) 28ms [+] Should generate a password complex enough for AD (77/300 attempt) 34ms [+] Should generate a password complex enough for AD (78/300 attempt) 33ms [+] Should generate a password complex enough for AD (79/300 attempt) 34ms [+] Should generate a password complex enough for AD (80/300 attempt) 33ms [+] Should generate a password complex enough for AD (81/300 attempt) 31ms [+] Should generate a password complex enough for AD (82/300 attempt) 24ms [+] Should generate a password complex enough for AD (83/300 attempt) 28ms [+] Should generate a password complex enough for AD (84/300 attempt) 33ms [+] Should generate a password complex enough for AD (85/300 attempt) 27ms [+] Should generate a password complex enough for AD (86/300 attempt) 29ms [+] Should generate a password complex enough for AD (87/300 attempt) 31ms [+] Should generate a password complex enough for AD (88/300 attempt) 35ms [+] Should generate a password complex enough for AD (89/300 attempt) 23ms [+] Should generate a password complex enough for AD (90/300 attempt) 34ms [+] Should generate a password complex enough for AD (91/300 attempt) 21ms [+] Should generate a password complex enough for AD (92/300 attempt) 46ms [+] Should generate a password complex enough for AD (93/300 attempt) 24ms [+] Should generate a password complex enough for AD (94/300 attempt) 29ms [+] Should generate a password complex enough for AD (95/300 attempt) 30ms [+] Should generate a password complex enough for AD (96/300 attempt) 37ms [+] Should generate a password complex enough for AD (97/300 attempt) 26ms [+] Should generate a password complex enough for AD (98/300 attempt) 30ms [+] Should generate a password complex enough for AD (99/300 attempt) 33ms [+] Should generate a password complex enough for AD (100/300 attempt) 27ms [+] Should generate a password complex enough for AD (101/300 attempt) 25ms [+] Should generate a password complex enough for AD (102/300 attempt) 43ms [+] Should generate a password complex enough for AD (103/300 attempt) 35ms [+] Should generate a password complex enough for AD (104/300 attempt) 35ms [+] Should generate a password complex enough for AD (105/300 attempt) 26ms [+] Should generate a password complex enough for AD (106/300 attempt) 33ms [+] Should generate a password complex enough for AD (107/300 attempt) 34ms [+] Should generate a password complex enough for AD (108/300 attempt) 25ms [+] Should generate a password complex enough for AD (109/300 attempt) 30ms [+] Should generate a password complex enough for AD (110/300 attempt) 32ms [+] Should generate a password complex enough for AD (111/300 attempt) 33ms [+] Should generate a password complex enough for AD (112/300 attempt) 28ms [+] Should generate a password complex enough for AD (113/300 attempt) 42ms [+] Should generate a password complex enough for AD (114/300 attempt) 26ms [+] Should generate a password complex enough for AD (115/300 attempt) 26ms [+] Should generate a password complex enough for AD (116/300 attempt) 29ms [+] Should generate a password complex enough for AD (117/300 attempt) 34ms [+] Should generate a password complex enough for AD (118/300 attempt) 27ms [+] Should generate a password complex enough for AD (119/300 attempt) 32ms [+] Should generate a password complex enough for AD (120/300 attempt) 30ms [+] Should generate a password complex enough for AD (121/300 attempt) 45ms [+] Should generate a password complex enough for AD (122/300 attempt) 26ms [+] Should generate a password complex enough for AD (123/300 attempt) 37ms [+] Should generate a password complex enough for AD (124/300 attempt) 34ms [+] Should generate a password complex enough for AD (125/300 attempt) 25ms [+] Should generate a password complex enough for AD (126/300 attempt) 33ms [+] Should generate a password complex enough for AD (127/300 attempt) 32ms [+] Should generate a password complex enough for AD (128/300 attempt) 32ms [+] Should generate a password complex enough for AD (129/300 attempt) 30ms [+] Should generate a password complex enough for AD (130/300 attempt) 25ms [+] Should generate a password complex enough for AD (131/300 attempt) 42ms [+] Should generate a password complex enough for AD (132/300 attempt) 24ms [+] Should generate a password complex enough for AD (133/300 attempt) 38ms [+] Should generate a password complex enough for AD (134/300 attempt) 35ms [+] Should generate a password complex enough for AD (135/300 attempt) 27ms [+] Should generate a password complex enough for AD (136/300 attempt) 37ms [+] Should generate a password complex enough for AD (137/300 attempt) 29ms [+] Should generate a password complex enough for AD (138/300 attempt) 22ms [+] Should generate a password complex enough for AD (139/300 attempt) 33ms [+] Should generate a password complex enough for AD (140/300 attempt) 29ms [+] Should generate a password complex enough for AD (141/300 attempt) 29ms [+] Should generate a password complex enough for AD (142/300 attempt) 34ms [+] Should generate a password complex enough for AD (143/300 attempt) 29ms [+] Should generate a password complex enough for AD (144/300 attempt) 39ms [+] Should generate a password complex enough for AD (145/300 attempt) 26ms [+] Should generate a password complex enough for AD (146/300 attempt) 26ms [+] Should generate a password complex enough for AD (147/300 attempt) 32ms [+] Should generate a password complex enough for AD (148/300 attempt) 21ms [+] Should generate a password complex enough for AD (149/300 attempt) 27ms [+] Should generate a password complex enough for AD (150/300 attempt) 30ms [+] Should generate a password complex enough for AD (151/300 attempt) 25ms [+] Should generate a password complex enough for AD (152/300 attempt) 32ms [+] Should generate a password complex enough for AD (153/300 attempt) 33ms [+] Should generate a password complex enough for AD (154/300 attempt) 33ms [+] Should generate a password complex enough for AD (155/300 attempt) 31ms [+] Should generate a password complex enough for AD (156/300 attempt) 28ms [+] Should generate a password complex enough for AD (157/300 attempt) 43ms [+] Should generate a password complex enough for AD (158/300 attempt) 24ms [+] Should generate a password complex enough for AD (159/300 attempt) 36ms [+] Should generate a password complex enough for AD (160/300 attempt) 27ms [+] Should generate a password complex enough for AD (161/300 attempt) 27ms [+] Should generate a password complex enough for AD (162/300 attempt) 34ms [+] Should generate a password complex enough for AD (163/300 attempt) 34ms [+] Should generate a password complex enough for AD (164/300 attempt) 31ms [+] Should generate a password complex enough for AD (165/300 attempt) 32ms [+] Should generate a password complex enough for AD (166/300 attempt) 25ms [+] Should generate a password complex enough for AD (167/300 attempt) 35ms [+] Should generate a password complex enough for AD (168/300 attempt) 23ms [+] Should generate a password complex enough for AD (169/300 attempt) 29ms [+] Should generate a password complex enough for AD (170/300 attempt) 36ms [+] Should generate a password complex enough for AD (171/300 attempt) 25ms [+] Should generate a password complex enough for AD (172/300 attempt) 23ms [+] Should generate a password complex enough for AD (173/300 attempt) 24ms [+] Should generate a password complex enough for AD (174/300 attempt) 25ms [+] Should generate a password complex enough for AD (175/300 attempt) 33ms [+] Should generate a password complex enough for AD (176/300 attempt) 32ms [+] Should generate a password complex enough for AD (177/300 attempt) 32ms [+] Should generate a password complex enough for AD (178/300 attempt) 34ms [+] Should generate a password complex enough for AD (179/300 attempt) 32ms [+] Should generate a password complex enough for AD (180/300 attempt) 23ms [+] Should generate a password complex enough for AD (181/300 attempt) 33ms [+] Should generate a password complex enough for AD (182/300 attempt) 25ms [+] Should generate a password complex enough for AD (183/300 attempt) 24ms [+] Should generate a password complex enough for AD (184/300 attempt) 31ms [+] Should generate a password complex enough for AD (185/300 attempt) 35ms [+] Should generate a password complex enough for AD (186/300 attempt) 30ms [+] Should generate a password complex enough for AD (187/300 attempt) 31ms [+] Should generate a password complex enough for AD (188/300 attempt) 34ms [+] Should generate a password complex enough for AD (189/300 attempt) 27ms [+] Should generate a password complex enough for AD (190/300 attempt) 43ms [+] Should generate a password complex enough for AD (191/300 attempt) 32ms [+] Should generate a password complex enough for AD (192/300 attempt) 31ms [+] Should generate a password complex enough for AD (193/300 attempt) 24ms [+] Should generate a password complex enough for AD (194/300 attempt) 35ms [+] Should generate a password complex enough for AD (195/300 attempt) 33ms [+] Should generate a password complex enough for AD (196/300 attempt) 27ms [+] Should generate a password complex enough for AD (197/300 attempt) 29ms [+] Should generate a password complex enough for AD (198/300 attempt) 27ms [+] Should generate a password complex enough for AD (199/300 attempt) 36ms [+] Should generate a password complex enough for AD (200/300 attempt) 27ms [+] Should generate a password complex enough for AD (201/300 attempt) 24ms [+] Should generate a password complex enough for AD (202/300 attempt) 35ms [+] Should generate a password complex enough for AD (203/300 attempt) 31ms [+] Should generate a password complex enough for AD (204/300 attempt) 32ms [+] Should generate a password complex enough for AD (205/300 attempt) 36ms [+] Should generate a password complex enough for AD (206/300 attempt) 27ms [+] Should generate a password complex enough for AD (207/300 attempt) 37ms [+] Should generate a password complex enough for AD (208/300 attempt) 30ms [+] Should generate a password complex enough for AD (209/300 attempt) 29ms [+] Should generate a password complex enough for AD (210/300 attempt) 36ms [+] Should generate a password complex enough for AD (211/300 attempt) 26ms [+] Should generate a password complex enough for AD (212/300 attempt) 29ms [+] Should generate a password complex enough for AD (213/300 attempt) 32ms [+] Should generate a password complex enough for AD (214/300 attempt) 29ms [+] Should generate a password complex enough for AD (215/300 attempt) 31ms [+] Should generate a password complex enough for AD (216/300 attempt) 28ms [+] Should generate a password complex enough for AD (217/300 attempt) 33ms [+] Should generate a password complex enough for AD (218/300 attempt) 22ms [+] Should generate a password complex enough for AD (219/300 attempt) 37ms [+] Should generate a password complex enough for AD (220/300 attempt) 32ms [+] Should generate a password complex enough for AD (221/300 attempt) 35ms [+] Should generate a password complex enough for AD (222/300 attempt) 36ms [+] Should generate a password complex enough for AD (223/300 attempt) 21ms [+] Should generate a password complex enough for AD (224/300 attempt) 29ms [+] Should generate a password complex enough for AD (225/300 attempt) 29ms [+] Should generate a password complex enough for AD (226/300 attempt) 28ms [+] Should generate a password complex enough for AD (227/300 attempt) 39ms [+] Should generate a password complex enough for AD (228/300 attempt) 24ms [+] Should generate a password complex enough for AD (229/300 attempt) 30ms [+] Should generate a password complex enough for AD (230/300 attempt) 24ms [+] Should generate a password complex enough for AD (231/300 attempt) 38ms [+] Should generate a password complex enough for AD (232/300 attempt) 33ms [+] Should generate a password complex enough for AD (233/300 attempt) 24ms [+] Should generate a password complex enough for AD (234/300 attempt) 31ms [+] Should generate a password complex enough for AD (235/300 attempt) 26ms [+] Should generate a password complex enough for AD (236/300 attempt) 36ms [+] Should generate a password complex enough for AD (237/300 attempt) 25ms [+] Should generate a password complex enough for AD (238/300 attempt) 38ms [+] Should generate a password complex enough for AD (239/300 attempt) 25ms [+] Should generate a password complex enough for AD (240/300 attempt) 33ms [+] Should generate a password complex enough for AD (241/300 attempt) 34ms [+] Should generate a password complex enough for AD (242/300 attempt) 26ms [+] Should generate a password complex enough for AD (243/300 attempt) 26ms [+] Should generate a password complex enough for AD (244/300 attempt) 33ms [+] Should generate a password complex enough for AD (245/300 attempt) 29ms [+] Should generate a password complex enough for AD (246/300 attempt) 43ms [+] Should generate a password complex enough for AD (247/300 attempt) 25ms [+] Should generate a password complex enough for AD (248/300 attempt) 42ms [+] Should generate a password complex enough for AD (249/300 attempt) 28ms [+] Should generate a password complex enough for AD (250/300 attempt) 27ms [+] Should generate a password complex enough for AD (251/300 attempt) 23ms [+] Should generate a password complex enough for AD (252/300 attempt) 37ms [+] Should generate a password complex enough for AD (253/300 attempt) 25ms [+] Should generate a password complex enough for AD (254/300 attempt) 40ms [+] Should generate a password complex enough for AD (255/300 attempt) 32ms [+] Should generate a password complex enough for AD (256/300 attempt) 28ms [+] Should generate a password complex enough for AD (257/300 attempt) 32ms [+] Should generate a password complex enough for AD (258/300 attempt) 24ms [+] Should generate a password complex enough for AD (259/300 attempt) 34ms [+] Should generate a password complex enough for AD (260/300 attempt) 29ms [+] Should generate a password complex enough for AD (261/300 attempt) 39ms [+] Should generate a password complex enough for AD (262/300 attempt) 25ms [+] Should generate a password complex enough for AD (263/300 attempt) 29ms [+] Should generate a password complex enough for AD (264/300 attempt) 23ms [+] Should generate a password complex enough for AD (265/300 attempt) 23ms [+] Should generate a password complex enough for AD (266/300 attempt) 34ms [+] Should generate a password complex enough for AD (267/300 attempt) 27ms [+] Should generate a password complex enough for AD (268/300 attempt) 35ms [+] Should generate a password complex enough for AD (269/300 attempt) 30ms [+] Should generate a password complex enough for AD (270/300 attempt) 34ms [+] Should generate a password complex enough for AD (271/300 attempt) 31ms [+] Should generate a password complex enough for AD (272/300 attempt) 28ms [+] Should generate a password complex enough for AD (273/300 attempt) 22ms [+] Should generate a password complex enough for AD (274/300 attempt) 38ms [+] Should generate a password complex enough for AD (275/300 attempt) 28ms [+] Should generate a password complex enough for AD (276/300 attempt) 23ms [+] Should generate a password complex enough for AD (277/300 attempt) 28ms [+] Should generate a password complex enough for AD (278/300 attempt) 34ms [+] Should generate a password complex enough for AD (279/300 attempt) 31ms [+] Should generate a password complex enough for AD (280/300 attempt) 31ms [+] Should generate a password complex enough for AD (281/300 attempt) 24ms [+] Should generate a password complex enough for AD (282/300 attempt) 32ms [+] Should generate a password complex enough for AD (283/300 attempt) 24ms [+] Should generate a password complex enough for AD (284/300 attempt) 26ms [+] Should generate a password complex enough for AD (285/300 attempt) 24ms [+] Should generate a password complex enough for AD (286/300 attempt) 29ms [+] Should generate a password complex enough for AD (287/300 attempt) 34ms [+] Should generate a password complex enough for AD (288/300 attempt) 33ms [+] Should generate a password complex enough for AD (289/300 attempt) 22ms [+] Should generate a password complex enough for AD (290/300 attempt) 23ms [+] Should generate a password complex enough for AD (291/300 attempt) 36ms [+] Should generate a password complex enough for AD (292/300 attempt) 31ms [+] Should generate a password complex enough for AD (293/300 attempt) 23ms [+] Should generate a password complex enough for AD (294/300 attempt) 31ms [+] Should generate a password complex enough for AD (295/300 attempt) 31ms [+] Should generate a password complex enough for AD (296/300 attempt) 37ms [+] Should generate a password complex enough for AD (297/300 attempt) 35ms [+] Should generate a password complex enough for AD (298/300 attempt) 29ms [+] Should generate a password complex enough for AD (299/300 attempt) 22ms [+] Should generate a password complex enough for AD (300/300 attempt) 30ms Tests completed in 11.48s Passed: 396 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0 Code coverage report: Covered 100.00% of 11 analyzed commands in 1 file. |
Code coverage of 100% and hundreds of tests passed make me feel confident that this function is resilient and in case a new issue will be found, I can simply add a specific test to catch/prevent it from happening again in case feature changes are modifying the behaviour of my code.
And this is the Pester script used for unit testing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# Paolo Frigo, 2018 # https://www.scriptinglibrary.com . .\New-RandomPassword.ps1 Describe "New-RandomPassword" -Tags "Unit Tests" { it "Should generate a String" { (New-RandomPassword).GetType().Name | Should BeExactly "String" } #Pick a password Length between 0 and 7 and 33 and 100 characters $n = 0..1 + 33..100 foreach ($length in $n){ it "Should throw an exeption if the password Length ($length) is out of the accepted range (8-32)" { {New-RandomPassword -Length 5} | Should throw } } #Pick a password Length between 8 and 32 characters $n = 8..32 foreach ($length in $n){ It "Should generate a password $length character long" { (New-RandomPassword($length)).length | Should BeExactly "$length" } } #Test/Generate 300 passwords foreach ($attempt in (1..300)){ It "Should generate a password complex enough for AD ($attempt/300 attempt)"{ $n = 8..32 | Get-Random $RegEx = "(?=^.{8,32}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*" (New-RandomPassword($n)) -match $RegEx } } } |