blib/lib/OpenSMTPD/Password.pm

filter: Statementstmt 88.5% 54 / 61 Branchbran 56.2% 9 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 66.6% 2 / 3 totaltotal 80.0% 76 / 95 CC 11 SCAR 14.8
CC
11
Cov
80%
newhash
7.6
checkhash
5.3
BEGIN
1.0
CRAP 4.4
↑ Summary
1 package OpenSMTPD::Password;
2 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 require Exporter;
5 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our @ISA = qw(Exporter);
8  
9 1 our @EXPORT = qw(
10 );
11  
12 1 our @EXPORT_OK = qw(
13 newhash
14 checkhash
15 );
16  
17 1 our $VERSION = '0.03';
18  
19 1 my $openbsd_newhash;
20 1 eval {
21 1 require OpenSMTPD::Password::XS;
22 0 OpenSMTPD::Password::XS->VERSION(0.01);
23  
24 0 $openbsd_newhash = OpenSMTPD::Password::XS->can('newhash');
25 };
26  
27 # thank you match::simple
28 0 ▶eval($openbsd_newhash ? <<'XS' : <<'PP');
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine checkhash: called CC=5 SCAR=16.7
Subroutine newhash: called CC=7 SCAR=20.3
Subroutine strncmp: called CC=1 SCAR=0.1
Pod documented
Pod documented
Pod undocumented
Branch: 8 branches
BranchTrueFalseTotal
unless (length $password and length $goodhash) 0 1 1
if ($^O eq "openbsd") 0 1 1
unless (defined $c) 0 1 1
unless (length $password) 0 1 1
if ($^O eq "openbsd") 0 1 1
unless defined $hash 0 2 2
unless strncmp($hash, $buffer, length $buffer) 1 1 2
$openbsd_newhash ? : 0 1 1
Condition33%1/3
length $password and length $goodhash
!ll&&!rl&&r
001
MC/DC0%0/2
length $password and length $goodhash
length $password length $goodhash
Truth table33%1/3
A and B
A= length $passwordB= length $goodhash
ABresult
0X0
100
111
29  
30 sub IMPLEMENTATION () { "XS" }
31  
32 # if yo can do one surely you can do the other
33 *newhash = *OpenSMTPD::Password::XS::newhash;
34 *checkhash = *OpenSMTPD::Password::XS::checkhash;
35  
36 XS
37  
38 use Carp;
39 use BSD::arc4random;
40  
41 sub IMPLEMENTATION () { "PP" }
42  
43 tie my $rand, 'BSD::arc4random', '64';
44  
45 my $SALT_LEN = 16;
46  
47 my @itoa64 = qw{. / 0 1 2 3 4 5 6 7 8 9 A B C D
48 E F G H I J K L M N O P Q R S T
49 U V W X Y Z a b c d e f g h i j
50 k l m n o p q r s t u v w x y z};
51  
52 my @ids = qw{2a 6 5 3 2 1};
53  
54 sub newhash {
55 my ($password) = @_;
56  
57 unless (length($password)) {
58 croak "newhash('password')";
59 }
60  
61 if ($^O eq 'openbsd') {
62 return openbsd_newhash($password);
63 }
64  
65 my @salt;
66
67 for (1 .. $SALT_LEN) {
68 push @salt, $itoa64[$rand];
69 }
70  
71 my $hash;
72 my $fmt = '$%s$' . '%s' x $SALT_LEN . '$';
73 foreach my $id (@ids) {
74 no warnings 'redundant';
75 no warnings 'uninitialized';
76 my $buffer = sprintf($fmt, $id, @salt);
77 $hash = crypt($password, $buffer);
78 next unless (defined $hash);
79 next unless (strncmp($hash, $buffer, length($buffer)));
80 last;
81 }
82  
83 return $hash;
84 }
85  
86 sub checkhash {
87 my ($password, $goodhash) = @_;
88  
89 unless (length($password) and length($goodhash)) {
90 croak "checkhash(password, goodhash)"
91 }
92  
93 if ($^O eq "openbsd") {
94 return openbsd_checkhash($password, $goodhash);
95 }
96  
97 my $c = crypt($password, $goodhash);
98  
99 unless (defined $c) {
100 croak "crypt() failed";
101 }
102  
103 return ($c eq $goodhash);
104 }
105  
106 sub strncmp {
107 my ($a, $b, $n) = @_;
108  
109 return (substr($a, 0, $n) eq substr($b, 0, $n));
110 }
111  
112 PP
113  
114 1 1;
115 __END__
↑ Summary