in reply to crypt sometimes returning undef
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:
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $never_blank_value = 'T'; 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))]; } die 'Too small \$never_blank_value' if length $never_blank_value < 2; my $cryptedpass = crypt($passwrd1, substr($never_blank_value, 0, 2)); say $cryptedpass; __END__ $ perl test.pl Too small \$never_blank_value at test.pl line 13.
Sample of error:
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $never_blank_value = 'T'; 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))]; } # die 'Too small \$never_blank_value' if length $never_blank_value < 2 +; my $cryptedpass = crypt($passwrd1, substr($never_blank_value, 0, 2)); say $cryptedpass; __END__ perl test.pl Use of uninitialized value $cryptedpass in say at test.pl line 15.
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:
die 'Not accepted characters at \$never_blank_value' unless ($never_blank_value =~ /^[a-zA-Z0-9.\/]+$/);
Hope this helps, BR.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: crypt sometimes returning undef
by Beaker (Beadle) on Feb 15, 2018 at 11:05 UTC |