-
-
Save holli-holzer/6509e002e7e41587294ca6abb0c82138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constant $all = set('!' .. '~'); | |
constant $digit = set('0' .. '9'); | |
constant $lower = set('a' .. 'z'); | |
constant $upper = set('A' .. 'Z'); | |
constant $alnum = $digit ∪ $lower ∪ $upper; | |
constant $symbol = $all (-) $alnum; # ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ | |
sub MAIN ( | |
Int $length where * ≥ 4 = 32, #= desired length of password | |
Int :n(:$number) = 1, #= number of passwords to print | |
Str :x(:$extra), #= desired symbols to use (does not use any other symbols) | |
Bool :d(:$dumb) #= remove all symbols from password | |
) { | |
my @char-sets = $dumb ?? ( $digit, $lower, $upper ) !! | |
$extra ?? ( $digit, $lower, $upper, set($extra.comb) ) !! | |
( $digit, $lower, $upper, $symbol ); | |
say gen-password( $length, @char-sets ) | |
for ^$number; | |
} | |
sub gen-password( $length, @char-sets ) | |
{ | |
my $allowed-chars = [∪] @char-sets; | |
my @password; | |
loop { | |
@password = $allowed-chars.roll( $length ); | |
last with test-password( @password, @char-sets ); | |
} | |
@password.join; | |
} | |
sub test-password( @password, @char-sets ) | |
{ | |
for @char-sets -> $set { | |
fail "Invalid Password: None of { $set.keys.join( ", " ) }" unless so any(@password) (elem) $set; | |
} | |
return True; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment