blib/lib/Crypt/Bcrypt.pm

filter: Statementstmt 91.9% 57 / 62 Branchbran 44.4% 8 / 18 Conditioncond 61.5% 8 / 13 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 77.9% 92 / 118 CC 15 SCAR 12.6
CC
15
Cov
78%
bcrypt
5.8
_get_parameters
4.5
bcrypt_prehashed
4.3
CRAP 3.5
↑ Summary crypt_blowfish.c »
1 package Crypt::Bcrypt;
2 2 $Crypt::Bcrypt::VERSION = '0.011';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 XSLoader::load('Crypt::Bcrypt');
8  
9 2 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our @EXPORT_OK = qw(bcrypt bcrypt_check bcrypt_prehashed bcrypt_check_prehashed bcrypt_hashed bcrypt_check_hashed bcrypt_needs_rehash bcrypt_supported_prehashes);
11  
12 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use MIME::Base64 2.21 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 24 ▶sub bcrypt {
Subroutine bcrypt: called CC=5 SCAR=17.5
Pod documented
17 24 my ($password, $subtype, $cost, $salt) = @_;
18 24 ▶ croak "Unknown subtype $subtype" if $subtype !~ /^2[abxy]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $subtype =~ /^2[abxy]$/ 0 24 24
19 24 ▶ croak "Invalid cost factor $cost" if $cost < 4 || $cost > 31;
Branch: 1 branch
BranchTrueFalseTotal
if $cost < 4 or $cost > 31 0 24 24
Condition33%1/3
$cost < 4 or $cost > 31
l!l&&r!l&&!r
0024
MC/DC0%0/2
$cost < 4 or $cost > 31
$cost < 4 $cost > 31
Truth table33%1/3
A or B
A= $cost < 4B= $cost > 31
ABresult
1X1
011
000
20 24 ▶ croak "Salt must be 16 bytes" if length $salt != 16;
Branch: 1 branch
BranchTrueFalseTotal
if length $salt != 16 0 24 24
21 24 my $encoded_salt = encode_base64($salt, "");
22 24 $encoded_salt =~ tr{A-Za-z0-9+/=}{./A-Za-z0-9}d;
23 24 return _bcrypt_hashpw($password, sprintf '$%s$%02d$%s', $subtype, $cost, $encoded_salt);
24 }
25  
26 2 my $subtype_qr = qr/2[abxy]/;
27 2 my $cost_qr = qr/\d{2}/;
28 2 my $salt_qr = qr{ [./A-Za-z0-9]{22} }x;
29 2 my $algo_qr = qr{ sha[0-9]+ }x;
30  
31 2 my %hash_for = (
32 sha256 => \&Digest::SHA::hmac_sha256,
33 sha384 => \&Digest::SHA::hmac_sha384,
34 sha512 => \&Digest::SHA::hmac_sha512,
35 );
36  
37 2 ▶sub bcrypt_prehashed {
Subroutine bcrypt_prehashed: called CC=4 SCAR=14.6
Pod documented
38 2 my ($password, $subtype, $cost, $salt, $algorithm) = @_;
39 2 ▶ if (length $algorithm) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $algorithm) { } 2 0 2
40 2 (my $encoded_salt = encode_base64($salt, "")) =~ tr{A-Za-z0-9+/=}{./A-Za-z0-9}d;
41 2 ▶ my $hasher = $hash_for{$algorithm} || croak "No such hash $algorithm";
Condition50%1/2
$hash_for{$algorithm} || croak("No such hash $algorithm")
l!l
20
MC/DC0%0/1
$hash_for{$algorithm} || croak("No such hash $algorithm")
$hash_for{$algorithm}
Truth table50%1/2
A
A= $hash_for{$algorithm}
Aresult
11
00
42 2 my $hashed_password = encode_base64($hasher->($password, $encoded_salt), "");
43 2 my $hash = bcrypt($hashed_password, $subtype, $cost, $salt);
44 2 ▶ $hash =~ s{ ^ \$ ($subtype_qr) \$ ($cost_qr) \$ ($salt_qr) }{\$bcrypt-$algorithm\$v=2,t=$1,r=$2\$$3\$}x or croak $hash;
Branch: 1 branch
BranchTrueFalseTotal
$hash =~ s/ ^ \$ ($subtype_qr) \$ ($cost_qr) \$ ($salt_qr) /\$bcrypt-$algorithm\$v=2,t=$1,r=$2\$$3\$/x or croak($hash) 2 0 2
45 2 return $hash;
46 }
47 else {
48 0 bcrypt($password, $subtype, $cost, $salt);
49 }
50 }
51  
52 4 ▶sub bcrypt_check_prehashed {
Subroutine bcrypt_check_prehashed: called CC=3 SCAR=12.0
Pod documented
53 4 my ($password, $hash) = @_;
54 4 ▶ if ($hash =~ s/ ^ \$ bcrypt-(\w+) \$ v=2,t=($subtype_qr),r=($cost_qr) \$ ($salt_qr) \$ /\$$2\$$3\$$4/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash =~ s/ ^ \$ bcrypt-(\w+) \$ v=2,t=($subtype_qr),r=($cost_qr) \$ ($salt_qr) \$ /\$$2\$$3\$$4/x) { } 4 0 4
55 4 ▶ my $hasher = $hash_for{$1} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $hasher = $hash_for{$1} or return 0 4 0 4
56 4 return bcrypt_check(encode_base64($hasher->($password, $4), ""), $hash);
57 }
58 else {
59 0 return bcrypt_check($password, $hash);
60 }
61 }
62  
63 #legacy names
64 2 *bcrypt_hashed = \&bcrypt_prehashed;
65 2 *bcrypt_check_hashed = \&bcrypt_check_prehashed;
66  
67 3 ▶sub _get_parameters {
Subroutine _get_parameters: called CC=3 SCAR=15.1
68 3 my ($hash) = @_;
69 3 ▶ if ($hash =~ / \A \$ ($subtype_qr) \$ ($cost_qr) \$ /x) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hash =~ / \A \$ ($subtype_qr) \$ ($cost_qr) \$ /x) { } 3 0 3
elsif ($hash =~ / ^ \$ bcrypt-($algo_qr) \$ v=2,t=($subtype_qr),r=($cost_qr) \$ /x) { } 0 0 0
70 3 return ($1, $2, '');
71 }
72 elsif ($hash =~ / ^ \$ bcrypt-($algo_qr) \$ v=2,t=($subtype_qr),r=($cost_qr) \$ /x) {
73 0 return ($2, $3, $1);
74 }
75 0 return ('', 0, '');
76 }
77  
78 3 ▶sub bcrypt_needs_rehash {
Subroutine bcrypt_needs_rehash: called CC=4 SCAR=14.1
Pod documented
79 3 my ($hash, $wanted_subtype, $wanted_cost, $wanted_hash) = @_;
80 3 my ($my_subtype, $my_cost, $my_hash) = _get_parameters($hash);
81 3 ▶ return $my_subtype ne $wanted_subtype || $my_cost != $wanted_cost || $my_hash ne ($wanted_hash || '');
Condition75%6/8
$my_subtype ne $wanted_subtype || $my_cost != $wanted_cost || $my_hash ne ($wanted_hash || '')
l!l&&r!l&&!r
201
$my_subtype ne $wanted_subtype or $my_cost != $wanted_cost
l!l&&r!l&&!r
111
$wanted_hash || ''
l!l
01
MC/DC66%2/3
$my_subtype ne $wanted_subtype || $my_cost != $wanted_cost || $my_hash ne ($wanted_hash || '')
$my_subtype ne $wanted_subtype $my_cost != $wanted_cost $my_hash ne ($wanted_hash || '')
MC/DC0%0/1
$wanted_hash || ''
$wanted_hash
Truth table75%3/4
A or B || C
A= $my_subtype ne $wanted_subtypeB= $my_cost != $wanted_costC= $my_hash ne ($wanted_hash || '')
ABCresult
1XX1
01X1
0011
0000
Truth table50%1/2
A
A= $wanted_hash
Aresult
11
00
82 }
83  
84 0 ▶sub bcrypt_supported_prehashes {
Subroutine bcrypt_supported_prehashes: not called CC=1 SCAR=6.9
Pod documented
85 0 return sort keys %hash_for;
86 }
87  
88 2 1;
89  
90 # ABSTRACT: A modern bcrypt implementation
91  
92 __END__
↑ Summary crypt_blowfish.c »