Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^4: Efficient way to verify scalar contents

by LanX (Cardinal)
on Jun 22, 2020 at 12:04 UTC ( #11118360=note: print w/replies, xml ) Need Help??


in reply to Re^3: Efficient way to verify scalar contents
in thread Efficient way to verify scalar contents

Though for the sake of readability I'd rather go for a hash of boundaries.

you may also want to limit the max length of your password.

use strict; use warnings; use Data::Dump qw/pp dd/; use List::Util qw/min/; sub pw_not_ok { my $pw = shift; my $short = sub { "is too short" }; my $lowercase = sub { /[a-z]/ ? "" : "has no lowercase character" }; my $uppercase = sub { /[A-Z]/ ? "" : "has no uppercase character" }; ### UPDATE +D my $number = sub { /[0-9]/ ? "" : "has no number" }; my $special = sub { if ( /[\x21-\x2F]/ || /[\x3A-\x40]/ || /[\x5B-\x60]/ || /[\x7B-\x7E]/ ) { return ""; } else { return "has no special characters"; } }; my $pass = sub {""}; my %rules_by_max_length = ( 7 => [ $short ], 11 => [ $lowercase, $uppercase, $number ,$special ], 15 => [ $lowercase, $uppercase, $number ], 19 => [ $lowercase, $uppercase ], 50 => [ $pass ] ); my $len = length $pw; return "too long" if $len >= 50; my @boundaries = sort { $a <=> $b } keys %rules_by_max_length; for my $boundary ( @boundaries ) { next if $len > $boundary; my $rules = $rules_by_max_length{$boundary}; for my $rule ( @$rules ) { $_ = $pw; if ( my $err = $rule->() ) { return $err; } } } return; } # --------- Tests for my $pw ( "A" x 3 , "A" x 7 , "A" x 11, "a" x 5 . "A" x 3, "a" x 5 . "A" x 3 . "1", "a" x 5 . "A" x 3 . "1" .":", "a" x 24, "a" x 100, ){ if ( my $err = pw_not_ok($pw) ){ warn "ERROR: $pw is $err\n"; } else { print "OK: $pw\n" } }

ERROR: AAA is is too short ERROR: AAAAAAA is is too short ERROR: AAAAAAAAAAA is has no lowercase character ERROR: aaaaaAAA is has no number ERROR: aaaaaAAA1 is has no special characters ERROR: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is too long OK: aaaaaAAA1: OK: aaaaaaaaaaaaaaaaaaaaaaaa

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

UPDATE

see Re^6: Efficient way to verify scalar contents

Replies are listed 'Best First'.
Re^5: Efficient way to verify scalar contents
by haukex (Bishop) on Jun 22, 2020 at 13:17 UTC

    Note that both of your solutions don't report an error for e.g. 'aaaaaaaaa0-' and 'aaaaaaaaaaaaaaaaaaa', which are invalid as per the OP's code.

      you are right, I had a bug in the uppercase rule!

      my $uppercase =  sub { /[a-z]/ ? "" : "has no uppercase character" };

      should be

      my $uppercase =  sub { /[A-Z]/ ? "" : "has no uppercase character" };

      C&P is your enemy! =)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11118360]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2021-02-28 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?