Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

crypt sometimes returning undef

by Beaker (Beadle)
on Feb 14, 2018 at 09:39 UTC ( #1209122=perlquestion: print w/replies, xml ) Need Help??
Beaker has asked for the wisdom of the Perl Monks concerning the following question:

I've moved to a new server, which has changed my Perl version from v5.10.1 to v5.16.3

The following code is designed to generate a temporary user login password.

srand(time ^ $$); my @passset = ('a'..'k', 'm'..'n', 'p'..'z', '2'..'9'); my $passwrd1 = ""; for (my $i=0; $i<8; $i++) { $passwrd1 .= $passset[int(rand($#passset + + 1))]; } my $cryptedpass = crypt($passwrd1, substr($never_blank_value, 0, 2)); use Babel; my $y = new Babel; my $newpasswrd = $y->encode($cryptedpass, "$passwrd1");

Since moving server the value of $cryptedpass is sometimes undef and throwing warnings from

Replies are listed 'Best First'.
Re: crypt sometimes returning undef
by thanos1983 (Vicar) on Feb 14, 2018 at 11:59 UTC

    Hello Beaker,

    I was reading the documentation of crypt and I noticed from the sample code:

    if (crypt($word, $pwd) ne $pwd) { die "Sorry...\n"; } else { print "ok\n"; }

    They are using first the word and then the password. It is not related to your problem but I think it would be more clear in future reference:

    my $cryptedpass = crypt($passwrd1, substr($never_blank_value, 0, 2));

    The only reason that I can imagine that you might have $cryptedpass as blank is that you have $never_blank_value. You are using substr and you are cutting off the first 2 characters of the string. In case the string it has less than 2 characters then you will end up with undef value. Sample of proposed code:

    Sample of error:

    Update: Based on the link 0007695: Crypt bug that fellow Monk poj point it out you might be passing non acceptable characters. If this is the case add this to your code:

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Thank you, I believe this is the issue.

      My debug for an instance of this undef value has $never_blank_value as T_ (after substr)

      $never_blank_value contains azAZ09\-_ so it's likely this would break if it contains the hyphen or underscore, I will run a regex over $never_blank_value to filter out these disallowed characters.

Re: crypt sometimes returning undef
by johngg (Canon) on Feb 14, 2018 at 16:55 UTC

    You could save some fiddly typing by using ASCII values with chr in a map to generate your character ranges. I would generate the "salt" for crypt on the fly from the documented allowed characters rather than your mysterious $never_blank_value.

    johngg@shiraz ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' my @saltChars = map chr, 46 .. 57, 65 .. 90, 97 .. 122; my @passChars = map chr, 50 .. 57, 97 .. 107, 109, 110, 112 .. 122; my $salt = join q{}, map $saltChars[ rand @saltChars ], 1 .. 2; my $password = join q{}, map $passChars[ rand @passChars ], 1 .. 8; my $crypted = crypt $password, $salt; say qq{Salt : $salt}; say qq{Password : $password}; say qq{crypt()ed : $crypted};' Salt : pI Password : x3ds5fey crypt()ed : pIS1tIDqATjmQ

    Running this a few times produces

    Salt : mQ Password : jxaijfqu crypt()ed : mQf.VRB0dpcbk
    Salt : /5 Password : h7si8gh9 crypt()ed : /5.1Gj12mBfQY
    Salt : Tp Password : yjqyaikk crypt()ed : Tp/YoIkhaUEOE

    I hope this is helpful.

    Update: Fixed missing </code> tag.



Re: crypt sometimes returning undef
by poj (Monsignor) on Feb 14, 2018 at 13:24 UTC
    I've moved to a new server,

    A previous post of yours mentioned Centos 7

    This post suggests that allowable salt characters may have changed

      Thank you.
Re: crypt sometimes returning undef
by hippo (Canon) on Feb 14, 2018 at 10:24 UTC
    my $cryptedpass = crypt($passwrd1, $never_blank_value));

    Is that a paste from your code? It appears to have unbalanced brackets. Perhaps that's part of the problem?

    Alternatively, if you add a diagnostic on such intermittent occurences you might spot a connection. eg:

    die "Empty result from crypt($passwrd1, $never_blank_value)" unless de +fined $cryptedpass;
      I've fixed this, sorry it was when I replaced the variable name with $never_blank_value for purposes of copying it here.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1209122]
Approved by Arunbear
Front-paged by 1nickt
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2018-08-17 05:44 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (174 votes). Check out past polls.