Skip to content

Instantly share code, notes, and snippets.

@holli-holzer
Created January 21, 2020 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holli-holzer/6509e002e7e41587294ca6abb0c82138 to your computer and use it in GitHub Desktop.
Save holli-holzer/6509e002e7e41587294ca6abb0c82138 to your computer and use it in GitHub Desktop.
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