in reply to Regular expression to check for qwerty sequence in a password
I posted a couple of replies in this thread then, just out of interest, continued to work on adding functionality. It may be overkill for what you want but you could pick out any bits that are useful.
use strict; use warnings; use 5.014; use re qw{ eval }; my $max = shift || 3; # Create pattern for consecutive ascending and descending digits. # my $ascDigPatt = q{(?x) ( ( \d ) (??{ join q{}, map { $2 + $_ } 1 .. $max }) ) }; my $descDigPatt = q{(?x) ( ( \d ) (??{ join q{}, map { $2 - $_ } 1 .. $max }) ) }; # Create pattern for consecutive ascending letters. # my $ucAscEnd = chr( ord( q{Z} ) - $max ); my $lcAscEnd = chr( ord( q{z} ) - $max ); my $ascLtrPatt = join q{ }, q{(?x)}, qq{( ( [A-${ucAscEnd}a-${lcAscEnd}] )}, q{(??{ join q{}, map { chr( ord( $2 ) + $_ ) } 1 .. $max }) ) }; # Create pattern for consecutive descending letters. # my $ucDescStart = chr( ord( q{A} ) + $max ); my $lcDescStart = chr( ord( q{a} ) + $max ); my $descLtrPatt = join q{ }, q{(?x)}, qq{( ( [${ucDescStart}-Z${lcDescStart}-z] )}, q{(??{ join q{}, map { chr( ord( $2 ) - $_ ) } 1 .. $max }) ) }; my $kbdTopRow = q{qwertyuiop}; my $kbdMiddleRow = q{asdfghjkl}; my $kbdBottomRow = q{zxcvbnm}; # Create passwords to test. # my @passwords = qw{ 1234 1243 4321 298761 4562 4568 4578 123 12 1 01234 01243 04321 0298761 04562 04568 04578 0123 012 01 a1234 1a234 12a34 123a4 1234a a1b2c3 a12b34c56 a1b2c3d a12b34c56d a123b45c6 a12b345c6 a123b45c6d a12b345c6d 1a2 1ab2 12ab34 12abc34def 12abc34def567 abc ab12c 2345 234y 012345 2356 3457 abcd XWVU bcd1e ZYX ZYXW kjyihgfs abcd def PQRST YXWV bcde bcdf stu stuc stuv wxyz hgfe lkjh edbca dhfe }; foreach my $pw ( @passwords ) { print qq{$pw - }; my $err = checkConsec( $pw ); say $err ? $err : q{pass}; } sub checkConsec { my $pw = shift; return qq{too many consecutive ascending digits - $1} if $pw =~ m{$ascDigPatt}; return qq{too many consecutive descending digits - $1} if $pw =~ m{$descDigPatt}; return qq{too many consecutive ascending letters - $1} if $pw =~ m{$ascLtrPatt}; return qq{too many consecutive descending letters - $1} if $pw =~ m{$descLtrPatt}; return 0; }
I didn't post it at the time but hope it might be helpful now.
Update: Looking at the code again, it seems that I hadn't got around to implementing the "qwerty" part of the problem yet :-}
Cheers,
JohnGG
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Regular expression to check for qwerty sequence in a password
by bradcathey (Prior) on Oct 10, 2016 at 06:13 UTC | |
by johngg (Canon) on Oct 15, 2016 at 21:07 UTC |