Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Efficient way to verify scalar contents

by choroba (Cardinal)
on Jun 22, 2020 at 09:17 UTC ( [id://11118354]=note: print w/replies, xml ) Need Help??


in reply to Efficient way to verify scalar contents

my @RE = (qr/[[:lower:]]/, qr/[[:upper:]]/, qr/[0-9]/, qr/[^[:alnum:]] +/); sub check { local ($_) = @_; my $length = length; return if $length < 8; return if $length < 12 && (! /$RE[0]/ || ! /$RE[1]/ || ! /$RE[2]/ +|| ! /$RE[3]/); return if $length < 16 && (! /$RE[0]/ || ! /$RE[1]/ || ! /$RE[2]/) +; return if $length < 20 && (! /$RE[0]/ || ! /$RE[1]/); return 1 }

Tested against the tests from Re: Efficient way to verify scalar contents (updated x3) (haukex++).

Update: Or, if you want to abstract away the repeating $RE[...]

use List::Util qw{ any }; my @RE = (qr/[[:lower:]]/, qr/[[:upper:]]/, qr/[0-9]/, qr/[^[:alnum:]] +/); sub check { my ($password) = @_; my $length = length $password; return if $length < 8; return if $length < 12 && any { $password !~ $RE[$_] } 0 .. 3; return if $length < 16 && any { $password !~ $RE[$_] } 0 .. 2; return if $length < 20 && any { $password !~ $RE[$_] } 0 .. 1; return 1 }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Efficient way to verify scalar contents
by LanX (Saint) on Jun 22, 2020 at 12:12 UTC
    Please correct me, but do these character classes not depend on localisation?

    [[:lower:]] [[:upper:]]

    I think passwords should rather be defined in terms of ASCII

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

      I'm not sure. I remember [a-z] depending on locale, but it was probably in bash rather than Perl...
      $ (LC_ALL=en_US.UTF-8; [[ é =~ [a-z] ]] || echo no) $ (LC_ALL=C; [[ é =~ [a-z] ]] || echo no) no $ perl -Mutf8 -wE 'say "é" =~ /[[:lower:]]/' 1 $ perl -Mutf8 -wE 'say "é" =~ /[a-z]/' $

      Update: Switched from <c> to <pre> and back.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-23 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found