| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 19 | 24 | ▶ | croak "Invalid cost factor $cost" if $cost < 4 || $cost > 31; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cost < 4 or $cost > 31
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
| |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 24 | ▶ | croak "Salt must be 16 bytes" if length $salt != 16; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 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")
MC/DC0%0/1
$hash_for{$algorithm} || croak("No such hash $algorithm")
$hash_for{$algorithm}
Truth table50%1/2
A
A= $hash_for{$algorithm}
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | my $hasher = $hash_for{$1} or return 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 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 || '')
$my_subtype ne $wanted_subtype or $my_cost != $wanted_cost
$wanted_hash || ''
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 || '')
Truth table50%1/2
A
A= $wanted_hash
| |||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||