Passphrase/Util/Crypt64.pm

filter: Statementstmt 100% 52 / 52 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 75.0% 3 / 4 totaltotal 97.1% 68 / 70 CC 6 SCAR 6.6
CC
6
Cov
98%
encode_crypt64_number
3.0
encode_crypt64
2.0
decode_crypt64
2.0
CRAP 1.9
« Passphrase/Fallback.pm ↑ Summary Passphrase/Encoder.pm »
1 package Crypt::Passphrase::Util::Crypt64;
2 4 $Crypt::Passphrase::Util::Crypt64::VERSION = '0.024';
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 my @strings = qw/encode_crypt64 decode_crypt64/;
8 4 my @numbers = qw/encode_crypt64_number decode_crypt64_number/;
9 4 our @EXPORT_OK = (@strings, @numbers);
10 4 our %EXPORT_TAGS = (
11 all => \@EXPORT_OK,
12 strings => \@strings,
13 numbers => \@numbers,
14 );
15  
16 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 my $base64_digits = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
19  
20 19 ▶sub encode_crypt64 {
Subroutine encode_crypt64: called CC=2 SCAR=6.9
Pod documented
21 19 my $bytes = shift;
22 19 my $nbytes = length $bytes;
23 19 my $npadbytes = 2 - ($nbytes + 2) % 3;
24 19 $bytes .= "\0" x $npadbytes;
25 19 my $digits = '';
26 19 ▶ for (my $i = 0; $i < $nbytes; $i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nbytes) 75 19 94
27 75 my $v = ord(substr $bytes, $i, 1) |
28 (ord(substr $bytes, $i + 1, 1) << 8) |
29 (ord(substr $bytes, $i + 2, 1) << 16);
30 75 $digits .= substr($base64_digits, $v & 0x3f, 1) .
31 substr($base64_digits, ($v >> 6) & 0x3f, 1) .
32 substr($base64_digits, ($v >> 12) & 0x3f, 1) .
33 substr($base64_digits, ($v >> 18) & 0x3f, 1);
34 }
35 19 substr $digits, -$npadbytes, $npadbytes, '';
36 19 return $digits;
37 }
38  
39 3 ▶sub decode_crypt64 {
Subroutine decode_crypt64: called CC=2 SCAR=6.9
Pod documented
40 3 my $digits = shift;
41 3 my $ndigits = length($digits);
42 3 my $npadbytes = 3 - ($ndigits + 3) % 4;
43 3 $digits .= '.' x $npadbytes;
44 3 my $bytes = '';
45 3 ▶ for(my $i = 0; $i < $ndigits; $i += 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ndigits) 11 3 14
46 11 my $v = index($base64_digits, substr $digits, $i, 1) |
47 (index($base64_digits, substr $digits, $i + 1, 1) << 6) |
48 (index($base64_digits, substr $digits, $i + 2, 1) << 12) |
49 (index($base64_digits, substr $digits, $i + 3, 1) << 18);
50 11 $bytes .= chr($v & 0xff) . chr(($v >> 8) & 0xff) . chr(($v >> 16) & 0xff);
51 }
52 3 substr $bytes, -$npadbytes, $npadbytes, '';
53 3 return $bytes;
54 }
55  
56 7 ▶sub encode_crypt64_number {
Subroutine encode_crypt64_number: called CC=3 SCAR=11.0
Pod undocumented
57 7 my ($input, $length) = @_;
58 7 my $output = '';
59 7 for (1 .. $length) {
60 27 my $remainder = $input % 64;
61 27 $output .= substr $base64_digits, $remainder, 1;
62 27 $input = int($input / 64);
63 }
64 7 ▶ croak "Number doesn't fit in $length characters" if $input != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $input != 0 0 7 7
65 7 return $output;
66 }
67  
68 6 ▶sub decode_crypt64_number {
Subroutine decode_crypt64_number: called CC=2 SCAR=6.9
Pod documented
69 6 my $input = shift;
70 6 my $result = 0;
71 6 for (0 .. length($input) - 1) {
72 22 $result += index($base64_digits, substr $input, $_, 1) * (1 << (6 * $_));
73 }
74 6 return $result;
75 }
76  
77 4 1;
78  
79 # ABSTRACT: An implementation of the crypt64 encoding
80  
81 __END__
« Passphrase/Fallback.pm ↑ Summary Passphrase/Encoder.pm »